GitHub Action for deploying code via rsync over ssh. (with NodeJS)
Go to file
github-actions c0d6c08d4d chore(release): 1.0.0 [skip ci]
# 1.0.0 (2024-04-19)

### Bug Fixes

* [#113](https://github.com/ximplez/ssh-deploy/issues/113) limit ssh script input ([5894f5e](5894f5e290))
* [#113](https://github.com/ximplez/ssh-deploy/issues/113) limit ssh script output ([59827af](59827af83c))
* [#113](https://github.com/ximplez/ssh-deploy/issues/113) limit ssh script output - rebuild ([756a522](756a522533))
* [#118](https://github.com/ximplez/ssh-deploy/issues/118) check undefined default values ([f0c02fb](f0c02fb2a5))
* [#118](https://github.com/ximplez/ssh-deploy/issues/118) check undefined default values rebuild ([54f8b6c](54f8b6c60b))
* [#120](https://github.com/ximplez/ssh-deploy/issues/120) check undefined data ([2fbb060](2fbb06015d))
* add assets to semantic-release git ([0867b12](0867b12954))
* add branch instead of a version in a readme ([8218c8e](8218c8ed95))
* add compiled file ([627ac29](627ac29ece))
* add githubWorkspace as default source root ([3ef66d0](3ef66d0be9))
* Add info for Permission denied issue. ([845b578](845b578606))
* Add manual inputs for e2e test ([8f71595](8f715957ab))
* add NPM token ([2197bc6](2197bc60ef))
* add package json auto updates ([ce56d75](ce56d75fc1))
* add uuid for ssh scripts ([66f6e4b](66f6e4b367))
* Add visible Rsync CMD for debugging ([91b6d28](91b6d280ae))
* added the missing declarations [#177](https://github.com/ximplez/ssh-deploy/issues/177) ([bb271fe](bb271fe4c6))
* change npm to public ([191c82d](191c82d9b4))
* codeql build ([5e456a4](5e456a475a))
* codeql yaml syntax update ([87ad671](87ad6713b5))
* fix default source if empty ([9f191f4](9f191f4263))
* fix default TARGET ([4d08634](4d086346af))
* Fix default values used incorrectly. ([a1b383f](a1b383f560))
* fix manual release action ([245b7a9](245b7a9d2f))
* log buffer from rsync stdout ([8e19e0d](8e19e0d6bc))
* Log rsync command ([85f4a96](85f4a96743))
* minify dist ([87551c3](87551c3893))
* move e2e tests to main branch ([0069fae](0069faea9f))
* move e2e tests typo branch name ([6d3554b](6d3554b01e))
* normalize line endings in SSH key for the underlying OS ([3f5d9aa](3f5d9aab1a))
* only=prod package json ([95f4dc8](95f4dc8069))
* plugins versions ([2f40dde](2f40dde84f))
* readme update ([80a7f53](80a7f53ec9)), closes [#67](https://github.com/ximplez/ssh-deploy/issues/67)
* rebuild ([2169399](2169399fef))
* rebuild and update readme ([98025d6](98025d680e))
* remove change npm to public ([3c36af7](3c36af7577))
* replace i with ci ([50d1f57](50d1f576f9))
* semantic-release/npm update config ([40096bf](40096bf224))
* trigger automated release ([4d8bbf0](4d8bbf0deb))
* Update changelog ([765f5ff](765f5ffd31))
* update ncc ([20a0cae](20a0cae1ae))
* update npm package name ([9c4e411](9c4e4119ad))
* update plugins ([b2adc00](b2adc00c92))
* update plugins ([a50a999](a50a999528))
* update sub dependencies ([2a32c8d](2a32c8dcde))
* update v3 e2e test ([450bf42](450bf423f3))
* use main branch for e2e testes ([21451fe](21451fec97))
* yaml step name ([5c13615](5c13615d4b))
* 修复 ARGS 为空时导致 rsync 执行错误问题。 ([caaa95b](caaa95b651))

* Merge pull request #173 from jeromelachaud/main ([ac1908e](ac1908e5d2)), closes [#173](https://github.com/ximplez/ssh-deploy/issues/173)
* Merge pull request #83 from easingthemes/feature/#82-update-node-version ([76660a8](76660a8456)), closes [#83](https://github.com/ximplez/ssh-deploy/issues/83) [easingthemes/feature/#82](https://github.com/easingthemes/feature//issues/82)

### Features

* add e2e tests ([40f855a](40f855a08b))
* Add multi source and multi target support ([73a65ec](73a65ec97c))
* add semantic-release-action ([ac3c9b5](ac3c9b51d5))
* add ssh port from VARs, fix package-lock ([808b002](808b0020d4))
* Add SSH remote script support -  before and after rsync ([2cd8a82](2cd8a820e2))
* Add SSH_CMD_ARGS support ([51a0063](51a00631e2))

### Performance Improvements

* update default rsync options ([2be7efb](2be7efb376))

### BREAKING CHANGES

* update to use nodeJS v20
* update to use nodeJS v20
* Default rsync options updated to speed up default deployments.
from `rltgoDzvO` to `-rlgoDzvc -i`
* update to use nodeJS v16
2024-04-19 06:29:05 +00:00
.github/workflows fix own action version 2024-02-26 23:48:33 +01:00
dist fix: normalize line endings in SSH key for the underlying OS 2023-09-28 20:05:00 +02:00
docs chore(release): 1.0.0 [skip ci] 2024-04-19 06:29:05 +00:00
src update index.js 2024-04-19 14:28:28 +08:00
test feat: Add multi source and multi target support 2023-01-03 02:49:54 +01:00
.editorconfig [tests] add editorconfig and eslint 2020-04-11 16:26:19 +02:00
.eslintrc.js Feature/ssh cmd (#94) 2023-01-02 21:06:33 +01:00
.gitignore start e2e 2022-12-29 15:46:56 +01:00
.releaserc fix: add assets to semantic-release git 2021-05-28 01:23:17 +02:00
action.yml Update action.yml 2024-04-19 14:03:23 +08:00
LICENSE [init]: update Licence 2019-09-25 23:54:12 +02:00
package-lock.json add config.js... 2024-04-18 11:52:57 +08:00
package.json chore(release): 1.0.0 [skip ci] 2024-04-19 06:29:05 +00:00
README.md fix: Add info for Permission denied issue. 2024-01-31 01:59:36 +01:00

ssh deployments

Deploy code with rsync over ssh.

Execute remote scripts before or after rsync

NodeJS version is more than a minute faster than simple Docker version.

This GitHub Action deploys specific directory from GITHUB_WORKSPACE to a folder on a server via rsync over ssh, using NodeJS.

This action would usually follow a build/test action which leaves deployable code in GITHUB_WORKSPACE, eg dist;

In addition to rsync, this action provides scripts execution on remote host before and/or after rsync.

Configuration

Pass configuration with env vars

1. SSH_PRIVATE_KEY [required]

Private key part of an SSH key pair. The public key part should be added to the authorized_keys file on the server that receives the deployment.

More info for SSH keys: https://www.ssh.com/ssh/public-key-authentication

The keys should be generated using the PEM format. You can use this command

ssh-keygen -m PEM -t rsa -b 4096

Please Note: You should not set a Passphrase (keep it empty) for the private key you generated. Because rsync ssh (used for deploy) does not support private key password to be entered as a command line parameter.

2. REMOTE_HOST [required]

eg: mydomain.com

3. REMOTE_USER [required]

eg: myusername

4. REMOTE_PORT (optional, default '22')

eg: '59184'

5. ARGS (optional, default '-rlgoDzvc -i')

For any initial/required rsync flags, eg: -avzr --delete

6. SOURCE (optional, default '')

The source directory, path relative to $GITHUB_WORKSPACE root, eg: dist/. Multiple sources should be separated by space.

7. TARGET (optional, default '/home/REMOTE_USER/')

The target directory

8. EXCLUDE (optional, default '')

path to exclude separated by ,, ie: /dist/, /node_modules/

9. SCRIPT_BEFORE (optional, default '')

Script to run on host machine before rsync. Single line or multiline commands. Execution is preformed by storing commands in .sh file and executing it via .bash over ssh If you have issues with ssh connection, use this var, eg SCRIPT_BEFORE: ls. This will force known_hosts update, adding your host via ssh-keyscan.

10. SCRIPT_AFTER (optional, default '')

Script to run on host machine after rsync. Rsync output is stored in $RSYNC_STDOUT env variable.

11. SSH_CMD_ARGS (optional, default '-o StrictHostKeyChecking=no')

A list of ssh arguments, they must be prefixed with -o and separated by a comma, for example: -o SomeArgument=no, -o SomeOtherArgument=5

Usage

Use the latest version from Marketplace,eg: ssh-deploy@v2 or use the latest version from a branch, eg: ssh-deploy@main

  - name: Deploy to Staging server
    uses: easingthemes/ssh-deploy@main
    with:
      SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }}
      ARGS: "-rlgoDzvc -i"
      SOURCE: "dist/"
      REMOTE_HOST: ${{ secrets.REMOTE_HOST }}
      REMOTE_USER: ${{ secrets.REMOTE_USER }}
      TARGET: ${{ secrets.REMOTE_TARGET }}
      EXCLUDE: "/dist/, /node_modules/"
      SCRIPT_BEFORE: |
        whoami
        ls -al
      SCRIPT_AFTER: |
        whoami
        ls -al
        echo $RSYNC_STDOUT

Example usage in workflow

name: Node CI

on: [push]

jobs:
  build:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v3
    - name: Install Node.js
      uses: actions/setup-node@v3
      with:
        node-version: '16.x'
    - name: Install npm dependencies
      run: npm install
    - name: Run build task
      run: npm run build --if-present
    - name: Deploy to Server
      uses: easingthemes/ssh-deploy@main
      with:
          SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }}
          ARGS: "-rlgoDzvc -i --delete"
          SOURCE: "dist/"
          REMOTE_HOST: ${{ secrets.REMOTE_HOST }}
          REMOTE_USER: ${{ secrets.REMOTE_USER }}
          TARGET: ${{ secrets.REMOTE_TARGET }}
          EXCLUDE: "/dist/, /node_modules/"

Issues

This is a GitHub Action wrapping rsync via ssh. Only issues with action functionality can be fixed here.

Almost 95% of the issues are related to wrong SSH connection or rsync params and permissions. These issues are not related to the action itself.

  • Check manually your ssh connection from your client before opening a bug report.
  • Check rsync params for your use-case. Default params are not necessarily going to be enough for everyone, it highly depends on your setup.
  • Check manually your rsync command from your client before opening a bug report.
  • Deployment Failed, Permission denied (publickey,password): This issue occures in some cases, it is related to OS and ssh. This action can only provide a workaround:
    • Use SCRIPT_BEFORE param, eg SCRIPT_BEFORE: ls. This will force known_hosts update, adding your host via ssh-keyscan.
    • Or manually add public key to authorized_keys and add a new line to a private key.

I've added e2e test for this action. Real example is executed on every PR merge to main. Check actions tab for example.

When opening an issue, please add example of your step with env vars. You can add dummy values.

More info for SSH keys: https://www.ssh.com/ssh/public-key-authentication

Tips

  • Optional ENV variables are created for simple requirements. For complex use cases, use ARGS and SSH_CMD_ARGS to fully configure rsync with all possible options.
  • If you need to use multiple steps, eg multi targets deployment, save shared ENV variables in >> $GITHUB_ENV. Check .github/workflows/e2e.yml for an example
  • For multi sources, use -R ARG to manipulate folders structure.
  • Great post about rsync options specific to usage of this action: https://logansnotes.com/2020/gh-action-site-deploy/

Disclaimer

Check your keys. Check your deployment paths. And use at your own risk.