Skip to content

Commit b87a4c3

Browse files
author
Victor Viale
committed
Add branch previews of the website with cleanup on merge/close
1 parent 00fa5b6 commit b87a4c3

1 file changed

Lines changed: 54 additions & 2 deletions

File tree

.github/workflows/ci.yml

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,20 @@ name: Continuous Integration
22

33
on:
44
pull_request:
5-
branches: ['**']
5+
branches: ["**"]
6+
types: [opened, synchronize, reopened, closed]
67
push:
7-
branches: ['main']
8+
branches: ["main"]
9+
10+
env:
11+
BRANCH_NAME: ${{ github.head_ref || github.ref_name }}
12+
CLOUDFLARE_PAGES_PROJECT_NAME: typelevel-website
813

914
jobs:
1015
build:
1116
name: Build and Test
1217
runs-on: ubuntu-latest
18+
if: github.event.action != 'closed'
1319
steps:
1420
- uses: actions/checkout@v6
1521
- uses: actions/setup-java@v5
@@ -22,9 +28,55 @@ jobs:
2228
scala-cli-version: 1.12.2
2329
- run: scala-cli fmt --check .
2430
- run: scala-cli --server=false build.scala
31+
- name: Publish to Cloudflare Pages
32+
if: github.event_name == 'pull_request' && github.event.pull_request.merged != true
33+
uses: cloudflare/wrangler-action@v4
34+
with:
35+
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
36+
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
37+
gitHubToken: ${{ secrets.GITHUB_TOKEN }}
38+
command: pages deploy ./target --project-name=${{ env.CLOUDFLARE_PAGES_PROJECT_NAME }} --branch=${{ env.BRANCH_NAME }}
2539
- if: github.event_name != 'pull_request'
2640
uses: peaceiris/actions-gh-pages@v4.0.0
2741
with:
2842
github_token: ${{ secrets.GITHUB_TOKEN }}
2943
publish_dir: target
3044
cname: typelevel.org
45+
46+
cleanup-preview:
47+
name: Delete Cloudflare Pages preview deployments
48+
runs-on: ubuntu-latest
49+
if: github.event_name == 'pull_request' && github.event.action == 'closed'
50+
steps:
51+
- name: Delete deployments for this PR's branch
52+
env:
53+
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
54+
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
55+
run: |
56+
set -euo pipefail
57+
58+
API="https://api.cloudflare.com/client/v4/accounts/$CLOUDFLARE_ACCOUNT_ID/pages/projects/$CLOUDFLARE_PAGES_PROJECT_NAME/deployments"
59+
AUTH_HEADER="Authorization: Bearer $CLOUDFLARE_API_TOKEN"
60+
61+
page=1
62+
ids=()
63+
while :; do
64+
response=$(curl -sf -H "$AUTH_HEADER" "$API?page=$page&per_page=25")
65+
count=$(echo "$response" | jq '.result | length')
66+
[ "$count" -eq 0 ] && break
67+
while IFS= read -r id; do
68+
ids+=("$id")
69+
done < <(echo "$response" | jq -r --arg branch "$BRANCH_NAME" \
70+
'.result[] | select(.deployment_trigger.metadata.branch == $branch) | .id')
71+
page=$((page + 1))
72+
done
73+
74+
if [ ${#ids[@]} -eq 0 ]; then
75+
echo "No preview deployments found for branch $BRANCH_NAME"
76+
exit 0
77+
fi
78+
79+
for id in "${ids[@]}"; do
80+
echo "Deleting deployment $id"
81+
curl -sf -X DELETE -H "$AUTH_HEADER" "$API/$id?force=true"
82+
done

0 commit comments

Comments
 (0)