-
Notifications
You must be signed in to change notification settings - Fork 38
213 lines (180 loc) · 7.27 KB
/
Copy pathpreview-deploy.yml
File metadata and controls
213 lines (180 loc) · 7.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# 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.158.0
PREVIEW_RETENTION_LIMIT: 6
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: Checkout gh-pages for preview retention
if: github.event.action != 'closed'
uses: actions/checkout@v6
with:
ref: gh-pages
fetch-depth: 0
filter: blob:none
sparse-checkout: |
pr-preview
path: gh-pages-maintenance
- name: Prune old PR previews
id: prune-previews
if: github.event.action != 'closed'
run: |
cd gh-pages-maintenance
mkdir -p pr-preview
removed_prs=()
mapfile -t previews < <(
while IFS= read -r preview; do
timestamp="$(git log -1 --format=%ct -- "pr-preview/$preview" 2>/dev/null || echo 0)"
printf '%s %s\n' "$timestamp" "$preview"
done < <(find pr-preview -mindepth 1 -maxdepth 1 -type d -name 'pr-*' -printf '%f\n') \
| sort -nr \
| awk '{print $2}'
)
if (( ${#previews[@]} <= PREVIEW_RETENTION_LIMIT )); then
echo "removed_prs_json=[]" >> "$GITHUB_OUTPUT"
exit 0
fi
for preview in "${previews[@]:PREVIEW_RETENTION_LIMIT}"; do
rm -rf "pr-preview/$preview"
removed_prs+=("${preview#pr-}")
done
if git diff --quiet -- pr-preview; then
echo "removed_prs=" >> "$GITHUB_OUTPUT"
echo "removed_prs_json=[]" >> "$GITHUB_OUTPUT"
exit 0
fi
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
git add pr-preview
git commit -m "Prune old PR previews"
git push
echo "removed_prs=$(IFS=,; echo "${removed_prs[*]}")" >> "$GITHUB_OUTPUT"
echo "removed_prs_json=$(printf '%s\n' "${removed_prs[@]}" | jq -R . | jq -sc .)" >> "$GITHUB_OUTPUT"
- 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: ${{ steps.deploy-preview.outputs.preview-url }}
> *Note: Preview may take a moment (GitHub Pages deployment in progress). Please wait and refresh. Track deployment [here](https://github.com/${{ github.repository }}/actions/workflows/pages/pages-build-deployment)*
- name: Comment on pruned previews
if: github.event.action != 'closed' && steps.prune-previews.outputs.removed_prs_json != '[]'
uses: actions/github-script@v7
env:
REMOVED_PRS_JSON: ${{ steps.prune-previews.outputs.removed_prs_json }}
PREVIEW_RETENTION_LIMIT: ${{ env.PREVIEW_RETENTION_LIMIT }}
with:
script: |
const removedPrs = JSON.parse(process.env.REMOVED_PRS_JSON);
const retentionLimit = process.env.PREVIEW_RETENTION_LIMIT;
const header = "pr-preview";
const marker = `<!-- Sticky Pull Request Comment${header} -->`;
for (const prNumber of removedPrs) {
const body =
`Preview deployment for PR #${prNumber} removed.\n\n` +
`This PR preview was automatically pruned because we keep only the ${retentionLimit} most recently updated previews on GitHub Pages to stay within deployment size limits.\n\n` +
`If needed, push a new commit to this PR to generate a fresh preview.\n` +
`${marker}`;
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: Number(prNumber),
per_page: 100,
});
const existingComment = [...comments].reverse().find((comment) =>
comment.user?.login === "github-actions[bot]" &&
comment.body?.includes(marker)
);
if (existingComment) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: existingComment.id,
body,
});
continue;
}
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: Number(prNumber),
body,
});
}
- 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