|
| 1 | +# Workflow for building and deploying Hugo PR previews to GitHub Pages |
| 2 | +name: preview-deploy |
| 3 | + |
| 4 | +on: |
| 5 | + pull_request_target: |
| 6 | + branches: [ "master" ] |
| 7 | + types: [opened, synchronize, reopened] |
| 8 | + |
| 9 | + workflow_dispatch: |
| 10 | + |
| 11 | +# Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages and comments |
| 12 | +permissions: |
| 13 | + contents: write |
| 14 | + pages: write |
| 15 | + pull-requests: write |
| 16 | + |
| 17 | +concurrency: |
| 18 | + group: "pages-${{ github.event.pull_request.number || github.run_id }}" |
| 19 | + cancel-in-progress: true |
| 20 | + |
| 21 | +# Default to bash |
| 22 | +defaults: |
| 23 | + run: |
| 24 | + shell: bash |
| 25 | + |
| 26 | +jobs: |
| 27 | + # Build and Deploy job |
| 28 | + build-and-deploy: |
| 29 | + runs-on: ubuntu-latest |
| 30 | + env: |
| 31 | + HUGO_VERSION: 0.147.9 |
| 32 | + steps: |
| 33 | + - name: Install Hugo CLI |
| 34 | + run: | |
| 35 | + wget -O ${{ runner.temp }}/hugo.deb https://github.com/gohugoio/hugo/releases/download/v${HUGO_VERSION}/hugo_extended_${HUGO_VERSION}_linux-amd64.deb \ |
| 36 | + && sudo dpkg -i ${{ runner.temp }}/hugo.deb |
| 37 | + - name: Install Dart Sass |
| 38 | + run: sudo snap install dart-sass |
| 39 | + |
| 40 | + - name: Checkout |
| 41 | + uses: actions/checkout@v6 |
| 42 | + with: |
| 43 | + ref: ${{ github.event.pull_request.head.sha }} |
| 44 | + submodules: recursive |
| 45 | + fetch-depth: 0 |
| 46 | + |
| 47 | + - name: Install Node.js dependencies |
| 48 | + run: "[[ -f package-lock.json || -f npm-shrinkwrap.json ]] && npm ci || true" |
| 49 | + |
| 50 | + - name: Build with Hugo |
| 51 | + env: |
| 52 | + # For maximum backward compatibility with Hugo modules |
| 53 | + HUGO_ENVIRONMENT: production |
| 54 | + HUGO_ENV: production |
| 55 | + run: | |
| 56 | + # Dynamically set BaseURL for PR preview |
| 57 | + # Format: https://<ORG>.github.io/<REPO>/pr-preview/pr-<NUMBER>/ |
| 58 | + # We use github context to make it generic |
| 59 | + REPO_NAME=$(echo "${{ github.repository }}" | cut -d'/' -f2) |
| 60 | + ORG_NAME=$(echo "${{ github.repository }}" | cut -d'/' -f1) |
| 61 | + |
| 62 | + PR_BASE_URL="https://${ORG_NAME}.github.io/${REPO_NAME}/pr-preview/pr-${{ github.event.pull_request.number }}/" |
| 63 | + |
| 64 | + echo "Building for BaseURL: ${PR_BASE_URL}" |
| 65 | + |
| 66 | + hugo \ |
| 67 | + --gc \ |
| 68 | + --minify \ |
| 69 | + --buildDrafts \ |
| 70 | + --buildFuture \ |
| 71 | + --baseURL "${PR_BASE_URL}" |
| 72 | + |
| 73 | + - name: Deploy PR Preview |
| 74 | + id: deploy-preview |
| 75 | + uses: rossjrw/pr-preview-action@v1.6.3 |
| 76 | + with: |
| 77 | + source-dir: ./public |
| 78 | + preview-branch: gh-pages |
| 79 | + umbrella-dir: pr-preview |
| 80 | + action: auto |
| 81 | + comment: false |
| 82 | + |
| 83 | + - name: Comment PR with Preview URL |
| 84 | + uses: marocchino/sticky-pull-request-comment@v2 |
| 85 | + if: github.event_name == 'pull_request_target' |
| 86 | + with: |
| 87 | + header: pr-preview |
| 88 | + message: | |
| 89 | + 🚀 **Preview deployment for PR #${{ github.event.pull_request.number }}** |
| 90 | + |
| 91 | + 🌐 **Preview URL**: ${{ steps.deploy-preview.outputs.preview-url }} |
| 92 | + |
| 93 | + _This preview will be updated automatically when you push new commits to this PR._ |
0 commit comments