Using Github Actions to Deploy Hugo

We will be building off of the work done in Deploying Hugo to Fly.io and using Github actions to deploy to Fly.io.

To get this workon, we just have to add the following to .github/workflows/release.yml, then configure the action’s secrets.

# .github/workflows/release.yml
name: Release

on:
  push:
    branches: [ main ]

concurrency:
  group: ${{ github.ref }}
  cancel-in-progress: ${{ github.ref != 'refs/heads/main' }}

env:
  FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }}
  HUGO_OPENGRAPH_KEY: ${{ secrets.HUGO_OPENGRAPH_KEY }}
  HUGO_OPENGRAPH_SECRET: ${{ secrets.HUGO_OPENGRAPH_SECRET }}

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
        with:
          ref: main
          fetch-depth: 0

      - uses: superfly/flyctl-actions/setup-flyctl@master

      - working-directory: hugo
        run: |
          flyctl deploy \
            --remote-only \
            --build-arg HUGO_OPENGRAPH_KEY=${{ env.HUGO_OPENGRAPH_KEY }} \
            --build-arg HUGO_OPENGRAPH_SECRET=${{ env.HUGO_OPENGRAPH_SECRET }}          

Visit your repository on github.com and click the “Settings” tab. Then, in the sidebar click “Secrets -> Actions” to reveal the action secrets for your repository. This is where we will be adding the HUGO_OPENGRAPH_KEY, HUGO_OPENGRAPH_SECRET, and FLY_API_TOKEN. You should already have the HUGO_OPENGRAPH_KEY, HUGO_OPENGRAPH_SECRET values in your hugo/.env. Assuming you have successfully deployed your site to Fly.io, you can get the FLY_API_TOKEN by running fly auth token.

Once those three variables have been added to your repositories secrets, you can commit these changes to the main branch and then visit the “Actions” tab of your repository on Github.com.