How to deploy to a server using SSH in GitHub Actions
SSH deployment, appleboy/ssh-action, SSH key secrets, known_hosts, remote commands, deployment script execution, server authentication
SSH Deployments
SSH deployment is the simplest production deployment pattern: connect to your server and run a script. It works with any Linux server regardless of cloud provider.
Setting Up SSH Secrets
Generate a deployment key pair locally: ssh-keygen -t ed25519 -C "github-actions-deploy". Add the public key to ~/.ssh/authorized_keys on the server. Store the private key in GitHub Secrets as SSH_PRIVATE_KEY. Store the server IP as SERVER_HOST.
Workflow
- name: Deploy via SSH
uses: appleboy/ssh-action@v1
with:
host: ${{ secrets.SERVER_HOST }}
username: deploy
key: ${{ secrets.SSH_PRIVATE_KEY }}
script: |
cd /var/www/myapp
git pull origin main
npm ci --production
pm2 restart myappThe appleboy/ssh-action handles SSH connection setup, known_hosts configuration, and command execution. The script block runs on the remote server. This example pulls the latest code, installs production dependencies, and restarts the PM2 process. Keep deployment scripts idempotent — they should produce the same result when run multiple times.
