Manually copying files to a server over SSH works until you forget a step, deploy the wrong branch, or leave a half-updated site live for an hour. Learning to deploy to vps github actions properly removes that guesswork. Every push to your main branch can run the same tested steps: check out the code, copy only the changed files to the VPS, and restart the service in a controlled way. When a deploy fails, you get a log, a clear error, and a repeatable way to roll back.

This guide shows a safe pattern for a single VPS. You will create a dedicated SSH key pair, store it in GitHub Secrets instead of in the workflow file, deploy with rsync, restart the application with systemd or Docker Compose, and handle rollback. It also covers the SSH failures that waste the most time: host key verification, key format mistakes, and file permission errors.
Prerequisites
You need a GitHub repository, a VPS running Linux with SSH access, and a deploy user on that server. This deploy to vps github actions guide assumes you know how to run basic commands on the VPS, how to check a systemd service, and how to use git. You also need repository admin access on GitHub, because only admins can add repository secrets. If your application runs in Docker, you need Docker and Docker Compose installed on the VPS. If it runs directly on the server, you need systemd and your application runtime already installed.
Why You Must Never Store Passwords in the Workflow
A workflow file lives in your repository, which means anyone with read access can see it, and anyone with access to your git history can see every old version of it. Putting a server password, an API key, or a private SSH key directly in that file is the same as publishing it. Even a private repository is not safe enough, because access changes over time, forks can leak files, and logs can expose values that were printed by mistake.
GitHub Secrets solve this by storing values encrypted and exposing them only during a workflow run. In a deploy to vps github actions setup, secrets are masked in logs, they are not available to pull requests from forks by default, and you can rotate them without changing the workflow file. The rule is simple: the workflow file contains only the names of secrets, never the values. If a secret ever leaks, revoke it on the server immediately and generate a new one, rather than trying to hide the old value.
How deploy to vps github actions Works
The pattern has three parts. First, the GitHub runner checks out your code. Second, it connects to your VPS over SSH using a private key stored as a secret. Third, it runs a small set of remote commands to put the new code live and restart the service. The runner itself is temporary and is discarded after the job, so no credentials remain on it. Your VPS keeps running the application, and GitHub keeps only the secret names and the workflow definition.
This approach is different from running a permanent agent on the server. If you prefer that model, read about GitHub Actions self-hosted runners on Linux and weigh the trade-off: a self-hosted runner avoids SSH setup, but you must patch, secure, and monitor a machine that can run untrusted workflow code. For a single VPS with one application, SSH deployment from a GitHub-hosted runner is simpler and easier to audit.
Creating the SSH Key Pair and GitHub Secrets
Create a dedicated key pair for deployment only. Do not reuse your personal SSH key, because revoking a shared key locks you out as well as the deployment. On your own machine, generate an Ed25519 key without a passphrase for unattended use, or use a passphrase and store it as an additional secret if your workflow tooling supports it. For most single-VPS setups, a passphrase-free deploy key with a restricted server user is the practical choice.
ssh-keygen -t ed25519 -C "github-actions-deploy" -f ~/.ssh/deploy_key
cat ~/.ssh/deploy_key.pub
Add the public key to the deploy user on the VPS, and set strict permissions. SSH refuses keys when the permissions are too open, so this step is not optional. Create the directory if it does not exist, append the public key, and lock down both the directory and the file.
mkdir -p ~/.ssh
chmod 700 ~/.ssh
cat >> ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys
When you run the cat command above, paste the public key content and press Ctrl-D to finish. Then restrict what the deploy user can do. Give it ownership of only the application directory, such as /opt/myapp, and allow it to restart only that one service with sudo if needed. A sudoers entry such as deploy ALL=(ALL) NOPASSWD: /usr/bin/systemctl restart myapp is far safer than giving the deploy user full passwordless sudo.
Now add four secrets in GitHub under Settings, then Secrets and variables, then Actions. HOST is your VPS IP address or hostname. USERNAME is the deploy user. SSH_KEY is the full private key, including the first line starting with BEGIN OPENSSH PRIVATE KEY and the last line starting with END OPENSSH PRIVATE KEY. PORT is your SSH port, usually 22 unless you changed it. Paste the private key exactly as generated, with no extra spaces and no missing lines.
| Secret | Value |
|---|---|
| HOST | VPS IP address or hostname |
| USERNAME | Deploy user on the VPS |
| SSH_KEY | Full private key, including BEGIN and END lines |
| PORT | SSH port, usually 22 |
Writing the deploy to vps github actions Workflow
Create the workflow file at .github/workflows/deploy.yml in your repository. It should trigger on pushes to your main branch only, so feature branches do not deploy to production by accident. The job checks out the code, prepares the SSH key on the runner, copies files with rsync, and then restarts the service on the VPS. For the official reference on workflow syntax, triggers, and secrets, see the official GitHub Actions documentation.
name: Deploy to VPS
on:
push:
branches: [main]
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up SSH key
run: |
mkdir -p ~/.ssh
echo "${{ secrets.SSH_KEY }}" > ~/.ssh/deploy_key
chmod 600 ~/.ssh/deploy_key
ssh-keyscan -p ${{ secrets.PORT }} ${{ secrets.HOST }} >> ~/.ssh/known_hosts
- name: Deploy with rsync
run: |
rsync -avz --delete --exclude '.git' -e "ssh -p ${{ secrets.PORT }} -i ~/.ssh/deploy_key" ./ ${{ secrets.USERNAME }}@${{ secrets.HOST }}:/opt/myapp/
- name: Restart service
uses: appleboy/ssh-action@v1
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
key: ${{ secrets.SSH_KEY }}
port: ${{ secrets.PORT }}
script: |
sudo systemctl restart myapp
sudo systemctl status myapp --no-pager
The ssh-keyscan step records the server host key before rsync connects, which prevents the job from hanging on a host key prompt. The rsync flags matter: the -a flag preserves permissions and timestamps, -v shows what changed, -z compresses the transfer, and –delete removes files on the server that no longer exist in the repository, so stale files do not pile up. The –exclude flag keeps the .git directory off the server. Always test –delete on a staging path first if your application writes uploads or user files inside the deployed directory, because rsync will delete files that are not in the repository.
If your application runs in Docker instead of systemd, replace the restart step with a Compose command. For a deploy to vps github actions workflow that targets Docker, pull the new image and recreate the containers, letting Compose handle the replacement. Keep the same rsync step for the Compose file and application code, or build and push an image from the workflow if you use a registry.
docker compose -f /opt/myapp/docker-compose.yml pull
docker compose -f /opt/myapp/docker-compose.yml up -d
Be honest about downtime with this pattern. Restarting a single service causes a brief outage while the new process starts. That is acceptable for many small sites and internal tools, and it is far better than a half-copied deploy. True zero downtime needs a second instance behind a load balancer, a blue-green setup, or a rolling update across multiple servers, which is a larger design change, not a workflow trick. If you need to test more combinations before deploying, you can also run a build matrix first, as described in this guide to GitHub Actions matrix builds, and deploy only when every job passes.
Rollback When a Deploy Goes Wrong
A deploy plan without rollback is incomplete, and a tested deploy to vps github actions rollback is what turns a scary deploy into a routine one. The simplest rollback is to redeploy the previous good commit. In GitHub, find the last successful workflow run, check out that commit locally or trigger a deploy from it, and let the same rsync and restart steps run again. Because the deploy is scripted, rollback uses the same path as a normal deploy, which makes it far less error-prone than manually copying old files back.
For extra safety, back up the live directory on the VPS before rsync overwrites it. A timestamped copy takes seconds for a small application and gives you an immediate fallback if the new code fails to start. Run this command on the VPS before the rsync step, or as the first command in the restart step, and keep only the last few backups to avoid filling the disk.
cp -a /opt/myapp /opt/myapp.bak-$(date +%Y%m%d%H%M%S)
For Docker deployments, rollback is even simpler: redeploy the previous image tag and run docker compose up again. Whatever method you choose, test rollback once on a staging server before you need it in production. A rollback you have never practised is only a theory.
Troubleshooting deploy to vps github actions Failures
Host key verification failures are the most common first-run problem. The error usually says the host key verification failed or the host key has changed. If you changed servers or rebuilt the VPS, the old host key is no longer valid, and you must update known_hosts. In the workflow above, ssh-keyscan rebuilds known_hosts on every run, which avoids stale keys on the runner. On your own machine, use ssh-keygen -R followed by the hostname to remove the old entry, then connect once manually to accept the new key.
Key format errors are the second common failure. The SSH_KEY secret must be the private key, not the public key, and it must include the BEGIN and END lines with all lines in between. A common mistake is pasting only part of the key, adding a trailing space, or saving the key with Windows line endings. If authentication fails but the key looks right, regenerate a fresh deploy key, add the new public key to the server, and update the secret in one careful pass.
Permission errors usually come from the server side. The .ssh directory must be 700, authorized_keys must be 600, and both must be owned by the deploy user. If the deploy user cannot write to /opt/myapp, rsync will fail with permission denied even though SSH login succeeds. Check ownership with ls -ld on the directory, fix it with chown if needed, and confirm the deploy user can restart only the intended service. If the workflow fails at the restart step, run the same systemctl command manually on the VPS to see whether the problem is the service or the workflow.
Conclusion
Safe VPS deployment is mostly about discipline: one dedicated key, secrets instead of hard-coded values, the same rsync and restart steps every time, and a rollback you have already tested. A stable deploy to vps github actions pipeline makes every push to main a boring, repeatable deploy, which is exactly what production deployment should feel like.