Skip to content

Commit 9d026f0

Browse files
authored
Merge pull request #678 from Koroeskohr/main
Add branch previews of the website with cleanup on merge/close
2 parents 00fa5b6 + 1ea1c41 commit 9d026f0

3 files changed

Lines changed: 119 additions & 3 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ name: Continuous Integration
22

33
on:
44
pull_request:
5-
branches: ['**']
5+
branches: ["**"]
66
push:
7-
branches: ['main']
7+
branches: ["main"]
88

99
jobs:
1010
build:
@@ -22,7 +22,27 @@ jobs:
2222
scala-cli-version: 1.12.2
2323
- run: scala-cli fmt --check .
2424
- run: scala-cli --server=false build.scala
25-
- if: github.event_name != 'pull_request'
25+
- name: Save PR number
26+
if: github.event_name == 'pull_request'
27+
run: echo "${{ github.event.pull_request.number }}" > pr_number.txt
28+
- name: Upload site artifact
29+
if: github.event_name == 'pull_request'
30+
uses: actions/upload-artifact@v4
31+
with:
32+
name: site
33+
path: target
34+
if-no-files-found: error
35+
retention-days: 30
36+
- name: Upload PR number artifact
37+
if: github.event_name == 'pull_request'
38+
uses: actions/upload-artifact@v4
39+
with:
40+
name: pr-number
41+
path: pr_number.txt
42+
if-no-files-found: error
43+
retention-days: 30
44+
- name: Publish to GitHub Pages
45+
if: github.event_name == 'push'
2646
uses: peaceiris/actions-gh-pages@v4.0.0
2747
with:
2848
github_token: ${{ secrets.GITHUB_TOKEN }}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
name: Cleanup PR Preview
2+
3+
on:
4+
pull_request_target:
5+
types: [closed]
6+
7+
permissions:
8+
contents: read
9+
10+
env:
11+
CLOUDFLARE_PAGES_PROJECT_NAME: typelevel-website
12+
BRANCH_NAME: pr-${{ github.event.pull_request.number }}
13+
14+
jobs:
15+
cleanup-preview:
16+
name: Delete Cloudflare Pages preview deployments
17+
runs-on: ubuntu-latest
18+
steps:
19+
- name: Delete deployments for this PR's branch
20+
env:
21+
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
22+
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
23+
run: |
24+
set -euo pipefail
25+
26+
API="https://api.cloudflare.com/client/v4/accounts/$CLOUDFLARE_ACCOUNT_ID/pages/projects/$CLOUDFLARE_PAGES_PROJECT_NAME/deployments"
27+
AUTH_HEADER="Authorization: Bearer $CLOUDFLARE_API_TOKEN"
28+
29+
page=1
30+
ids=()
31+
while :; do
32+
response=$(curl -sf -H "$AUTH_HEADER" "$API?page=$page&per_page=25")
33+
count=$(echo "$response" | jq '.result | length')
34+
[ "$count" -eq 0 ] && break
35+
while IFS= read -r id; do
36+
ids+=("$id")
37+
done < <(echo "$response" | jq -r --arg branch "$BRANCH_NAME" \
38+
'.result[] | select(.deployment_trigger.metadata.branch == $branch) | .id')
39+
page=$((page + 1))
40+
done
41+
42+
if [ ${#ids[@]} -eq 0 ]; then
43+
echo "No preview deployments found for branch $BRANCH_NAME"
44+
exit 0
45+
fi
46+
47+
for id in "${ids[@]}"; do
48+
echo "Deleting deployment $id"
49+
curl -sf -X DELETE -H "$AUTH_HEADER" "$API/$id?force=true"
50+
done
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
name: Deploy PR Preview
2+
3+
on:
4+
workflow_run:
5+
workflows: ["Continuous Integration"]
6+
types: [completed]
7+
8+
permissions:
9+
actions: read
10+
contents: read
11+
12+
env:
13+
CLOUDFLARE_PAGES_PROJECT_NAME: typelevel-website
14+
15+
jobs:
16+
deploy:
17+
name: Publish to Cloudflare Pages
18+
runs-on: ubuntu-latest
19+
if: >
20+
github.event.workflow_run.event == 'pull_request' &&
21+
github.event.workflow_run.conclusion == 'success'
22+
steps:
23+
- name: Download site artifact
24+
uses: actions/download-artifact@v4
25+
with:
26+
name: site
27+
path: site
28+
run-id: ${{ github.event.workflow_run.id }}
29+
github-token: ${{ secrets.GITHUB_TOKEN }}
30+
- name: Download PR number artifact
31+
uses: actions/download-artifact@v4
32+
with:
33+
name: pr-number
34+
path: .
35+
run-id: ${{ github.event.workflow_run.id }}
36+
github-token: ${{ secrets.GITHUB_TOKEN }}
37+
- name: Read PR number
38+
id: pr
39+
run: echo "branch=pr-$(cat pr_number.txt)" >> "$GITHUB_OUTPUT"
40+
- name: Deploy
41+
uses: cloudflare/wrangler-action@v4
42+
with:
43+
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
44+
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
45+
gitHubToken: ${{ secrets.GITHUB_TOKEN }}
46+
command: pages deploy ./site --project-name=${{ env.CLOUDFLARE_PAGES_PROJECT_NAME }} --branch=${{ steps.pr.outputs.branch }}

0 commit comments

Comments
 (0)