basforge

Search titles, text and the “If it doesn’t work” cases.

    RU

    Why not SSH from GitHub Actions

    Instead of handing GitHub Actions a private SSH key to your server, keep the initiative on the server: let it pull commits from git and images from the registry itself. Inbound SSH from CI stores a login key on GitHub’s side, forces port 22 open to thousands of shifting runner IPs, and turns a compromised repository into server access.

    push (SSH from CI): GitHub Actions (SSH key in Secrets) ── SSH :22 ──▶ web1 (port open to the whole cloud)
    pull (no SSH):      GitHub (infra-web1 + GHCR) ◀── HTTPS / deploy key ── web1 (deploy.sh on a timer)

    What breaks with SSH from GitHub Actions

    • A server login key sits in GitHub Secrets. Anyone who can edit .github/workflows/ — or a compromised third-party action pinned by tag instead of commit SHA — can grab that key and run arbitrary commands on the server. Even if login isn’t as root, any user allowed to run docker compose is effectively root on the host (docker.sock).
    • Port 22 stays open to shifting cloud IPs. Hosted ubuntu-latest runners come from thousands of Azure subnets whose list keeps changing. You can’t lock port 22 down by IP in the firewall: you either leave it open to the whole internet or punch a hole through a firewall API before every run (adding another token and point of failure).
    • Builds depend on server uptime. If the server is rebooting or loses network for a minute, the entire CI run fails. Moving to a new VPS forces you to update the IP and host key fingerprint in every repository’s secrets.

    What to use instead: the pull model

    Keep the initiative on the server so it never accepts inbound connections from CI:

    A leaked repository secret or hijacked workflow yields no shell on the server: at worst, a bad commit lands on the branch, and deploy.sh rolls it back automatically when the stack fails its healthcheck.

    When SSH from CI is unavoidable

    If you need an instant button-triggered deploy from CI and a timer won’t do, contain the blast radius of the key:

    • issue a dedicated key per repository (Each key and token grants one access) and never reuse your personal key;
    • pin the key in authorized_keys to a single script with restrict,command="/opt/scripts/deploy.sh" (SSH keys): no shell, no port forwarding, and no command choice from CI;
    • pin every third-party action in the workflow to a full commit SHA, and put the key secret in a GitHub Environment restricted to the main branch;
    • connect over a short-lived tunnel (WireGuard or Tailscale for the duration of the job) rather than public port 22 whenever possible.

    If it doesn’t work

    In the pull model, a change takes up to 10 minutes to reach the server, and you need an immediate deploy

    Cause
    deploy.timer runs deploy.sh every 10 minutes (OnCalendar=*:0/10).
    Fix
    For a one-off urgent deploy, run sudo systemctl start deploy.service from your workstation. If you always want faster deploys, shorten the interval in deploy.timer (for example, *:0/2) instead of opening SSH to CI.

    Removed the SSH deploy step from the workflow, yet the old key is still in authorized_keys on the server

    Cause
    Deleting a secret in GitHub settings doesn’t revoke the key on the server.
    Fix
    Remove the CI key line from /home/example-user/.ssh/authorized_keys (and /root/.ssh/authorized_keys if it was there) and update the /opt/host/authorized_keys copy in git.

    Locked the key in authorized_keys with restrict,command="…", and a third-party SSH GitHub Action fails

    Cause
    With command=, sshd ignores whatever script the client sends over SSH and always runs the forced command; actions that stream commands into a session break.
    Fix
    Either switch to the pull model without SSH (Deploy without SSH), or run a bare ssh [email protected] in the workflow and keep all deploy logic in the script on the server.

    How to check

    Confirm no CI-issued keys remain in authorized_keys on the server:

    sudo grep -iE 'github|actions|ci|deploy' /home/example-user/.ssh/authorized_keys /root/.ssh/authorized_keys 2>/dev/null

    The output should be empty (or list only your personal keys).

    In each repository on GitHub (Settings, Secrets and variables, Actions), verify there are no private server SSH keys, and confirm the timer is pulling updates:

    systemctl list-timers deploy.timer