
Automate Next.js deployment to VPS with GitHub Actions CI/CD. Zero-downtime updates via SSH. Follow our guide for seamless pushes!
GitHub Actions CI/CD streamlines Next.js auto-deployment to a VPS. This guide builds on our Next.js VPS setup guide and zero-downtime deployment scripting, automating the deploy.sh script execution via SSH for seamless updates on every push to the main branch.
Before starting, ensure you have:
Generate an SSH key pair locally:
ssh-keygen -t rsa -b 4096 -C "your_email@example.com" -f ./id_rsaOr, for enhanced security, use ED25519:
ssh-keygen -t ed25519 -a 200 -C "your_email@example.com" -f ./id_rsaLeave the passphrase empty for GitHub Actions compatibility.
Add the public key to your VPS:
Copy public key
cat ./id_rsa.pubLogin via ssh
ssh your_username@your-vps-ipAdd in ~/.ssh/authorized_keys
nano ~/.ssh/authorized_keysEnsure correct permissions:
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keysStore the private key in GitHub Secrets:
Copy the private key (id_rsa or id_ed25519) and add it as VPS_SSH_KEY in your GitHub repository under Settings > Secrets and variables > Actions > New repository secret.
Add the following secrets in Settings > Secrets and variables > Actions:
VPS_HOST: Your VPS IP address or domain (e.g., 192.168.1.1 or yourdomain.com).VPS_USERNAME: VPS username with repository access (e.g., deployuser).VPS_SSH_KEY: Private SSH key from the previous step.VPS_SSH_PORT: Non-standard SSH port if not 22.Create a workflow file:
In your repository, create .github/workflows/deploy.yml.
Add the workflow configuration:
name: Deploy to VPS
on:
push:
branches:
- main
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Deploy to VPS
uses: appleboy/ssh-action@v1
with:
host: ${{ secrets.VPS_HOST }}
username: ${{ secrets.VPS_USERNAME }}
key: ${{ secrets.VPS_SSH_KEY }}
port: ${{ secrets.VPS_SSH_PORT || 22 }}
script: |
cd /home/your_username/your_nextjs_app
./deploy.shReplace
/home/your_username/your_nextjs_appwith the actual path to your repository on the VPS. Ensuredeploy.shis executable (zero-downtime guide):
chmod +x /home/your_username/your_nextjs_app/deploy.shdeploy.yml to the main branch.Update your code: Make changes to your Next.js app, commit, and push to the main branch:
git add .
git commit -m "Test deployment"
git push origin mainMonitor the workflow: Go to your GitHub repository’s Actions tab, find the Deploy to VPS workflow, and check its status.
Verify the deployment:
SSH into your VPS, check PM2 logs (pm2 logs nextapp), and visit your app’s URL to confirm updates.
This GitHub Actions CI/CD setup automates Next.js deployments with zero-downtime, triggered on every main branch push. It builds on our VPS deployment guide and zero-downtime scripting guide, ensuring a reliable, efficient deployment process.