How to deploy with GitHub Actions on DigitalOcean
Deploy to DigitalOcean using GitHub Actions by creating a workflow file that builds your application, pushes it to a registry, and deploys to your Droplet via SSH. Set up secrets in GitHub for secure authentication and use DigitalOcean's App Platform or manual Droplet deployment.
Prerequisites
- GitHub repository with your application code
- DigitalOcean account
- Basic knowledge of Docker and CI/CD concepts
- SSH key pair for server access
Step-by-Step Instructions
Create a DigitalOcean Droplet or App
Set up GitHub Secrets
DIGITALOCEAN_ACCESS_TOKEN- Your DO API tokenHOST- Your Droplet's IP addressUSERNAME- SSH username (usually root)PRIVATE_KEY- Your private SSH key content
Create GitHub Actions Workflow File
.github/workflows/deploy.yml. Start with the basic structure:name: Deploy to DigitalOcean
on:
push:
branches: [ main ]
jobs:
deploy:
runs-on: ubuntu-latest
steps:Configure Build and Test Steps
- uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '18'
- name: Install dependencies
run: npm install
- name: Run tests
run: npm test
- name: Build application
run: npm run buildAdd Docker Build Step (Optional)
- name: Build Docker image
run: docker build -t ${{ github.repository }}:${{ github.sha }} .
- name: Push to registry
run: |
echo ${{ secrets.DIGITALOCEAN_ACCESS_TOKEN }} | docker login registry.digitalocean.com -u ${{ secrets.DIGITALOCEAN_ACCESS_TOKEN }} --password-stdin
docker push registry.digitalocean.com/${{ github.repository }}:${{ github.sha }}Configure SSH Deployment
- name: Deploy to DigitalOcean
uses: appleboy/ssh-action@v1.0.0
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
key: ${{ secrets.PRIVATE_KEY }}
script: |
cd /var/www/your-app
git pull origin main
npm install --production
pm2 restart allTest and Monitor Deployment
Common Issues & Troubleshooting
SSH connection fails during deployment
Verify your SSH key is correctly added to GitHub secrets and that the public key is in /root/.ssh/authorized_keys on your Droplet. Check that SSH is enabled and the firewall allows port 22.
Docker push fails to DigitalOcean Container Registry
Ensure your Container Registry name matches exactly in the push command. Verify the DIGITALOCEAN_ACCESS_TOKEN has write permissions to the registry.
Application fails to start after deployment
Check application logs using pm2 logs or journalctl. Verify all environment variables are set correctly on the Droplet and dependencies are properly installed.
GitHub Actions workflow times out
Increase timeout values using timeout-minutes in your job configuration. Optimize your build process by using caching with actions/cache@v3 for dependencies.