rsync-deployments GitHub Action

repository·master·Indexed 19 days ago

https://github.com/burnett01/rsync-deployments

A cross-platform GitHub Action for automating file deployments to remote servers using rsync over SSH. Optimized with a minimal Alpine Linux base image, it supports custom rsync switches, SSH private key authentication, and legacy RSA hostkey support for OpenSSH 8.8+. The current recommended LTS version is v9.

Tokens
2.2K
Snippets
7
Records
11
Agent score
16%

What's inside rsync-deployments

  1. Manage known_hosts with hosts-* commands

    master

    These commands manage the $HOME/.ssh/known_hosts file to ensure correct permissions and entry management.

    Initialize known_hosts

    hosts-init creates the $HOME/.ssh/known_hosts file with default permissions of 600.

    Add a host key

    hosts-add [key] adds a new entry to the known_hosts file and ensures the file permissions remain correct.

    • Argument: The new key string to add.

    Clear known_hosts

    hosts-clear truncates the known_hosts file, effectively removing all known host entries.

    hosts-init
    hosts-add "ssh-rsa AAAAB3Nza..."
    hosts-clear
  2. Use rsync-deployments GitHub Action

    master

    This GitHub Action deploys files from a source path (relative to GITHUB_WORKSPACE) to a remote folder via rsync over ssh. It is designed for use in CD workflows after a checkout step (like actions/checkout).

    The action uses a lightweight Alpine-based image for fast deployments.

    Current Version: v9 (LTS recommended).

    Release Channels:

    • v9: Latest MAJOR (pointer to 9.MINOR.PATCH) - Recommended/LTS
    • 9.0.0: Latest MAJOR+MINOR+PATCH - Immutable
    • v8: Previous MAJOR (ESU)
    • 8.0.5: Previous MAJOR+MINOR+PATCH - Immutable
    name: DEPLOY
    on: push
      branches: [master]
    jobs:
      deploy:
        runs-on: ubuntu-latest
        steps:
        - uses: actions/checkout@v7
        - name: rsync deployments
          uses: burnett01/rsync-deployments@v9
          with:
            switches: -avzr --delete
            path: src/
            remote_path: ${{ secrets.REMOTE_PATH }}
            remote_host: ${{ secrets.REMOTE_HOST }}
            remote_port: ${{ secrets.REMOTE_PORT }}
            remote_user: ${{ secrets.REMOTE_USER }}
            remote_key: ${{ secrets.REMOTE_PRIVATE_KEY }}
  3. Upgrade from EOL versions

    master

    If you are using older versions of rsync-deployments, it is recommended to upgrade to the latest version (v9) for better performance and security.

    • Version 3.0 (EOL): Uses alpine:latest directly. Upgrade to 4.0+ to use the optimized drinternet/rsync:1.0.1 image.
    • Version 2.0 (EOL): Uses ubuntu:latest. Upgrade to 3.0+ for faster deployments.
    • Version 1.0 (EOL): Has reached end-of-life state.
  4. Manage SSH agents with agent-* commands

    master

    The following commands manage the lifecycle of SSH agents. Note that agent-start must be sourced to correctly update your current shell environment variables.

    Start an agent

    agent-start [name] starts the SSH agent if it isn't already running. It checks for an existing SSH_AGENT_PID or an agent ID file.

    • Argument: An optional name for the agent. Defaults to "default".
    • Requirement: You must use source to apply environment changes.

    Stop an agent

    agent-stop [name] stops the SSH agent if it is currently active.

    • Argument: An optional name for the agent. Defaults to "default".

    Add a key to the agent

    agent-add adds a key to the currently running SSH agent (identified by SSH_AGENT_PID). The key content must be provided via stdin.

    Handle passphrases with agent-askpass

    agent-askpass is designed to be used when the SSH_ASKPASS environment variable is set. It returns the SSH_PASS to the ssh-askpass(1) utility. This command is ignored by ssh-add if the key does not require a passphrase.

    # Start the default agent
    source agent-start "default"
    
    # Stop a specific agent
    agent-stop "my-agent-name"
    
    # Add a key from a file via stdin
    cat ~/.ssh/id_ed25519 | agent-add
  5. Enable legacy RSA hostkey support for OpenSSH 8.8+

    master

    If your remote OpenSSH server is version 8.8 or newer and still uses legacy RSA hostkeys, you must explicitly enable support by setting legacy_allow_rsa_hostkeys: "true" in your workflow configuration.

    - name: rsync deployments
      uses: burnett01/rsync-deployments@v9
      with:
        switches: -avzr --delete
        legacy_allow_rsa_hostkeys: "true"
        path: src/
        remote_path: ${{ secrets.REMOTE_PATH }}
        remote_host: ${{ secrets.REMOTE_HOST }}
        remote_port: ${{ secrets.REMOTE_PORT }}
        remote_user: ${{ secrets.REMOTE_USER }}
        remote_key: ${{ secrets.REMOTE_PRIVATE_KEY }}
  6. Set up SSH keys for deployment

    master

    To use this action, you must provide an SSH private key via the remote_key input.

    1. Generate a key pair: Use Ed25519 or RSA 4096-bit.
    2. Server Setup: Add the public key (.pub) to the server's ~/.ssh/authorized_keys file. Ensure permissions are correct:
      chmod 700 ~/.ssh
      chmod 600 ~/.ssh/authorized_keys
    3. GitHub Setup: Add the private key (the file without the .pub extension) to your GitHub Repository/Organization Secrets as a new secret (e.g., REMOTE_PRIVATE_KEY).
    4. Reference in Workflow: Use ${{ secrets.REMOTE_PRIVATE_KEY }} in your workflow file.
    # Generate a new SSH key pair (recommended: Ed25519 or RSA 4096-bit)
    ssh-keygen -t ed25519 -C "deploy@yourproject" -f ~/.ssh/deploy_yourproject -N ""
    # OR for RSA:
    ssh-keygen -t rsa -b 4096 -C "deploy@yourproject" -f ~/.ssh/deploy_yourproject -N ""
  7. Troubleshoot missing rsync on remote host

    master

    If the action fails with rsync: command not found, you must install rsync on your remote server using its package manager:

    Ubuntu/Debian:

    sudo apt-get update && sudo apt-get install rsync

    CentOS/RHEL/Rocky/AlmaLinux:

    sudo yum install rsync
    # OR
    sudo dnf install rsync

    Alpine Linux:

    sudo apk add rsync
  8. Troubleshoot SSH Permission Denied errors

    master

    If you see Permission denied (publickey,password), check the following:

    1. SSH Key Configuration:
      • Ensure the public key is in the server's ~/.ssh/authorized_keys.
      • Ensure the private key is correctly stored in GitHub Secrets and referenced as remote_key.
      • Verify server-side permissions: chmod 700 ~/.ssh and chmod 600 ~/.ssh/authorized_keys.
    2. Remote Path Permissions: Ensure the remote_user has write access to the remote_path on the target server.
    3. Firewalls: If the server restricts IP ranges, you may need to whitelist GitHub Actions IP ranges or use a self-hosted runner.
  9. Exclude files and folders from deployment

    master

    By default, rsync copies all files and dotfiles at the specified path. To exclude specific directories or files (like .git or node_modules), use the --exclude flag within the switches input.

    switches: -avzr --delete --exclude='.git/'
    # Or multiple exclusions:
    switches: -avzr --delete --exclude='.git/' --exclude='node_modules/' --exclude='.env'
  10. Configure rsync-deployments inputs

    master

    The following inputs are available for the burnett01/rsync-deployments action. Note that * indicates a required field.

    InputDescription
    debug*Enable debug output ("true" / "false"). Default: "false"
    switches*Initial/required rsync flags (e.g., -avzr --delete)
    rshRemote shell commands
    strict_hostkeys_checkingEnable strict hostkeys (fingerprint) checking ("true" / "false"). Default: "false"
    legacy_allow_rsa_hostkeysEnable support for legacy RSA host keys on OpenSSH 8.8+ ("true" / "false"). Default: "false"
    pathSource path relative to GITHUB_WORKSPACE.
    remote_path*The deployment target path
    remote_host*The remote host
    remote_portThe remote port. Default: 22
    remote_user*The remote user
    remote_key*The remote SSH private key
    remote_key_passThe remote SSH private key passphrase (if any)