ssh-deploy/README.md

174 lines
5.7 KiB
Markdown
Raw Normal View History

2019-09-25 21:24:25 +00:00
# ssh deployments
2019-02-09 13:17:45 +00:00
Deploy code with rsync over ssh.
Execute remote scripts before or after rsync
2019-10-02 22:51:32 +00:00
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.
2019-02-09 13:17:45 +00:00
2019-09-25 21:24:25 +00:00
This action would usually follow a build/test action which leaves deployable code in `GITHUB_WORKSPACE`, eg `dist`;
2019-02-09 13:17:45 +00:00
In addition to rsync, this action provides scripts execution on remote host before and/or after rsync.
2019-09-25 21:52:29 +00:00
# Configuration
2019-02-09 13:17:45 +00:00
2019-09-25 21:52:29 +00:00
Pass configuration with `env` vars
2019-02-09 13:17:45 +00:00
##### 1. `SSH_PRIVATE_KEY` [required]
2019-10-02 22:51:32 +00:00
Private key part of an SSH key pair.
2020-04-11 21:08:36 +00:00
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
2020-06-06 20:36:28 +00:00
The keys should be generated using the PEM format. You can use this command
2020-04-11 14:33:22 +00:00
```
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.
2019-02-09 13:17:45 +00:00
##### 2. `REMOTE_HOST` [required]
2019-10-02 22:51:32 +00:00
eg: mydomain.com
##### 3. `REMOTE_USER` [required]
2019-10-02 22:17:19 +00:00
2019-10-02 22:51:32 +00:00
eg: myusername
##### 4. `REMOTE_PORT` (optional, default '22')
eg: '59184'
##### 5. `ARGS` (optional, default '-rlgoDzvc -i')
2019-10-02 22:51:32 +00:00
2019-09-25 21:52:29 +00:00
For any initial/required rsync flags, eg: `-avzr --delete`
2019-02-09 13:17:45 +00:00
##### 6. `SOURCE` (optional, default '')
2019-10-02 22:51:32 +00:00
The source directory, path relative to `$GITHUB_WORKSPACE` root, eg: `dist/`.
Multiple sources should be separated by space.
2019-02-09 13:17:45 +00:00
##### 7. `TARGET` (optional, default '/home/REMOTE_USER/')
2019-10-02 22:51:32 +00:00
2019-10-02 22:17:19 +00:00
The target directory
2019-02-09 13:17:45 +00:00
2021-03-12 06:42:12 +00:00
##### 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`
2023-01-03 08:25:16 +00:00
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.
2023-01-02 20:31:04 +00:00
##### 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
2019-10-02 22:51:32 +00:00
# Usage
2019-09-25 21:24:25 +00:00
Use the latest version from Marketplace,eg: ssh-deploy@v2
or use the latest version from a branch, eg: ssh-deploy@main
2020-09-18 21:29:28 +00:00
2019-09-25 21:52:29 +00:00
```
2019-10-02 22:17:19 +00:00
- name: Deploy to Staging server
uses: easingthemes/ssh-deploy@main
with:
2022-10-28 02:16:02 +00:00
SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }}
ARGS: "-rlgoDzvc -i"
2019-10-02 22:17:19 +00:00
SOURCE: "dist/"
REMOTE_HOST: ${{ secrets.REMOTE_HOST }}
REMOTE_USER: ${{ secrets.REMOTE_USER }}
TARGET: ${{ secrets.REMOTE_TARGET }}
2021-03-12 06:42:12 +00:00
EXCLUDE: "/dist/, /node_modules/"
SCRIPT_BEFORE: |
whoami
ls -al
SCRIPT_AFTER: |
whoami
ls -al
echo $RSYNC_STDOUT
2019-09-25 21:52:29 +00:00
```
2019-02-09 13:17:45 +00:00
2019-10-02 22:17:19 +00:00
# Example usage in workflow
2019-02-09 13:17:45 +00:00
```
2019-09-25 21:52:29 +00:00
name: Node CI
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
2023-02-21 22:11:54 +00:00
- uses: actions/checkout@v3
2019-10-02 22:17:19 +00:00
- name: Install Node.js
2023-02-21 22:11:54 +00:00
uses: actions/setup-node@v3
2019-09-25 21:52:29 +00:00
with:
2023-02-21 22:11:54 +00:00
node-version: '16.x'
2019-09-25 21:52:29 +00:00
- name: Install npm dependencies
2019-10-02 22:17:19 +00:00
run: npm install
2019-09-25 21:52:29 +00:00
- name: Run build task
2019-10-02 22:17:19 +00:00
run: npm run build --if-present
- name: Deploy to Server
uses: easingthemes/ssh-deploy@main
with:
2022-10-28 02:16:02 +00:00
SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }}
ARGS: "-rlgoDzvc -i --delete"
2019-10-02 22:17:19 +00:00
SOURCE: "dist/"
REMOTE_HOST: ${{ secrets.REMOTE_HOST }}
REMOTE_USER: ${{ secrets.REMOTE_USER }}
TARGET: ${{ secrets.REMOTE_TARGET }}
2021-03-12 06:42:12 +00:00
EXCLUDE: "/dist/, /node_modules/"
2019-02-09 13:17:45 +00:00
```
2022-12-31 09:33:16 +00:00
## Issues
This is a GitHub Action wrapping `rsync` via `ssh`. Only issues with action functionality can be fixed here.
2022-12-31 14:20:15 +00:00
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.
2022-12-31 14:20:15 +00:00
- Check manually your ssh connection from your client before opening a bug report.
2023-06-28 19:36:01 +00:00
- Check `rsync` params for your use-case. Default params are not necessarily going to be enough for everyone, it highly depends on your setup.
2022-12-31 14:20:15 +00:00
- 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.
2022-12-31 14:20:15 +00:00
2022-12-31 09:33:16 +00:00
I've added e2e test for this action.
2022-12-31 14:20:15 +00:00
Real example is executed on every PR merge to `main`.
2022-12-31 09:33:16 +00:00
Check actions tab for example.
2023-02-21 22:11:54 +00:00
When opening an issue, please add example of your step with env vars. You can add dummy values.
2022-12-31 14:13:32 +00:00
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.
2023-01-03 08:25:16 +00:00
- Great post about `rsync` options specific to usage of this action: https://logansnotes.com/2020/gh-action-site-deploy/
2022-12-31 14:20:15 +00:00
2019-02-09 13:17:45 +00:00
## Disclaimer
2020-04-11 14:30:28 +00:00
Check your keys. Check your deployment paths. And use at your own risk.