Skip to content

Commit 42ed01c

Browse files
committed
Add pull request pre-release builds and publishing
Enhance CI workflow to automatically build and publish pre-release versions for pull requests with versioning scheme including PR number, build number, and commit hash. Add automatic PR comments to notify about published pre-releases and update documentation.
1 parent 67c26e1 commit 42ed01c

2 files changed

Lines changed: 123 additions & 32 deletions

File tree

.github/workflows/build.yaml

Lines changed: 113 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: build
2-
on: [push, pull_request]
2+
on: [push, pull_request_target]
33

44
jobs:
55
test:
@@ -14,7 +14,13 @@ jobs:
1414
checks: write
1515
pull-requests: write
1616
steps:
17-
- uses: actions/checkout@v4
17+
- if: github.event_name == 'push'
18+
uses: actions/checkout@v4
19+
- if: github.event_name == 'pull_request_target'
20+
uses: actions/checkout@v4
21+
with:
22+
repository: ${{ github.event.pull_request.head.repo.full_name }}
23+
ref: ${{ github.event.pull_request.head.sha }}
1824
- uses: denoland/setup-deno@v2
1925
with:
2026
deno-version: v2.x
@@ -54,7 +60,13 @@ jobs:
5460
fail-fast: false
5561
runs-on: ${{ matrix.os }}
5662
steps:
57-
- uses: actions/checkout@v4
63+
- if: github.event_name == 'push'
64+
uses: actions/checkout@v4
65+
- if: github.event_name == 'pull_request_target'
66+
uses: actions/checkout@v4
67+
with:
68+
repository: ${{ github.event.pull_request.head.repo.full_name }}
69+
ref: ${{ github.event.pull_request.head.sha }}
5870
- uses: denoland/setup-deno@v2
5971
with:
6072
deno-version: v2.x
@@ -74,7 +86,13 @@ jobs:
7486
fail-fast: false
7587
runs-on: ${{ matrix.os }}
7688
steps:
77-
- uses: actions/checkout@v4
89+
- if: github.event_name == 'push'
90+
uses: actions/checkout@v4
91+
- if: github.event_name == 'pull_request_target'
92+
uses: actions/checkout@v4
93+
with:
94+
repository: ${{ github.event.pull_request.head.repo.full_name }}
95+
ref: ${{ github.event.pull_request.head.sha }}
7896
- uses: denoland/setup-deno@v2
7997
with:
8098
deno-version: v2.x
@@ -93,7 +111,13 @@ jobs:
93111
lint:
94112
runs-on: ubuntu-latest
95113
steps:
96-
- uses: actions/checkout@v4
114+
- if: github.event_name == 'push'
115+
uses: actions/checkout@v4
116+
- if: github.event_name == 'pull_request_target'
117+
uses: actions/checkout@v4
118+
with:
119+
repository: ${{ github.event.pull_request.head.repo.full_name }}
120+
ref: ${{ github.event.pull_request.head.sha }}
97121
- uses: denoland/setup-deno@v2
98122
with:
99123
deno-version: v2.x
@@ -105,7 +129,13 @@ jobs:
105129
id-token: write
106130
contents: read
107131
steps:
108-
- uses: actions/checkout@v4
132+
- if: github.event_name == 'push'
133+
uses: actions/checkout@v4
134+
- if: github.event_name == 'pull_request_target'
135+
uses: actions/checkout@v4
136+
with:
137+
repository: ${{ github.event.pull_request.head.repo.full_name }}
138+
ref: ${{ github.event.pull_request.head.sha }}
109139
- uses: denoland/setup-deno@v2
110140
with:
111141
deno-version: v2.x
@@ -134,8 +164,18 @@ jobs:
134164
permissions:
135165
id-token: write
136166
contents: write
167+
pull-requests: write
168+
outputs:
169+
version: ${{ steps.versioning.outputs.version }}
170+
short_version: ${{ steps.versioning.outputs.short_version }}
137171
steps:
138-
- uses: actions/checkout@v4
172+
- if: github.event_name == 'push'
173+
uses: actions/checkout@v4
174+
- if: github.event_name == 'pull_request_target'
175+
uses: actions/checkout@v4
176+
with:
177+
repository: ${{ github.event.pull_request.head.repo.full_name }}
178+
ref: ${{ github.event.pull_request.head.sha }}
139179
- uses: denoland/setup-deno@v2
140180
with:
141181
deno-version: v2.x
@@ -145,21 +185,35 @@ jobs:
145185
- uses: pnpm/action-setup@v4
146186
with:
147187
version: 10
148-
- if: github.ref_type == 'branch'
188+
- if: github.event_name == 'push' && github.ref_type == 'branch'
149189
run: |
150190
jq \
151191
--arg build "$GITHUB_RUN_NUMBER" \
152192
--arg commit "${GITHUB_SHA::8}" \
153193
'.version = .version + "-dev." + $build + "+" + $commit' \
154194
deno.json > deno.json.tmp
155195
mv deno.json.tmp deno.json
196+
working-directory: ${{ github.workspace }}/fedify/
197+
- if: github.event_name == 'pull_request_target'
198+
run: |
156199
jq \
200+
--arg pr_number "$PR_NUMBER" \
157201
--arg build "$GITHUB_RUN_NUMBER" \
158-
--arg commit "${GITHUB_SHA::8}" \
159-
'.version = .version + "-dev." + $build + "+" + $commit' \
160-
package.json > package.json.tmp
161-
mv package.json.tmp package.json
202+
--arg commit "${PR_SHA::8}" \
203+
'.version = .version + "-pr." + $pr_number + "." + $build + "+" + $commit' \
204+
deno.json > deno.json.tmp
205+
mv deno.json.tmp deno.json
162206
working-directory: ${{ github.workspace }}/fedify/
207+
env:
208+
PR_NUMBER: ${{ github.event.pull_request.number }}
209+
PR_SHA: ${{ github.event.pull_request.head.sha }}
210+
- id: versioning
211+
run: |
212+
set -ex
213+
echo version="$(jq -r .version deno.json)" >> $GITHUB_OUTPUT
214+
echo short_version="$(jq -r .version deno.json | sed 's/[+].*//')" >> $GITHUB_OUTPUT
215+
working-directory: ${{ github.workspace }}/fedify/
216+
- run: deno task -r sync-version
163217
- if: github.ref_type == 'tag'
164218
run: |
165219
set -ex
@@ -202,29 +256,27 @@ jobs:
202256
fedify/fedify-fedify-*.tgz
203257
cli/fedify-cli-*
204258
generate_release_notes: false
205-
- if: |
206-
github.event_name == 'push' &&
207-
github.ref_type == 'tag' || github.ref == 'refs/heads/main'
208-
run: deno task publish --allow-dirty
259+
- run: deno task publish --allow-dirty
209260
working-directory: ${{ github.workspace }}/fedify/
210-
- if: |
211-
github.event_name == 'push' &&
212-
github.ref_type == 'tag' || github.ref == 'refs/heads/main'
213-
run: deno task publish --allow-dirty
261+
- run: deno task publish --allow-dirty
214262
working-directory: ${{ github.workspace }}/cli/
215-
- if: |
216-
github.event_name == 'push' &&
217-
github.ref_type == 'tag' || github.ref == 'refs/heads/main'
218-
run: |
263+
- run: |
219264
set -ex
220265
npm config set //registry.npmjs.org/:_authToken "$NPM_AUTH_TOKEN"
221266
if [[ "$GITHUB_REF_TYPE" = "tag" ]]; then
222267
npm publish --provenance --access public fedify-fedify-*.tgz
268+
elif [[ "$GITHUB_EVENT_NAME" = "pull_request_target" ]]; then
269+
npm publish \
270+
--provenance \
271+
--access public \
272+
--tag "pr-$PR_NUMBER" \
273+
fedify-fedify-*.tgz
223274
else
224275
npm publish --provenance --access public --tag dev fedify-fedify-*.tgz
225276
fi
226277
env:
227278
NPM_AUTH_TOKEN: ${{ secrets.NPM_AUTH_TOKEN }}
279+
PR_NUMBER: ${{ github.event.pull_request.number }}
228280
working-directory: ${{ github.workspace }}/fedify/
229281
- if: github.event_name == 'push' && github.ref_type == 'tag'
230282
run: |
@@ -234,6 +286,20 @@ jobs:
234286
env:
235287
NPM_AUTH_TOKEN: ${{ secrets.NPM_AUTH_TOKEN }}
236288
working-directory: ${{ github.workspace }}/cli/
289+
- if: github.event_name == 'pull_request_target'
290+
uses: thollander/actions-comment-pull-request@v3
291+
with:
292+
message: |
293+
The latest push to this pull request has been published to JSR and npm as a pre-release:
294+
295+
- [`jsr:@fedify/fedify@${{ steps.versioning.outputs.version }}`][1]
296+
- [`jsr:@fedify/cli@${{ steps.versioning.outputs.version }}`][2]
297+
- [`npm:@fedify/fedify@${{ steps.versioning.outputs.short_version }}`][3]
298+
299+
[1]: https://jsr.io/@fedify/fedify@${{ steps.versioning.outputs.version }}
300+
[2]: https://jsr.io/@fedify/cli@${{ steps.versioning.outputs.version }}
301+
[3]: https://www.npmjs.com/package/@fedify/fedify/v/${{ steps.versioning.outputs.short_version }}
302+
pr-number: ${{ github.event.pull_request.number }}
237303

