2020-01-18 19:20:37 +08:00
# Install SSH Key
2019-09-18 19:39:54 +08:00
2019-09-19 06:10:37 +08:00
[![Build][image-build]][link-build]
2020-06-06 10:05:25 +08:00
[![Windows Server 2019][image-verify-windows-2019]][link-verify-windows-2019]
[![macOS Catalina][image-verify-macos-1015]][link-verify-macos-1015]
[![Ubuntu 20.04][image-verify-ubuntu-2004]][link-verify-ubuntu-2004]
[![Ubuntu 18.04][image-verify-ubuntu-1804]][link-verify-ubuntu-1804]
[![Ubuntu 16.04][image-verify-ubuntu-1604]][link-verify-ubuntu-1604]
2021-02-08 22:31:19 +08:00
[![Docker container][image-verify-docker-container]][link-verify-docker-container]
2019-09-19 06:05:03 +08:00
[![Release][image-release]][link-release]
[![License][image-license]][link-license]
2020-01-18 10:23:25 +08:00
[![Stars][image-stars]][link-stars]
2019-09-19 06:05:03 +08:00
2020-01-18 20:25:09 +08:00
This action installs SSH key in `~/.ssh` .
2019-09-18 19:39:54 +08:00
2019-09-22 14:30:47 +08:00
Useful for SCP, SFTP, and `rsync` over SSH in deployment script.
2019-09-18 19:39:54 +08:00
2020-06-06 10:05:25 +08:00
**Works on all [virtual environments ](https://help.github.com/en/actions/automating-your-workflow-with-github-actions/virtual-environments-for-github-hosted-runners#supported-runners-and-hardware-resources ) --**
**Windows Server 2019, macOS Catalina, Ubuntu 20.04, Ubuntu 18.04, and Ubuntu 16.04.**
2019-12-31 12:23:55 +08:00
2019-09-18 19:39:54 +08:00
## Usage
Add your SSH key to your product secrets by clicking `Settings` - `Secrets` - `Add a new secret` beforehand.
2021-02-23 09:22:30 +08:00
PEM(RSA), PKCS8, and RFC4716(OpenSSH) formats are OK.
2020-01-22 22:50:32 +08:00
2019-09-18 19:39:54 +08:00
```yaml
runs-on: ubuntu-latest
steps:
- name: Install SSH key
2020-02-08 17:58:07 +08:00
uses: shimataro/ssh-key-action@v2
2019-09-18 19:39:54 +08:00
with:
2020-02-08 17:58:07 +08:00
key: ${{ secrets.SSH_KEY }}
2019-09-18 21:55:24 +08:00
name: id_rsa # optional
2020-02-08 17:58:07 +08:00
known_hosts: ${{ secrets.KNOWN_HOSTS }}
2019-12-22 18:01:05 +08:00
config: ${{ secrets.CONFIG }} # ssh_config; optional
2019-09-18 19:39:54 +08:00
- name: rsync over ssh
2019-09-29 12:18:35 +08:00
run: rsync ./foo/ user@remote:bar/
2019-09-18 19:39:54 +08:00
```
See [Workflow syntax for GitHub Actions ](https://help.github.com/en/articles/workflow-syntax-for-github-actions ) for details.
2019-12-30 07:24:22 +08:00
### Install multiple keys
If you want to install multiple keys, call this action multiple times.
It is useful for port forwarding.
2020-02-08 17:58:07 +08:00
**NOTE:** When this action is called multiple times, **the contents of `known_hosts` and `config` will be appended** . `key` must be saved as different name, by using `name` option.
2019-12-30 07:24:22 +08:00
```yaml
runs-on: ubuntu-latest
steps:
- name: Install SSH key of bastion
2020-02-08 17:58:07 +08:00
uses: shimataro/ssh-key-action@v2
2019-12-30 07:24:22 +08:00
with:
2020-02-08 17:58:07 +08:00
key: ${{ secrets.SSH_KEY_OF_BASTION }}
2019-12-30 07:24:22 +08:00
name: id_rsa-bastion
2020-02-08 17:58:07 +08:00
known_hosts: ${{ secrets.KNOWN_HOSTS_OF_BASTION }}
2019-12-30 07:24:22 +08:00
config: |
Host bastion
HostName xxx.xxx.xxx.xxx
User user-of-bastion
IdentityFile ~/.ssh/id_rsa-bastion
- name: Install SSH key of target
2020-02-08 17:58:07 +08:00
uses: shimataro/ssh-key-action@v2
2019-12-30 07:24:22 +08:00
with:
2020-02-08 17:58:07 +08:00
key: ${{ secrets.SSH_KEY_OF_TARGET }}
2019-12-30 07:24:22 +08:00
name: id_rsa-target
2021-02-08 20:40:22 +08:00
known_hosts: ${{ secrets.KNOWN_HOSTS_OF_TARGET }} # will be appended to existing .ssh/known_hosts
config: | # will be appended to existing .ssh/config
2019-12-30 07:24:22 +08:00
Host target
HostName yyy.yyy.yyy.yyy
User user-of-target
IdentityFile ~/.ssh/id_rsa-target
ProxyCommand ssh -W %h:%p bastion
- name: SCP via port-forwarding
run: scp ./foo/ target:bar/
```
2020-01-26 23:07:39 +08:00
## Q&A
### SSH failed even though key has been installed.
2020-06-06 08:33:26 +08:00
Check below:
2020-01-26 23:07:39 +08:00
* `Host key verification failed.` :
2020-06-17 22:12:44 +08:00
* Set `known_hosts` parameter correctly (use `ssh-keyscan` command).
2020-01-26 23:07:39 +08:00
### How do I use encrypted SSH key?
This action doesn't support encrypted key directly.
Here are some solutions:
* decrypting key beforehand: best bet, and works on any VM
* `sshpass` command: next best bet, but not supported on Windows
* `expect` command: be careful not to expose passphrase to console
* `SSH_ASKPASS` environment variable: might be troublesome
### Which one is the best way for transferring files, "direct SCP/SFTP/rsync" or "SCP/SFTP/rsync via bastion"?
I recommend **rsync via bastion** .
2020-02-10 19:05:55 +08:00
```bash
rsync -e "ssh bastion ssh" ./foo/ target:bar/
```
2020-01-26 23:07:39 +08:00
It has some advantages over other methods:
* "Rsync via bastion" doesn't require to update workflow files and `secrets` even if it is necessary to transfer files to multiple servers.
2020-02-08 17:58:07 +08:00
* Other methods require to update `known_hosts` if servers have changed.
2020-01-26 23:07:39 +08:00
* Rsync:
* is fastest of all.
* does **NOT** break files even if disconnected during transferring.
* can remove files that don't exist on server.
* SCP is [deprecated by OpenSSH ](https://www.openssh.com/txt/release-8.0 ) due to outdated and inflexible protocol.
* Using bastion is more secure because:
* it is not necessarily to expose SSH port on servers to public.
* Address filtering is less effective.
* Because Azure address range is [very wide ](https://help.github.com/en/actions/automating-your-workflow-with-github-actions/virtual-environments-for-github-hosted-runners#ip-addresses-of-github-hosted-runners ).
* And will be updated continuously.
* if security incident ―e.g., private key leaked― occurs, it's OK just to remove `authorized_keys` on bastion.
2019-09-18 19:39:54 +08:00
## License
The scripts and documentation in this project are released under the [MIT License ](LICENSE )
2019-09-19 06:05:03 +08:00
## Changelog
See [CHANGELOG.md ](CHANGELOG.md ).
2020-02-08 17:58:07 +08:00
[image-build]: https://github.com/shimataro/ssh-key-action/workflows/Build/badge.svg?event=push& branch=v2
2020-02-07 04:54:57 +08:00
[link-build]: https://github.com/shimataro/ssh-key-action/actions?query=workflow%3ABuild
2020-06-06 10:05:25 +08:00
[image-verify-windows-2019]: https://github.com/shimataro/ssh-key-action/workflows/Windows%20Server%202019/badge.svg?event=push& branch=v2
[link-verify-windows-2019]: https://github.com/shimataro/ssh-key-action/actions?query=workflow%3A%22Windows+Server+2019%22
[image-verify-macos-1015]: https://github.com/shimataro/ssh-key-action/workflows/macOS%20Catalina/badge.svg?event=push& branch=v2
[link-verify-macos-1015]: https://github.com/shimataro/ssh-key-action/actions?query=workflow%3A%22macOS+Catalina%22
[image-verify-ubuntu-2004]: https://github.com/shimataro/ssh-key-action/workflows/Ubuntu%2020.04/badge.svg?event=push& branch=v2
[link-verify-ubuntu-2004]: https://github.com/shimataro/ssh-key-action/actions?query=workflow%3A%22Ubuntu+20.04%22
[image-verify-ubuntu-1804]: https://github.com/shimataro/ssh-key-action/workflows/Ubuntu%2018.04/badge.svg?event=push& branch=v2
[link-verify-ubuntu-1804]: https://github.com/shimataro/ssh-key-action/actions?query=workflow%3A%22Ubuntu+18.04%22
[image-verify-ubuntu-1604]: https://github.com/shimataro/ssh-key-action/workflows/Ubuntu%2016.04/badge.svg?event=push& branch=v2
[link-verify-ubuntu-1604]: https://github.com/shimataro/ssh-key-action/actions?query=workflow%3A%22Ubuntu+16.04%22
2021-02-23 09:38:18 +08:00
[image-verify-docker-container]: https://github.com/shimataro/ssh-key-action/workflows/Docker%20container/badge.svg?event=push& branch=v2
[link-verify-docker-container]: https://github.com/shimataro/ssh-key-action/actions?query=workflow%3A%22Docker+container%22
2019-09-19 06:05:03 +08:00
[image-release]: https://img.shields.io/github/release/shimataro/ssh-key-action.svg
[link-release]: https://github.com/shimataro/ssh-key-action/releases
[image-license]: https://img.shields.io/github/license/shimataro/ssh-key-action.svg
[link-license]: ./LICENSE
2020-01-18 10:23:25 +08:00
[image-stars]: https://img.shields.io/github/stars/shimataro/ssh-key-action.svg
[link-stars]: https://github.com/shimataro/ssh-key-action/stargazers