|
| 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, closed] |
| 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: |
| 28 | + runs-on: ubuntu-latest |
| 29 | + env: |
| 30 | + HUGO_VERSION: 0.147.9 |
| 31 | + |
| 32 | + steps: |
| 33 | + - name: Install Hugo CLI |
| 34 | + if: github.event.action != 'closed' |
| 35 | + run: | |
| 36 | + wget -O ${{ runner.temp }}/hugo.deb https://github.com/gohugoio/hugo/releases/download/v${HUGO_VERSION}/hugo_extended_${HUGO_VERSION}_linux-amd64.deb \ |
| 37 | + && sudo dpkg -i ${{ runner.temp }}/hugo.deb |
| 38 | +
|
| 39 | + - name: Install Dart Sass |
| 40 | + if: github.event.action != 'closed' |
| 41 | + run: sudo snap install dart-sass |
| 42 | + |
| 43 | + - name: Checkout |
| 44 | + if: github.event.action != 'closed' |
| 45 | + uses: actions/checkout@v6 |
| 46 | + with: |
| 47 | + ref: ${{ github.event.pull_request.head.sha }} |
| 48 | + submodules: recursive |
| 49 | + fetch-depth: 0 |
| 50 | + |
| 51 | + - name: Install Node.js dependencies |
| 52 | + if: github.event.action != 'closed' |
| 53 | + run: "[[ -f package-lock.json || -f npm-shrinkwrap.json ]] && npm ci || true" |
| 54 | + |
| 55 | + - name: Build with Hugo |
| 56 | + if: github.event.action != 'closed' |
| 57 | + env: |
| 58 | + HUGO_ENVIRONMENT: production |
| 59 | + HUGO_ENV: production |
| 60 | + run: | |
| 61 | + REPO_NAME=$(echo "${{ github.repository }}" | cut -d'/' -f2) |
| 62 | + ORG_NAME=$(echo "${{ github.repository }}" | cut -d'/' -f1) |
| 63 | +
|
| 64 | + PR_BASE_URL="https://${ORG_NAME}.github.io/${REPO_NAME}/pr-preview/pr-${{ github.event.pull_request.number }}/" |
| 65 | +
|
| 66 | + echo "Building for BaseURL: ${PR_BASE_URL}" |
| 67 | +
|
| 68 | + hugo \ |
| 69 | + --gc \ |
| 70 | + --minify \ |
| 71 | + --buildDrafts \ |
| 72 | + --buildFuture \ |
| 73 | + --baseURL "${PR_BASE_URL}" |
| 74 | +
|
| 75 | + - name: Deploy PR Preview |
| 76 | + if: github.event.action != 'closed' |
| 77 | + id: deploy-preview |
| 78 | + uses: rossjrw/pr-preview-action@v1.6.3 |
| 79 | + with: |
| 80 | + source-dir: ./public |
| 81 | + preview-branch: gh-pages |
| 82 | + umbrella-dir: pr-preview |
| 83 | + action: auto |
| 84 | + comment: false |
| 85 | + |
| 86 | + - name: Comment PR with Preview URL |
| 87 | + if: github.event.action != 'closed' |
| 88 | + uses: marocchino/sticky-pull-request-comment@v2 |
| 89 | + with: |
| 90 | + header: pr-preview |
| 91 | + message: | |
| 92 | + 🚀 **Preview deployment for PR #${{ github.event.pull_request.number }}** |
| 93 | + |
| 94 | + 🌐 **Preview URL**: ${{ steps.deploy-preview.outputs.preview-url }} |
| 95 | + |
| 96 | + _This preview will be updated automatically when you push new commits to this PR._ |
| 97 | +
|
| 98 | + - name: Cleanup PR Preview on Close |
| 99 | + if: github.event.action == 'closed' |
| 100 | + uses: rossjrw/pr-preview-action@v1.6.3 |
| 101 | + with: |
| 102 | + preview-branch: gh-pages |
| 103 | + umbrella-dir: pr-preview |
| 104 | + action: remove |
0 commit comments