Skip to content

Deploy Hugo site to Pages #1105

Deploy Hugo site to Pages

Deploy Hugo site to Pages #1105

Workflow file for this run

# 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"]
# Run daily at 10 AM UTC for bookmark updates and scheduled post publishing
schedule:
- cron: '0 10 * * *'
# 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: write
pages: write
id-token: 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:
shell: bash
jobs:
# Update bookmarks job
update-bookmarks:
runs-on: ubuntu-latest
outputs:
changes: ${{ steps.check-changes.outputs.changes }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.11'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install requests python-dotenv
- name: Run bookmarks.py
env:
LINKDING_API_KEY: ${{ secrets.LINKDING_API_KEY }}
LINKDING_API_ENDPOINT: ${{ secrets.LINKDING_API_ENDPOINT }}
P_ACCESS_TOKEN_ID: ${{ secrets.P_ACCESS_TOKEN_ID }}
P_ACCESS_TOKEN: ${{ secrets.P_ACCESS_TOKEN }}
run: |
python bookmarks.py
- name: Check for changes
id: check-changes
run: |
git add ./content/bookmarks
if git diff --cached --quiet; then
echo "changes=false" >> $GITHUB_OUTPUT
echo "No changes detected"
else
echo "changes=true" >> $GITHUB_OUTPUT
echo "Changes detected"
fi
- name: Commit and push changes
if: steps.check-changes.outputs.changes == 'true'
run: |
git config --global user.name "github-actions[bot]"
git config --global user.email "github-actions[bot]@users.noreply.github.com"
git commit -m "updated bookmarks"
git push
# Check for scheduled posts ready to publish
check-scheduled-posts:
runs-on: ubuntu-latest
outputs:
has_posts: ${{ steps.check-posts.outputs.has_posts }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Check for posts ready to publish
id: check-posts
run: |
NOW=$(date -u +%s)
HAS_POSTS=false
# Find all blog posts and collect their dates first (avoids broken pipe)
DATE_LINES=$(find content/blog -name "index.md" -exec grep -H "^date:" {} \; 2>/dev/null || true)
# Check each date line
while IFS= read -r line; do
if [ -z "$line" ]; then
continue
fi
POST_DATE=$(echo "$line" | awk -F'date: "' '{if (NF>1) print $2}' | tr -d '"')
if [ -n "$POST_DATE" ]; then
POST_TIME=$(date -d "$POST_DATE" +%s 2>/dev/null || echo "0")
if [ "$POST_TIME" -gt 0 ] && [ "$POST_TIME" -le "$NOW" ]; then
HAS_POSTS=true
break
fi
fi
done <<< "$DATE_LINES"
echo "has_posts=$HAS_POSTS" >> $GITHUB_OUTPUT
if [ "$HAS_POSTS" = "true" ]; then
echo "Scheduled posts ready to publish"
else
echo "No scheduled posts ready to publish"
fi
# Build job
build:
runs-on: ubuntu-latest
needs: [update-bookmarks, check-scheduled-posts]
if: needs.update-bookmarks.outputs.changes == 'true' || needs.check-scheduled-posts.outputs.has_posts == 'true' || github.event_name == 'push' || github.event_name == 'workflow_dispatch'
env:
HUGO_VERSION: 0.128.0
steps:
- name: Install Hugo CLI
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
run: sudo snap install dart-sass
- name: Checkout
uses: actions/checkout@v4
with:
submodules: recursive
ref: main
- name: Setup Pages
id: pages
uses: actions/configure-pages@v5
- name: Install Node.js dependencies
run: "[[ -f package-lock.json || -f npm-shrinkwrap.json ]] && npm ci || true"
- name: Build with Hugo
env:
HUGO_CACHEDIR: ${{ runner.temp }}/hugo_cache
HUGO_ENVIRONMENT: production
run: |
hugo mod get -u
hugo \
--minify \
--baseURL "${{ steps.pages.outputs.base_url }}/"
- name: List build output
run: ls -l ./public
- name: Upload artifact
uses: actions/upload-pages-artifact@v3
with:
path: ./public
name: github-pages
# Deployment job
deploy:
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
runs-on: ubuntu-latest
needs: [update-bookmarks, check-scheduled-posts, build]
if: needs.update-bookmarks.outputs.changes == 'true' || needs.check-scheduled-posts.outputs.has_posts == 'true' || github.event_name == 'push' || github.event_name == 'workflow_dispatch'
steps:
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v4