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 asroot, any user allowed to rundocker composeis effectivelyrooton the host (docker.sock). - Port 22 stays open to shifting cloud IPs. Hosted
ubuntu-latestrunners 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:
deploy.shpulls configs and compose files on a timer using a read-only Deploy key oninfra-web1(Deploy without SSH);- GitHub Actions builds and publishes your images to GHCR, and the server pulls them with a token scoped solely to
read:packages(GitHub Actions to GHCR, Build in CI, not on the server).
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_keysto a single script withrestrict,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
mainbranch; - 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.timerrunsdeploy.shevery 10 minutes (OnCalendar=*:0/10).- Fix
- For a one-off urgent deploy, run
sudo systemctl start deploy.servicefrom your workstation. If you always want faster deploys, shorten the interval indeploy.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_keysif it was there) and update the/opt/host/authorized_keyscopy 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