Skip to content

Hugo Build from any branch #27

Hugo Build from any branch

Hugo Build from any branch #27

name: Hugo Build from any branch
# run action on a merged PR on "production"
on:
workflow_dispatch:
inputs:
branch:
description: Branch to build
required: false
type: string
publish-branch:
description: Branch name for build files
required: false
type: string
environment:
description: environment for Hugo
required: false
type: string
default: production
baseURL:
description: baseURL for Hugo
required: false
type: string
env:
publish_branch: ${{ inputs.publish-branch && inputs.publish-branch || inputs.branch && inputs.branch || github.ref_name && github.ref_name }}
HUGO_GIT_BRANCH: ${{ inputs.branch && inputs.branch || github.ref_name && github.ref_name }}
TZ: America/New_York
jobs:
# ------------------
# build to the branch
# ------------------
build:
runs-on: ubuntu-22.04
# can't use the "env" context at the job level, only in steps, so need to eval the conditional again
name: Build `${{ inputs.branch && inputs.branch || github.ref_name && github.ref_name }} to builds/${{ inputs.publish-branch && inputs.publish-branch || inputs.branch && inputs.branch || github.ref_name && github.ref_name }}`
# wait for other triggered jobs to finish before running this
concurrency: # don't run two of these jobs at the same time
group: ${{ github.workflow }}-${{ inputs.branch }} # use the given branch name, so only one job on a branch will run at a time
cancel-in-progress: true # this makes it so that only the most recent job is run
# define job's steps
steps:
# setting line endings to LF to match line endings in source files (which were used to compute resource integrity)
- name: set git EOL
run: |
git config --global core.eol lf
git config --global core.autocrlf input
# using checkout action
- name: Checkout
uses: actions/checkout@v4
with:
ref: ${{ inputs.branch }}
fetch-depth: 0
# get last commit
- name: get last commit
id: last_commit
run: |
echo "last_hash=$(git log -1 --format=%h --abbrev=10)" >> $GITHUB_OUTPUT
echo "last_msg=\"$(git log -1 --format=%s)\"" >> $GITHUB_OUTPUT
# set up node and cache for npm
- name: Setup node
uses: actions/setup-node@v4
with:
node-version: 22.5.x
cache: "npm"
# set up cache for node modules
- name: Cache node_modules
id: cache-nodemodules
uses: actions/cache@v4
env:
cache-name: cache-node-modules
with:
path: node_modules
key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-build-${{ env.cache-name }}-
${{ runner.os }}-build-
${{ runner.os }}-
# prune node_modules
- name: Prune dependencies
run: npm prune
# update npm dependencies (if cached)
- name: Update dependencies
if: steps.cache-nodemodules.outputs.cache-hit == 'true'
run: npm update
# install npm dependencies (if not cached)
- name: Install dependencies
if: steps.cache-nodemodules.outputs.cache-hit != 'true'
run: npm ci
# list dependencies
- name: List dependencies
run: npm ls
- name: List sub-dependencies
run: npm ls --all
# set up Hugo
- name: Hugo setup
uses: peaceiris/actions-hugo@v3
with:
hugo-version: "0.122.0"
extended: true
# build site with Hugo
# if only environment is specified
- name: Hugo Build environment
if: ${{ ! inputs.baseURL }}
run: |
export HUGO_GIT_LAST_HASH=${{ steps.last_commit.outputs.last_hash }}
export HUGO_GIT_LAST_MSG=${{ steps.last_commit.outputs.last_msg }}
hugo --environment ${{ inputs.environment }} --cleanDestinationDir --logLevel debug
# if baseURL is also specified
- name: Hugo Build environment + baseURL
if: ${{ inputs.baseURL }}
run: |
export HUGO_GIT_LAST_HASH=${{ steps.last_commit.outputs.last_hash }}
export HUGO_GIT_LAST_MSG=${{ steps.last_commit.outputs.last_msg }}
hugo --environment ${{ inputs.environment }} --baseURL ${{ inputs.baseURL }} --cleanDestinationDir --logLevel debug
# run pagefind
- name: Build search with pagefind
run: |
cd '${{ github.workspace }}'
npx -y pagefind --site docs
# commit built index
- name: Commit changes to PagesIndex.json, package.json, & package-lock.json
uses: elstudio/actions-js-build/commit@v4
with:
commitMessage: Update index & NPM package list
# publish to "deploy" branch
- name: Publish to "builds/${{ env.publish_branch }}"
uses: peaceiris/actions-gh-pages@v4
with:
full_commit_message: Publish ${{ steps.last_commit.outputs.last_hash }} (${{ steps.last_commit.outputs.last_msg }}) to "builds/${{ env.publish_branch }}"
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_branch: builds/${{ env.publish_branch }}
publish_dir: ./docs/
enable_jekyll: false