11# Workflow for building and deploying Hugo PR previews to GitHub Pages
22name : preview-deploy
33
4-
54on :
65 pull_request_target :
76 branches : [ "master" ]
8- types : [opened, synchronize, reopened]
9-
7+ types : [opened, synchronize, reopened, closed]
108
119 workflow_dispatch :
1210
13-
1411# Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages and comments
1512permissions :
1613 contents : write
1714 pages : write
1815 pull-requests : write
1916
20-
2117concurrency :
2218 group : " pages-${{ github.event.pull_request.number || github.run_id }}"
2319 cancel-in-progress : true
2420
25-
2621# Default to bash
2722defaults :
2823 run :
2924 shell : bash
3025
31-
3226jobs :
33- # Build and Deploy job
3427 build-and-deploy :
3528 runs-on : ubuntu-latest
3629 env :
3730 HUGO_VERSION : 0.147.9
31+
3832 steps :
3933 - name : Install Hugo CLI
34+ if : github.event.action != 'closed'
4035 run : |
4136 wget -O ${{ runner.temp }}/hugo.deb https://github.com/gohugoio/hugo/releases/download/v${HUGO_VERSION}/hugo_extended_${HUGO_VERSION}_linux-amd64.deb \
42- && sudo dpkg -i ${{ runner.temp }}/hugo.deb
37+ && sudo dpkg -i ${{ runner.temp }}/hugo.deb
4338
4439 - name : Install Dart Sass
40+ if : github.event.action != 'closed'
4541 run : sudo snap install dart-sass
4642
47-
4843 - name : Checkout
44+ if : github.event.action != 'closed'
4945 uses : actions/checkout@v6
5046 with :
5147 ref : ${{ github.event.pull_request.head.sha }}
5248 submodules : recursive
5349 fetch-depth : 0
5450
51+ - name : Checkout for cleanup
52+ if : github.event.action == 'closed'
53+ uses : actions/checkout@v6
54+ with :
55+ ref : gh-pages
56+ fetch-depth : 0
5557
5658 - name : Install Node.js dependencies
59+ if : github.event.action != 'closed'
5760 run : " [[ -f package-lock.json || -f npm-shrinkwrap.json ]] && npm ci || true"
5861
59-
6062 - name : Build with Hugo
63+ if : github.event.action != 'closed'
6164 env :
62- # For maximum backward compatibility with Hugo modules
6365 HUGO_ENVIRONMENT : production
6466 HUGO_ENV : production
6567 run : |
66- # Dynamically set BaseURL for PR preview
67- # Format: https://<ORG>.github.io/<REPO>/pr-preview/pr-<NUMBER>/
68- # We use github context to make it generic
6968 REPO_NAME=$(echo "${{ github.repository }}" | cut -d'/' -f2)
7069 ORG_NAME=$(echo "${{ github.repository }}" | cut -d'/' -f1)
71-
70+
7271 PR_BASE_URL="https://${ORG_NAME}.github.io/${REPO_NAME}/pr-preview/pr-${{ github.event.pull_request.number }}/"
73-
72+
7473 echo "Building for BaseURL: ${PR_BASE_URL}"
75-
74+
7675 hugo \
7776 --gc \
7877 --minify \
7978 --buildDrafts \
8079 --buildFuture \
81- --baseURL "${PR_BASE_URL}"
82-
80+ --baseURL "${PR_BASE_URL}"
81+
8382 - name : Deploy PR Preview
83+ if : github.event.action != 'closed'
8484 id : deploy-preview
8585 uses : rossjrw/pr-preview-action@v1.6.3
8686 with :
@@ -90,15 +90,22 @@ jobs:
9090 action : auto
9191 comment : false
9292
93-
9493 - name : Comment PR with Preview URL
94+ if : github.event.action != 'closed'
9595 uses : marocchino/sticky-pull-request-comment@v2
96- if : github.event_name == 'pull_request_target'
9796 with :
9897 header : pr-preview
9998 message : |
10099 🚀 **Preview deployment for PR #${{ github.event.pull_request.number }}**
101100
102101 🌐 **Preview URL**: ${{ steps.deploy-preview.outputs.preview-url }}
103102
104- _This preview will be updated automatically when you push new commits to this PR._
103+ _This preview will be updated automatically when you push new commits to this PR._
104+
105+ - name : Cleanup PR Preview on Close
106+ if : github.event.action == 'closed'
107+ uses : rossjrw/pr-preview-action@v1.6.3
108+ with :
109+ preview-branch : gh-pages
110+ umbrella-dir : pr-preview
111+ action : remove
0 commit comments