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]
2021-03-18 19:57:28 +08:00
[![Windows][image-verify-windows]][link-verify-windows]
[![macOS][image-verify-macos]][link-verify-macos]
[![Ubuntu][image-verify-ubuntu]][link-verify-ubuntu]
2021-02-24 20:24:03 +08:00
[![Docker container (Ubuntu)][image-verify-docker-container-ubuntu]][link-verify-docker-container-ubuntu]
[![Docker container (CentOS)][image-verify-docker-container-centos]][link-verify-docker-container-centos]
2021-03-21 13:56:48 +08:00
[![Docker container (Alpine Linux)][image-verify-docker-container-alpine]][link-verify-docker-container-alpine]
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
2021-02-24 20:24:03 +08:00
tested on:
2021-02-23 10:27:02 +08:00
2021-08-01 21:31:32 +08:00
* [all available virtual machines ](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/2016, macOS Catalina, and Ubuntu 20.04/18.04)
2021-03-21 13:56:48 +08:00
* [Docker container (Ubuntu) ](https://hub.docker.com/_/ubuntu ) / requires `openssh-client` package; `apt install -y openssh-client`
* [Docker container (CentOS) ](https://hub.docker.com/_/centos ) / requires `openssh-clients` package; `yum install -y openssh-clients`
* [Docker container (Alpine Linux) ](https://hub.docker.com/_/alpine ) / requires `openssh-client` package; `apk add openssh-client`
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
2021-03-18 19:57:28 +08:00
if_key_exists: fail # replace / ignore / fail; optional (defaults to fail)
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.
2021-03-21 14:43:28 +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
2021-03-18 19:57:28 +08:00
### I want to replace/ignore key if exists.
Use `if_key_exists` parameter.
* `replace` : replaces key
* `ignore` : does nothing
* `fail` : fails (default)
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.
2021-03-21 14:43:28 +08:00
### I want to omit `known_hosts`.
First of all, you have to understand that it is NOT secure to SSH with no `known_hosts` and using `StrictHostKeyChecking=no` option.
Why do you want to omit it?
If the reason is ** "I'm not understanding about the function of `known_hosts` "** or ** "It's bother to fetch server key"**, you should not omit.
If ** "It is hard to prefetch server key because the server will be created dynamically"**, you can use bastion server.
**"`known_hosts` is unnecessary because I'm using secure method for SSH, such as SSHFP and signed server key."** — OK, here is a special value to omit `known_hosts` .
You should use it ONLY IF you are using secure methods...
It is `known_hosts: unnecessary` .
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
2021-02-24 20:24:03 +08:00
[link-build]: https://github.com/shimataro/ssh-key-action/actions/workflows/build.yml
2021-03-18 19:57:28 +08:00
[image-verify-windows]: https://github.com/shimataro/ssh-key-action/workflows/Windows/badge.svg?event=push& branch=v2
[link-verify-windows]: https://github.com/shimataro/ssh-key-action/actions/workflows/verify-on-windows.yml
[image-verify-macos]: https://github.com/shimataro/ssh-key-action/workflows/macOS/badge.svg?event=push& branch=v2
[link-verify-macos]: https://github.com/shimataro/ssh-key-action/actions/workflows/verify-on-macos.yml
[image-verify-ubuntu]: https://github.com/shimataro/ssh-key-action/workflows/Ubuntu/badge.svg?event=push& branch=v2
[link-verify-ubuntu]: https://github.com/shimataro/ssh-key-action/actions/workflows/verify-on-ubuntu.yml
2021-02-24 20:24:03 +08:00
[image-verify-docker-container-ubuntu]: https://github.com/shimataro/ssh-key-action/actions/workflows/verify-on-container-ubuntu.yml/badge.svg?event=push& branch=v2
[link-verify-docker-container-ubuntu]: https://github.com/shimataro/ssh-key-action/actions/workflows/verify-on-container-ubuntu.yml
[image-verify-docker-container-centos]: https://github.com/shimataro/ssh-key-action/actions/workflows/verify-on-container-centos.yml/badge.svg?event=push& branch=v2
[link-verify-docker-container-centos]: https://github.com/shimataro/ssh-key-action/actions/workflows/verify-on-container-centos.yml
2021-03-21 13:56:48 +08:00
[image-verify-docker-container-alpine]: https://github.com/shimataro/ssh-key-action/actions/workflows/verify-on-container-alpine.yml/badge.svg?event=push& branch=v2
[link-verify-docker-container-alpine]: https://github.com/shimataro/ssh-key-action/actions/workflows/verify-on-container-alpine.yml
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