238304
publish-examples-blog:
239305
if: github.event_name == 'push'
@@ -243,7 +309,13 @@ jobs:
243309
id-token: write
244310
contents: read
245311
steps:
246-
- uses: actions/checkout@v4
312+
- if: github.event_name == 'push'
313+
uses: actions/checkout@v4
314+
- if: github.event_name == 'pull_request_target'
315+
uses: actions/checkout@v4
316+
with:
317+
repository: ${{ github.event.pull_request.head.repo.full_name }}
318+
ref: ${{ github.event.pull_request.head.sha }}
247319
- uses: denoland/setup-deno@v2
248320
with:
249321
deno-version: v2.x
@@ -256,40 +328,46 @@ jobs:
256328
root: .
257329

258330
publish-docs:
259-
if: github.event_name == 'push'
260331
needs: [publish]
261332
runs-on: ubuntu-latest
262333
permissions:
263334
id-token: write
264335
pages: write
265336
deployments: write
337+
pull-requests: write
266338
environment:
267339
name: github-pages
268340
url: ${{ steps.deployment.outputs.page_url }}
269341
steps:
270-
- uses: actions/checkout@v4
342+
- if: github.event_name == 'push'
343+
uses: actions/checkout@v4
344+
- if: github.event_name == 'pull_request_target'
345+
uses: actions/checkout@v4
346+
with:
347+
repository: ${{ github.event.pull_request.head.repo.full_name }}
348+
ref: ${{ github.event.pull_request.head.sha }}
271349
- uses: pnpm/action-setup@v4
272350
with:
273351
version: 10
274352
- run: |
275353
set -ex
276354
pnpm install
355+
pnpm add -D "@fedify/fedify@$SHORT_VERSION"
277356
if [[ "$GITHUB_EVENT_NAME" = "push" && "$GITHUB_REF_TYPE" = "tag" ]]; then
278-
pnpm add -D "@fedify/fedify@$GITHUB_REF_NAME"
279357
EXTRA_NAV_TEXT=Unstable \
280358
EXTRA_NAV_LINK="$UNSTABLE_DOCS_URL" \
281359
SITEMAP_HOSTNAME="$STABLE_DOCS_URL" \
282360
JSR_REF_VERSION=stable \
283361
pnpm run build
284362
else
285-
pnpm add -D @fedify/fedify@dev
286363
EXTRA_NAV_TEXT=Stable \
287364
EXTRA_NAV_LINK="$STABLE_DOCS_URL" \
288365
SITEMAP_HOSTNAME="$UNSTABLE_DOCS_URL" \
289366
JSR_REF_VERSION=unstable \
290367
pnpm run build
291368
fi
292369
env:
370+
SHORT_VERSION: ${{ needs.publish.outputs.short_version }}
293371
PLAUSIBLE_DOMAIN: ${{ secrets.PLAUSIBLE_DOMAIN }}
294372
STABLE_DOCS_URL: ${{ vars.STABLE_DOCS_URL }}
295373
UNSTABLE_DOCS_URL: ${{ vars.UNSTABLE_DOCS_URL }}
@@ -300,17 +378,20 @@ jobs:
300378
- id: deployment
301379
if: github.event_name == 'push' && github.ref_type == 'tag'
302380
uses: actions/deploy-pages@v4
303-
- if: github.event_name == 'pull_request' || github.ref_type == 'branch'
381+
- if: github.event_name == 'pull_request_target' || github.ref_type == 'branch'
304382
uses: nwtgck/actions-netlify@v3.0
305383
with:
306384
publish-dir: docs/.vitepress/dist
307385
production-branch: main
386+
production-deploy:
387+
${{ github.event_name == 'push' && github.ref == 'refs/heads/main' && 'true' || 'false' }}
308388
github-token: ${{ github.token }}
309-
enable-pull-request-comment: false
389+
enable-pull-request-comment: true
310390
enable-commit-comment: false
391+
overwrites-pull-request-comment: true
311392
env:
312393
NETLIFY_AUTH_TOKEN: ${{ secrets.NETLIFY_AUTH_TOKEN }}
313394
NETLIFY_SITE_ID: ${{ secrets.NETLIFY_SITE_ID }}
314395
timeout-minutes: 2
315396

316-
# cSpell: ignore submark softprops npmjs deployctl nwtgck
397+
# cSpell: ignore submark softprops npmjs deployctl nwtgck thollander elif

CONTRIBUTING.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,16 @@ A patch set should include the following:
131131

132132
Feature pull requests should target the *main* branch.
133133

134+
### Pull request builds
135+
136+
Each pull request is automatically built and published to the JSR and npm
137+
registries as a pre-release. You can test the pull request by installing
138+
the pre-release version of the Fedify library. The version number of
139+
the pre-release version consists of the base version number, the pull request
140+
number, the build number, and the commit hash, which looks like
141+
`1.2.3-pr.456.789+abcdef01`. You can find the exact version number in
142+
the comment left by the build process in the pull request.
143+
134144

135145
Build
136146
-----

0 commit comments

Comments
 (0)