Enhance preview workflow to handle closed PRs #6
Workflow file for this run
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Workflow for building and deploying Hugo PR previews to GitHub Pages | |
| name: preview-deploy | |
| on: | |
| pull_request_target: | |
| branches: [ "master" ] | |
| types: [opened, synchronize, reopened, closed] | |
| workflow_dispatch: | |
| # Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages and comments | |
| permissions: | |
| contents: write | |
| pages: write | |
| pull-requests: write | |
| concurrency: | |
| group: "pages-${{ github.event.pull_request.number || github.run_id }}" | |
| cancel-in-progress: true | |
| # Default to bash | |
| defaults: | |
| run: | |
| shell: bash | |
| jobs: | |
| build-and-deploy: | |
| runs-on: ubuntu-latest | |
| env: | |
| HUGO_VERSION: 0.147.9 | |
| steps: | |
| - name: Install Hugo CLI | |
| if: github.event.action != 'closed' | |
| 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: github.event.action != 'closed' | |
| run: sudo snap install dart-sass | |
| - name: Checkout | |
| if: github.event.action != 'closed' | |
| uses: actions/checkout@v6 | |
| with: | |
| ref: ${{ github.event.pull_request.head.sha }} | |
| submodules: recursive | |
| fetch-depth: 0 | |
| - name: Checkout for cleanup | |
| if: github.event.action == 'closed' | |
| uses: actions/checkout@v6 | |
| with: | |
| ref: gh-pages | |
| fetch-depth: 0 | |
| - name: Install Node.js dependencies | |
| if: github.event.action != 'closed' | |
| run: "[[ -f package-lock.json || -f npm-shrinkwrap.json ]] && npm ci || true" | |
| - name: Build with Hugo | |
| if: github.event.action != 'closed' | |
| env: | |
| HUGO_ENVIRONMENT: production | |
| HUGO_ENV: production | |
| run: | | |
| REPO_NAME=$(echo "${{ github.repository }}" | cut -d'/' -f2) | |
| ORG_NAME=$(echo "${{ github.repository }}" | cut -d'/' -f1) | |
| PR_BASE_URL="https://${ORG_NAME}.github.io/${REPO_NAME}/pr-preview/pr-${{ github.event.pull_request.number }}/" | |
| echo "Building for BaseURL: ${PR_BASE_URL}" | |
| hugo \ | |
| --gc \ | |
| --minify \ | |
| --buildDrafts \ | |
| --buildFuture \ | |
| --baseURL "${PR_BASE_URL}" | |
| - name: Deploy PR Preview | |
| if: github.event.action != 'closed' | |
| id: deploy-preview | |
| uses: rossjrw/pr-preview-action@v1.6.3 | |
| with: | |
| source-dir: ./public | |
| preview-branch: gh-pages | |
| umbrella-dir: pr-preview | |
| action: auto | |
| comment: false | |
| - name: Comment PR with Preview URL | |
| if: github.event.action != 'closed' | |
| uses: marocchino/sticky-pull-request-comment@v2 | |
| with: | |
| header: pr-preview | |
| message: | | |
| 🚀 **Preview deployment for PR #${{ github.event.pull_request.number }}** | |
| 🌐 **Preview URL**: ${{ steps.deploy-preview.outputs.preview-url }} | |
| _This preview will be updated automatically when you push new commits to this PR._ | |
| - name: Cleanup PR Preview on Close | |
| if: github.event.action == 'closed' | |
| uses: rossjrw/pr-preview-action@v1.6.3 | |
| with: | |
| preview-branch: gh-pages | |
| umbrella-dir: pr-preview | |
| action: remove |