Automating Deployment to an Ubuntu Server with GitHub Actions

admin Avatar

Deploying code to your server is an essential part of any development pipeline. Automating this process not only saves time but also reduces the risk of human error. In this blog post, I’ll walk you through setting up a GitHub Actions workflow that automatically deploys updates to your Ubuntu server whenever you push changes to your repository.

Why Use GitHub Actions for Deployment?

GitHub Actions provides a powerful CI/CD tool directly integrated with your GitHub repository. It allows you to automate tasks like testing, building, and deploying your code. By setting up a workflow, you can ensure that your server is always up to date with the latest changes, without needing to manually SSH into your server to pull updates.

Prerequisites

Before we begin, ensure you have the following:

  1. An Ubuntu Server: This is where your application is hosted.
  2. GitHub Repository: The repository where your code is stored.
  3. SSH Access to Your Server: You’ll need SSH keys set up to allow GitHub Actions to access your server.

Step 1: Generate SSH Keys

First, generate an SSH key pair on your local machine. This key will be used by GitHub Actions to securely connect to your server.

ssh-keygen -t rsa -b 4096 -C "[email protected]"

Follow the prompts, and save the key pair in the default location (~/.ssh/id_rsa). You’ll use the private key in the GitHub repository and add the public key to your server.

Add Public Key to Your Server

Copy the public key to your server:

ssh-copy-id -i ~/.ssh/id_rsa.pub your_server_username@your_server_ip

This command adds the public key to your server’s authorized_keys file, allowing SSH access.

Step 2: Add SSH Key to GitHub Secrets

Next, add the private key to your GitHub repository as a secret:

  1. Go to your repository on GitHub.
  2. Navigate to Settings > Secrets and variables > Actions.
  3. Click on New repository secret and add the following:
  • Name: SSH_PRIVATE_KEY
  • Value: Paste the contents of your ~/.ssh/id_rsa file.

Also, add the following secrets:

  • SERVER_USERNAME: Your server username.
  • SERVER_IP: Your server’s IP address.

Step 3: Create a GitHub Actions Workflow

Now, let’s create a workflow that will automate the deployment process.

  1. In your repository, go to the Actions tab.
  2. Click on New workflow, then Set up a workflow yourself.
  3. Replace the content with the following:
name: Deploy to Ubuntu Server

on:
  push:
    branches:
      - main

jobs:
  deploy:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v3

      - name: Deploy to server
        env:
          SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }}
          SERVER_USERNAME: ${{ secrets.SERVER_USERNAME }}
          SERVER_IP: ${{ secrets.SERVER_IP }}
        run: |
          echo "$SSH_PRIVATE_KEY" > private_key
          chmod 600 private_key
          ssh -o StrictHostKeyChecking=no -i private_key $SERVER_USERNAME@$SERVER_IP << 'EOF'
            cd /path/to/project/
            git pull origin main
            composer install
            php artisan config:cache
            php artisan route:cache
            php artisan view:cache
            php artisan migrate --force
            npm install
            npm run build
          EOF
          rm -f private_key

Explanation of the Workflow

  • Trigger: The workflow is triggered on every push to the main branch.
  • Jobs: It runs a job called deploy on the latest Ubuntu environment (ubuntu-latest).
  • Steps:
  • The first step checks out the code from your repository.
  • The second step connects to your server via SSH, navigates to your project directory, pulls the latest code, installs dependencies, caches configurations, migrates the database, and builds the frontend assets.

Step 4: Commit and Push

Finally, commit and push your workflow file to your repository. The next time you push changes to the main branch, the workflow will automatically deploy the latest updates to your server.

Leave a Reply

Your email address will not be published. Required fields are marked *

Latest Posts