Auto-Deploy to Ubuntu Server with GitHub Actions

Stop manually deploying every time you push code. Set up GitHub Actions to automatically deploy your app when you push to main.

Setup

1. Add Repository Secrets

Go to your repo → SettingsSecrets and variablesActions

Add these secrets:

2. Generate SSH Keys

On your server:

ssh-keygen -t rsa -b 4096 -C "your-email@example.com"
# Press enter for default location

Add GitHub to known hosts on your server:

ssh-keyscan -H github.com >> ~/.ssh/known_hosts

Add public key to GitHub and your server:

cat ~/.ssh/id_rsa.pub
# Copy this and:
# 1. Add to GitHub Settings → SSH and GPG keys
# 2. Add to your server's authorized_keys:
echo "your-public-key-here" >> ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys

Get private key for GitHub secrets:

cat ~/.ssh/id_rsa
# Copy the entire key including -----BEGIN/END----- lines
# Add this as SSH_PRIVATE_KEY secret

3. Create Workflow

Create .github/workflows/deploy.yml:

name: Deploy Polaric

on:
  push:
    branches:
      - master

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
      - name: Add SSH private key to agent
        uses: webfactory/ssh-agent@v0.9.0
        with:
          ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }}
      - name: Deployment to Server
        env:
          SERVER_USER: ${{ secrets.SERVER_USER }}
          SERVER_IP: ${{ secrets.SERVER_IP }}
          APP_DIR: ${{ secrets.APP_DIR }}
        run: |
         ssh -o StrictHostKeyChecking=no $SERVER_USER@$SERVER_IP "
          cd $APP_DIR &&
          git remote set-url origin git@github.com:febriliankr/<REPOSITORY NAME>.git &&
          git pull origin master && make ci-deploy
          "

Done

Push to main and check the Actions tab to see your deployment run.

That’s it. Your app now deploys automatically on every push to main.


Tested and working as of August 17, 2025