193 lines
6.3 KiB
YAML
193 lines
6.3 KiB
YAML
# Sample workflow for building and deploying a Hugo site to GitHub Pages
|
|
name: Deploy Hugo site to Pages
|
|
|
|
on:
|
|
# Runs on pushes targeting the default branch
|
|
push:
|
|
branches:
|
|
- main
|
|
|
|
# Runs on pull requests
|
|
pull_request:
|
|
|
|
# Allows you to run this workflow manually from the Actions tab
|
|
workflow_dispatch:
|
|
|
|
# Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages
|
|
permissions:
|
|
contents: read
|
|
pages: write
|
|
id-token: write
|
|
issues: write
|
|
pull-requests: write
|
|
|
|
# Allow only one concurrent deployment, skipping runs queued between the run in-progress and latest queued.
|
|
# However, do NOT cancel in-progress runs as we want to allow these production deployments to complete.
|
|
concurrency:
|
|
group: "pages"
|
|
cancel-in-progress: false
|
|
|
|
# Default to bash
|
|
defaults:
|
|
run:
|
|
# GitHub-hosted runners automatically enable `set -eo pipefail` for Bash shells.
|
|
shell: bash
|
|
|
|
jobs:
|
|
# Build job
|
|
build:
|
|
runs-on: ubuntu-latest
|
|
env:
|
|
HUGO_VERSION: 0.148.2
|
|
HUGO_ENVIRONMENT: production
|
|
TZ: America/New_York
|
|
steps:
|
|
- name: Checkout for gh CLI
|
|
uses: actions/checkout@v4
|
|
with:
|
|
submodules: recursive
|
|
fetch-depth: 0
|
|
|
|
- name: Check for existing PR artifact (main branch only)
|
|
if: github.ref == 'refs/heads/main'
|
|
id: check-pr
|
|
run: |
|
|
PR_NUMBER=$(gh pr list --state merged --limit 10 --json number,mergeCommit --jq ".[] | select(.mergeCommit.oid == \"${{ github.sha }}\") | .number" | head -1)
|
|
if [ -n "$PR_NUMBER" ]; then
|
|
echo "Found PR #$PR_NUMBER for this commit"
|
|
echo "pr_number=$PR_NUMBER" >> $GITHUB_OUTPUT
|
|
echo "has_pr=true" >> $GITHUB_OUTPUT
|
|
else
|
|
echo "No PR found, will build normally"
|
|
echo "has_pr=false" >> $GITHUB_OUTPUT
|
|
fi
|
|
env:
|
|
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
|
|
|
- name: Try to download existing PR artifact
|
|
if: github.ref == 'refs/heads/main' && steps.check-pr.outputs.has_pr == 'true'
|
|
id: download-existing
|
|
uses: dawidd6/action-download-artifact@v3
|
|
continue-on-error: true
|
|
with:
|
|
pr: ${{ steps.check-pr.outputs.pr_number }}
|
|
name: github-pages
|
|
path: ./public
|
|
|
|
- name: Check if build is needed
|
|
id: build-needed
|
|
run: |
|
|
if [[ "${{ github.event_name }}" == "pull_request" || "${{ steps.download-existing.outcome }}" == "failure" || "${{ steps.check-pr.outputs.has_pr }}" == "false" ]]; then
|
|
echo "need_build=true" >> $GITHUB_OUTPUT
|
|
else
|
|
echo "need_build=false" >> $GITHUB_OUTPUT
|
|
fi
|
|
|
|
- name: Install Hugo CLI
|
|
if: steps.build-needed.outputs.need_build == 'true'
|
|
run: |
|
|
wget -O ${{ runner.temp }}/hugo.deb https://github.com/gohugoio/hugo/releases/download/v${HUGO_VERSION}/hugo_extended_${HUGO_VERSION}_linux-amd64.deb
|
|
sudo dpkg -i ${{ runner.temp }}/hugo.deb
|
|
- name: Install Dart Sass
|
|
if: steps.build-needed.outputs.need_build == 'true'
|
|
run: sudo snap install dart-sass
|
|
- name: Setup Pages
|
|
if: steps.build-needed.outputs.need_build == 'true'
|
|
id: pages
|
|
uses: actions/configure-pages@v5
|
|
- name: Cache Restore
|
|
if: steps.build-needed.outputs.need_build == 'true'
|
|
id: cache-restore
|
|
uses: actions/cache/restore@v4
|
|
with:
|
|
path: |
|
|
${{ runner.temp }}/hugo_cache
|
|
key: hugo-${{ github.run_id }}
|
|
restore-keys:
|
|
hugo-
|
|
- name: Configure Git
|
|
if: steps.build-needed.outputs.need_build == 'true'
|
|
run: git config core.quotepath false
|
|
- name: Build with Hugo
|
|
if: steps.build-needed.outputs.need_build == 'true'
|
|
run: |
|
|
hugo \
|
|
--gc \
|
|
--minify \
|
|
--baseURL "${{ steps.pages.outputs.base_url }}/" \
|
|
--cacheDir "${{ runner.temp }}/hugo_cache"
|
|
- name: Create CNAME file
|
|
if: steps.build-needed.outputs.need_build == 'true'
|
|
run: |
|
|
cat << EOF > public/CNAME
|
|
neilhanlon.me
|
|
neilhanlon.com
|
|
hanlon.ninja
|
|
thepotato.tech
|
|
EOF
|
|
- name: Cache Save
|
|
if: steps.build-needed.outputs.need_build == 'true'
|
|
id: cache-save
|
|
uses: actions/cache/save@v4
|
|
with:
|
|
path: |
|
|
${{ runner.temp }}/hugo_cache
|
|
key: ${{ steps.cache-restore.outputs.cache-primary-key }}
|
|
|
|
- uses: actions/upload-artifact@v4
|
|
with:
|
|
name: github-pages
|
|
path: ./public
|
|
|
|
- name: Force push to destination branch
|
|
uses: ad-m/github-push-action@latest
|
|
with:
|
|
github_token: ${{ secrets.GITHUB_TOKEN }}
|
|
branch: gh-pages
|
|
force: true
|
|
directory: ./public/
|
|
|
|
- name: Upload new artifact
|
|
if: steps.build-needed.outputs.need_build == 'true'
|
|
run: |
|
|
cd public
|
|
git init
|
|
git config --local user.email "actions@github.com"
|
|
git config --local user.name "Github Actions"
|
|
git commit -m "deploy"
|
|
|
|
# PR Preview job
|
|
preview:
|
|
if: github.event_name == 'pull_request'
|
|
runs-on: ubuntu-latest
|
|
needs: build
|
|
steps:
|
|
- name: Download artifact
|
|
uses: actions/download-artifact@v4
|
|
with:
|
|
name: github-pages
|
|
path: ./preview
|
|
- name: Extract site
|
|
run: |
|
|
cd preview
|
|
tar -xf artifact.tar
|
|
- name: Install surge
|
|
run: npm install -g surge
|
|
- name: Deploy to surge.sh
|
|
run: |
|
|
export SURGE_DOMAIN="thepotato-tech-pr-${{ github.event.number }}.surge.sh"
|
|
surge ./preview --domain $SURGE_DOMAIN --token ${{ secrets.SURGE_TOKEN }}
|
|
echo "Preview URL: https://$SURGE_DOMAIN" >> $GITHUB_STEP_SUMMARY
|
|
- name: Comment PR
|
|
uses: actions/github-script@v7
|
|
with:
|
|
github-token: ${{secrets.GITHUB_TOKEN}}
|
|
script: |
|
|
const domain = `thepotato-tech-pr-${{ github.event.number }}.surge.sh`;
|
|
github.rest.issues.createComment({
|
|
issue_number: context.issue.number,
|
|
owner: context.repo.owner,
|
|
repo: context.repo.repo,
|
|
body: `🚀 Preview deployed to: https://${domain}`
|
|
});
|