Topics & Navigation

Tutorials

GitHub Actions Deploy to VPS: Secure SSH Pipeline Step by Step

Table of contents (17 sections)

In short: a safe GitHub Actions deployment to a VPS has four parts: a workflow that runs your tests first, a dedicated SSH key stored as a secret, a pinned server host key so nobody can impersonate your server, and a small deploy script on the server that the key is allowed to run, and nothing else.

Deploying by hand works until the day you forget a step at midnight. A pipeline makes every release identical: push to main, tests run, the server updates, and you get a clear log. The risk is that you give a cloud runner the keys to your server. This guide shows how to do it with minimal permissions, using plain OpenSSH instead of a third-party action, so there is less to trust. It pairs with our guide to Docker Compose in production.

Prerequisites

  • A GitHub repository with a test command (for example npm test)
  • A Linux VPS with Docker and Docker Compose already running your application, as in our PocketBase production setup
  • A non-root user on the server for deployments, for example deploy, with access to the application folder
  • SSH access to the server from your own computer to set things up

How the pipeline works

  1. You push to main.
  2. A test job checks out the code and runs your tests.
  3. If tests pass, a deploy job connects to the VPS over SSH and runs one script.
  4. The script pulls the new image and restarts the services with Docker Compose.

If tests fail, nothing is deployed.

Step 1: create a dedicated deploy key

Generate a key pair used only for this pipeline, with no passphrase because the runner cannot type one:

bashssh-keygen -t ed25519 -f ./deploy_key -C "github-actions-deploy" -N ""

You get a private key (deploy_key) and a public key (deploy_key.pub). Never reuse your personal key.

Step 2: write the deploy script on the server

Create /opt/app/deploy.sh and make it executable. It does only what a deployment needs:

bash#!/usr/bin/env bash
set -euo pipefail
 
cd /opt/app
docker compose pull
docker compose up -d --remove-orphans
docker image prune -f

Keep this script short and owned by root or the deploy user, with write access limited to that account.

Step 3: restrict the key to that script

Add the public key to /home/deploy/.ssh/authorized_keys with options that limit what it can do. Even if the private key leaks, it can only run the deploy script:

textcommand="/opt/app/deploy.sh",no-port-forwarding,no-agent-forwarding,no-X11-forwarding,no-pty ssh-ed25519 AAAA...your-public-key... github-actions-deploy

With the command= option, the server runs the script whatever the client asks for. Check permissions: ~/.ssh should be 700 and authorized_keys should be 600.

Step 4: pin the server host key

If the runner accepts any host key, an attacker who intercepts the connection can impersonate your server. Pin the key instead. On your computer, print the server’s host key and copy the output:

bashssh-keyscan -t ed25519 your-server.example.com

Compare the fingerprint with the one shown on the server (ssh-keygen -lf /etc/ssh/ssh_host_ed25519_key.pub) to be sure it is genuine.

Step 5: add secrets in GitHub

In your repository, open Settings > Secrets and variables > Actions and create:

  • SSH_PRIVATE_KEY: the full content of deploy_key
  • SSH_KNOWN_HOSTS: the line you copied from ssh-keyscan
  • SSH_HOST: your server’s hostname
  • SSH_USER: deploy

For extra control, create an environment named production and require a manual approval for deployments if you want a human check.

Step 6: write the workflow

Create .github/workflows/deploy.yml:

yamlname: Test and deploy
 
on:
  push:
    branches: [main]
 
permissions:
  contents: read
 
concurrency:
  group: deploy-production
  cancel-in-progress: false
 
jobs:
  test:
    runs-on: ubuntu-latest
    timeout-minutes: 10
    steps:
      - uses: actions/checkout@v6
      - uses: actions/setup-node@v5
        with:
          node-version: 22
          cache: npm
      - run: npm ci
      - run: npm test
 
  deploy:
    needs: test
    runs-on: ubuntu-latest
    timeout-minutes: 10
    environment: production
    steps:
      - name: Prepare SSH
        env:
          SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }}
          SSH_KNOWN_HOSTS: ${{ secrets.SSH_KNOWN_HOSTS }}
        run: |
          install -m 700 -d ~/.ssh
          printf '%s\n' "$SSH_PRIVATE_KEY" > ~/.ssh/id_ed25519
          chmod 600 ~/.ssh/id_ed25519
          printf '%s\n' "$SSH_KNOWN_HOSTS" > ~/.ssh/known_hosts
      - name: Deploy
        env:
          SSH_USER: ${{ secrets.SSH_USER }}
          SSH_HOST: ${{ secrets.SSH_HOST }}
        run: ssh -o StrictHostKeyChecking=yes "$SSH_USER@$SSH_HOST" true

The final true is only a placeholder argument: because the key is restricted with command=, the server runs deploy.sh regardless. Secrets are passed through env instead of being pasted into the script text, which is safer. Adapt the Node.js lines to your language. At the time of writing, the checkout action is at major version v6; check the repositories of both actions for newer releases before you copy the file.

Step 7: keep the pipeline itself secure

  • Limit permissions. The permissions: contents read line gives the token the minimum access. Add more only when a step needs it.
  • Pin actions to a commit SHA. A version tag can be moved, so a compromised action could run malicious code with access to your secrets. Pin to the full commit hash and keep the readable version in a comment.
  • Enable Dependabot for GitHub Actions so updates arrive as reviewable pull requests. This matters in practice: GitHub moved JavaScript actions from Node.js 20 to Node.js 24 by default in 2026, and old versions had to be updated.
  • Never print secrets. GitHub masks known secrets in logs, but do not echo them on purpose.
  • Do not run deployments from pull requests of forks, since they should not access secrets.

Rollbacks

Tag each image with a version or commit hash and keep the previous tag. To roll back, change the tag in your compose file (or an environment variable) back to the previous version and run the deploy script again. If a release includes a database migration that is not reversible, take a backup first, as described in our PocketBase backup guide.

Troubleshooting

  • Permission denied (publickey): the public key is missing from authorized_keys, the file permissions are wrong, or the secret contains extra whitespace or a truncated key.
  • Host key verification failed: the SSH_KNOWN_HOSTS secret does not match the server. Regenerate it with ssh-keyscan and compare fingerprints.
  • docker: permission denied in the deploy script: add the deploy user to the docker group, knowing that this grants high privileges, so use it only for this dedicated account.
  • Two deployments overlap: keep the concurrency block so runs queue instead of colliding.
  • The workflow does not start: check the branch name in on.push.branches and that Actions are enabled for the repository.

FAQ

Can I use a third-party SSH action instead?

Yes, several are popular, but each one is code that runs with your secrets. If you use one, pin it to a commit SHA and read what it does. Plain ssh as above has fewer moving parts.

Should the server build the image or the runner?

Prefer building in CI, pushing to a registry, and pulling on the server. It keeps the server lean and makes every release traceable to a commit.

Is a password-less key safe?

It is safe enough when the key is dedicated, restricted by command=, stored only as a GitHub secret, and rotated if you suspect a leak.

How do I deploy to several servers?

Use a matrix or several deploy jobs, with one key and one known host entry per server, and keep each restricted in the same way.

Next steps

Harden the application side with Docker Compose in production, and review what an assistant writes before it ships with our checklist for reviewing AI-generated code. The official GitHub Actions documentation covers environments, secrets and security hardening in depth.