Deploying a Hugo Application with GitHub Actions

GitHub Actions is a powerful tool for automating processes in GitHub repositories. In this article, we will discuss how to deploy a Hugo-based website using GitHub Actions.

Configuring GitHub Actions

  1. Create a file named .github/workflows/deploy.yml in your repository.

  2. Add the following configuration to the deploy.yml file:

name: Deploy website

on:
  push:
    branches:
      - master

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - name: Get files
        uses: actions/checkout@v3
        with: 
          submodules: true
          fetch-depth: 0

      - name: Setup Hugo
        uses: peaceiris/actions-hugo@v2
        with:
          hugo-version: '0.101.0'
          extended: true

      - name: Build
        run: hugo 

      - name: Install SSH key
        uses: shimataro/ssh-key-action@v2
        with: 
          key: ${{ secrets.SSH_KEY }}
          known_hosts: ${{ secrets.KNOWN_HOSTS }}

      - name: Deploy
        run: rsync -avhe "ssh -p ${{ secrets.SSH_PORT }}" --rsync-path="sudo rsync" ./public/ ${{ secrets.SSH_TARGET }}:${{ secrets.SSH_PATH }} --delete

Adding Variables in GitHub Repository

To add variables like secrets.SSH_PORT to your GitHub repository:

  1. Go to the settings of your repository on GitHub.

  2. In the “Secrets” section, you can add new variables. For example, to add SSH_PORT:

    • Click on “New repository secret.”
    • Give it the name SSH_PORT.
    • Enter the appropriate SSH port value.

    You can do the same for other secrets like SSH_KEY, KNOWN_HOSTS, SSH_TARGET, and SSH_PATH, which are used in the deployment script.

Now, after each code push to the master branch, GitHub Actions will automatically trigger the deployment process of your Hugo-based website to the target server using the provided secrets.