This Rust project automates the generation of SSH keys for GitHub Actions or any other SSH authentication system. It generates an RSA SSH key pair, appends the public key to the authorized_keys file for authentication, and prints the private key so it can be added to GitHub Secrets.
This would be relevant for you if you are setting up a new server or you are migrating. I find myself repeating the same process multiple times for my projects, that's why I created a script for it.
Goodluck!
- Allows the user to specify a custom SSH key name (defaults to
github-actions). - Automatically creates the
~/.sshdirectory if it doesn't exist. - Uses
ssh-keygento generate a 4096-bit RSA key pair. - Appends the public key to the
authorized_keysfile, allowing SSH access. - Outputs the private key so it can be added to GitHub repository secrets.
- Rust installed on your machine. If you don't have Rust installed, you can get it here.
- OpenSSH installed (for
ssh-keygen). On Linux or macOS, it's typically pre-installed. On Windows, you may need to install it via optional features or through WSL.
- Clone this repository:
git clone https://github.com/UseBatimus/ssh-action-rsync- Navigate to the project directory:
```bash
cd ssh-action-rsync- Run the project:
cargo run- Run the program:
When you run the program, you'll be prompted to enter the name you want to use for your SSH key. If you leave it blank, the default will be
github-actions.
Enter the name you want to use for the SSH key (default: github-actions):-
SSH Key Generation: The program generates a 4096-bit RSA key pair, with the private key stored in
~/.ssh/{key_name}and the public key in~/.ssh/{key_name}.pub. -
Adding to GitHub Secrets: The private key will be displayed in the terminal. You can copy this private key and add it to your GitHub repository secrets (Settings > Secrets and variables > Actions > New repository secret).
-
SSH Authentication: The public key is automatically added to
~/.ssh/authorized_keys, allowing you to use the private key for SSH authentication.
$ cargo run --release
Enter the name you want to use for the SSH key (default: github-actions):
my-custom-key
SSH key generated successfully.
Public key added to authorized_keys.
Private key to add to GitHub Secrets:
-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEA4...
...
-----END RSA PRIVATE KEY-----Here is an example usage in GitHub action
name: Deployment Action
on:
push:
branches: [master]
workflow_dispatch:
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [16.x]
steps:
- uses: actions/checkout@v3
- name: Node LTS versions
uses: msimerson/node-lts-versions@v1.1.1
- name: Use Node.js 16
uses: actions/setup-node@v3
- name: Install all packages
run: npm install
env:
NPM_AUTH_TOKEN: ${{ secrets.NPM_AUTH_TOKEN }}
- name: Build
run: npm run build
- name: rsync deployments
uses: burnett01/rsync-deployments@5.1
with:
switches: -avzr --delete
path: ./*
remote_path: "path/on/your/server/where/you/want/the/code/to/be/deployed/to"
remote_host: "${{secrets.SERVER_IP_ADDRESS}}"
remote_user: root
remote_key: "${{ secrets.SSH_PRIVATE_KEY_STAGING }}"This project is licensed under the MIT License
Contributions are welcome! Please feel free to submit a pull request or open an issue if you have any suggestions or improvements.