Uploading changes via FTP or connecting via SSH to make git pull works—until the day you forget a file or deploy the wrong branch. With GitHub Actions, every push to the main branch copies the code to the VPS and restarts the application—always in the same way.
How It Works
- You go from
pushtomain. - GitHub starts up a temporary machine, downloads your repository, and, if necessary, compiles the project.
- That machine connects to your VPS via SSH using a dedicated key, synchronizes the files with
rsync, and restarts the service.
The VPS does not need access to GitHub, and the key used by GitHub only grants access to a user without privileges.
1. Set up a deployment user on the VPS
sudo adduser --disabled-password --gecos '' deploysudo mkdir -p /var/www/myappsudo chown deploy:deploy /var/www/myappIf you need to restart a service during deployment, allow deploy to run that command—and no other—using sudo:
TMP=$(mktemp)echo 'deploy ALL=(root) NOPASSWD: /usr/bin/systemctl restart myapp' > "$TMP"sudo visudo -cf "$TMP" && sudo install -m 440 -o root -g root "$TMP" /etc/sudoers.d/deployrm -f "$TMP"visudo -cf validates the rule before installing it, and install only runs if it is correct. An error in a sudoers file can cause you to lose sudo system-wide.
2. Create an SSH key just for GitHub
On your computer, generate a new SSH key without a passphrase, because GitHub needs to use it without anyone typing it in. Do not reuse it for anything else:
ssh-keygen -t ed25519 -f deploy_key -N "" -C "github-actions-deploy"On the VPS, add the public key to user deploy, replacing PUBLIC_KEY with the contents of deploy_key.pub:
sudo -u deploy mkdir -p /home/deploy/.sshecho 'PUBLIC_KEY' | sudo -u deploy tee -a /home/deploy/.ssh/authorized_keyssudo chmod 700 /home/deploy/.sshsudo chmod 600 /home/deploy/.ssh/authorized_keys3. Save the server's fingerprint
To prevent GitHub from accepting any server that impersonates yours, save its fingerprint in advance. From your computer:
ssh-keyscan -t ed25519 -p 22 your-server.example.com > known_hosts_vpsssh-keygen -lf known_hosts_vpsUse the same name or IP address and the same port that you will enter in secrets SSH_HOST and SSH_PORT. If the port is different from 22, the line begins with [host]:puerto: keep it exactly as it is.
Before trusting the result, compare the ED25519 fingerprint with the one shown by the VPS itself:
ssh-keygen -lf /etc/ssh/ssh_host_ed25519_key.pub4. Add the secrets to the repository
On GitHub, go to Settings → Secrets and variables → Actions and create these secrets:
SSH_HOST: The IP address or domain name of the VPS.SSH_PORT: the SSH port (22, unless you've changed it).SSH_USER:deploy.SSH_KEY: the complete contents of the private keydeploy_key.SSH_KNOWN_HOSTS: the content ofknown_hosts_vps.
Afterward, delete deploy_key from your computer or save it in your password manager: it should never end up in the repository.
5. Create the workflow
Add this file to .github/workflows/deploy.yml:
name: Deployon: push: branches: [main]concurrency: group: production-deploy cancel-in-progress: falsejobs: deploy: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - name: Configure SSH run: | mkdir -p ~/.ssh echo "${{ secrets.SSH_KEY }}" > ~/.ssh/deploy_key chmod 600 ~/.ssh/deploy_key echo "${{ secrets.SSH_KNOWN_HOSTS }}" > ~/.ssh/known_hosts - name: Upload files run: > rsync -az --delete --exclude '.git' --exclude '.github' --exclude '.env' --exclude 'storage/' -e "ssh -i ~/.ssh/deploy_key -p ${{ secrets.SSH_PORT }}" ./ ${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }}:/var/www/myapp/ - name: Restart service run: ssh -i ~/.ssh/deploy_key -p ${{ secrets.SSH_PORT }} ${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }} 'sudo systemctl restart myapp'Adjust the path, exclusions, and service name to fit your project. If your application needs to be compiled (npm run build, composer install), add those steps before Upload files and sync only the resulting folder. The concurrency block prevents two deployments from overlapping if you run two pushs in a row.
Common Mistakes
Host key verification failed: The secretSSH_KNOWN_HOSTSis empty or does not correspond to the port you are using.Permission denied (publickey): The public key is not inauthorized_keysfor userdeploy, or the permissions for.sshare too permissive.- Files have disappeared from the server:
--deletedeletes everything from the destination that isn't in the repository. Before the first deployment, add an exclusion for user uploads, the.envfile, and the caches.
ext step
Using the same workflow, you can have one VPS for testing and another for production—you just need to change the secrets. Deploy your Cloud VPS and connect it to your repository.