Skip to content

Fixup

Fixup #287

#
# GitHub Actions workflow: Builds the site and uploads it to the target server.
#
# For more details on workflows, see README.md.
#
# See also: https://gohugo.io/hosting-and-deployment/hosting-on-github/
#
name: Build and Deploy
# When to run this workflow
# See: https://docs.github.com/en/actions/reference/events-that-trigger-workflows
# See: https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#on
on:
# Trigger the workflow on push to the main branch (deploy to production).
push:
# NOTE: The preview branch is for tests without pull request (as GitHub Action sometimes work differently for pushes
# and pull requests).
branches: [ main, preview ]
# Trigger the workflow for any pull requests (deploy to preview, if "local" PR; don't deploy if PR from fork).
pull_request:
# Allow manual run of this workflow (https://docs.github.com/en/actions/using-workflows/manually-running-a-workflow)
workflow_dispatch:
# Permissions for GITHUB_TOKEN for this workflow.
# See: https://docs.github.com/en/actions/reference/authentication-in-a-workflow#permissions-for-the-github_token
permissions:
contents: read
env:
# The Hugo version to use: https://github.com/gohugoio/hugo/releases
# NOTE: It sometimes happens that new Hugo versions break the site. So I've decided to always
# "pin" the Hugo version (instead of using the latest version). This way, the Hugo version
# can be updated with a pull request where I can test the new version.
# IMPORTANT: Always use a pull request to update the Hugo version to be able to fix all problems
# before pushing the result to production!!!
HUGO_VERSION: 0.123.7
# Set "DEPLOY_STATE" to "production" if this workflow was triggered by a push to the main branch.
# Set "DEPLOY_STATE" to "preview" in any other case (i.e. pull requests).
DEPLOY_STAGE: ${{ github.ref == 'refs/heads/main' && 'production' || 'preview' }}
# Allow only one concurrent deployment, skipping runs queued between the run in-progress and latest queued.
# https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#concurrency
concurrency:
# Makes this workflow part of the "deploy" concurrency group. (Note that this text can be chosen arbitrarily.)
# NOTE: Unfortunately, we can't use "env.DEPLOY_STAGE" as "env." is not supported.
group: deploy-${{ github.ref == 'refs/heads/main' && 'production' || 'preview' }}
# Do NOT cancel in-progress runs as we want to allow these deployments to complete.
cancel-in-progress: false
# NOTE: Jobs run in parallel by default.
# https://docs.github.com/en/actions/using-jobs/using-jobs-in-a-workflow
jobs:
build-and-deploy:
# Name the job
name: Build & Deploy
# Set the type of machine to run on
# See: https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#jobsjob_idruns-on
runs-on: ubuntu-latest
env:
# The directory in which Hugo stores the generated site.
# NOTE: '${{ runner.temp }}' isn't available here so we have to use '${{ github.workspace }}'.
PUBLISH_DIR: '${{ github.workspace }}/public/'
# The directory in which to store diffable files (i.e. the generated site which can be more easily diffed
# especially across branches).
DIFF_PUBLISH_DIR: '${{ github.workspace }}/public-diff/'
# Hugo build parameters used by all build steps.
HUGO_COMMON_BUILD_PARAMS: --printPathWarnings --logLevel info --panicOnWarning
steps:
###########################################################################
#
# Setup Steps
#
###########################################################################
# See: https://github.com/actions/checkout
- name: Clone Git repository
uses: actions/checkout@v4
###########################################################################
#
# Build Steps
#
###########################################################################
###########################################################################
#
# Archive Steps
#
###########################################################################
- name: Create diffable output
run: |
mkdir '${{ env.DIFF_PUBLISH_DIR }}'
echo 'abc' > ${{ env.DIFF_PUBLISH_DIR }}test.txt
- name: Make branch name compatible with file names
id: file-branch-name
# Based on: https://stackoverflow.com/a/58035262/614177
# For syntax, see: https://www.gnu.org/software/bash/manual/bash.html#Shell-Parameter-Expansion
# Strips "feature/" from PR branch names. Also replaces '/' with '-'.
run: echo "branch=${${${GITHUB_HEAD_REF#feature/}:-${GITHUB_REF#refs/heads/}}//\//-}" >> $GITHUB_OUTPUT
#
# Store generated files in a zip file in the workflow itself. This way outputs between two workflow runs can be compared,
# if necessary.
#
# See: https://github.com/actions/upload-artifact
#
# NOTES:
# * Artifacts are retained only up to 90 days at the moment. See: https://github.com/orgs/community/discussions/107115
# * We keep the default compression level as in our tests increasing it to 9 (max) resulted in a zip that's only
# about 10 KB smaller (5.88 MB to 5.87 MB).
#
- name: Attach generated files to workflow run
uses: actions/upload-artifact@v4
with:
name: 'published-output__#${{ github.run_number }}__${{ steps.file-branch-name.outputs.branch }}'
path: ${{ env.DIFF_PUBLISH_DIR }}
if-no-files-found: error
###########################################################################
#
# Deploy Steps
#
###########################################################################