Automating Deployments with GitHub Actions and SSH

Manual git pull and service restarts on your remote server are prone to human error and slow down your iteration cycle. True developer efficiency comes from fully automated Continuous Integration and Continuous Deployment (CI/CD).

This guide demonstrates how to set up a lightweight automated deployment pipeline from GitHub to your Starlight VPS.

The Deployment Workflow Concept

  1. Push to Main: You push code updates to your GitHub repository.
  2. Trigger Runner: GitHub Actions triggers a workflow runner.
  3. SSH Remote Execution: The runner securely logs into your VPS via SSH, pulls the latest code, and restarts your Docker containers.

Setting Up Your GitHub Action Workflow

Create a file at .github/workflows/deploy.yml in your project repository:

name: Deploy to VPS

on:
  push:
    branches: [ main ]

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Code
        uses: actions/checkout@v4

      - name: Deploy via SSH
        uses: appleboy/ssh-action@master
        with:
          host: ${{ secrets.VPS_HOST }}
          username: ${{ secrets.VPS_USER }}
          key: ${{ secrets.VPS_SSH_KEY }}
          script: |
            cd /var/www/my-project
            git pull origin main
            docker compose up -d --build

Securing Your Secrets

Never hardcode your server IP addresses or SSH private keys into code repositories. Store them securely under your GitHub repository's Settings > Secrets and variables > Actions.

Build on a Solid Foundation

Smooth CI/CD pipelines rely on low-latency network connectivity and dependable server uptimes.

Have questions about configuring SSH keys or GitHub Actions? Feel free to reach out via GitHub or email.

🚀 Ready to Launch Your Next Project?

The domain and hosting environment recommended here are powered by Spaceship. Experience the next-generation minimalist cloud platform.

Explore Spaceship Offers