From d4538092702313651b7ad71027f7876943bf752d Mon Sep 17 00:00:00 2001 From: Mathias Vanden Auweele Date: Tue, 24 Nov 2026 22:11:13 +0000 Subject: [PATCH 1/4] ci: deploy to Cloudflare Pages with a preview per branch Adds a workflow that builds the site on every branch push and deploys it with wrangler to the Cloudflare Pages project named after the repository. main is the production deployment, other branches get a preview alias. The job skips itself while the Cloudflare secrets are missing. The GitHub Pages workflow stays in place until the switch is confirmed. --- .github/workflows/deploy-cloudflare.yml | 94 +++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 .github/workflows/deploy-cloudflare.yml diff --git a/.github/workflows/deploy-cloudflare.yml b/.github/workflows/deploy-cloudflare.yml new file mode 100644 index 00000000..2b854356 --- /dev/null +++ b/.github/workflows/deploy-cloudflare.yml @@ -0,0 +1,94 @@ +name: Deploy to Cloudflare Pages + +# Every branch gets its own preview at https://..pages.dev, +# main is the production deployment. The Cloudflare Pages project is named +# after the repository and is created on the first run. +on: + push: + branches: ["**"] + workflow_dispatch: + +permissions: + contents: read + deployments: write + +concurrency: + group: cloudflare-pages-${{ github.ref }} + cancel-in-progress: true + +jobs: + check: + runs-on: ubuntu-latest + outputs: + enabled: ${{ steps.check.outputs.enabled }} + steps: + - id: check + env: + CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }} + CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} + run: | + if [ -n "$CLOUDFLARE_API_TOKEN" ] && [ -n "$CLOUDFLARE_ACCOUNT_ID" ]; then + echo "enabled=true" >> "$GITHUB_OUTPUT" + else + echo "::notice::CLOUDFLARE_API_TOKEN or CLOUDFLARE_ACCOUNT_ID is not available (not set yet, or a Dependabot run); skipping the Cloudflare Pages deployment." + echo "enabled=false" >> "$GITHUB_OUTPUT" + fi + + deploy: + needs: check + if: needs.check.outputs.enabled == 'true' + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v5 + + - uses: actions/setup-node@v6 + with: + node-version: "lts/*" + cache: npm + cache-dependency-path: | + package-lock.json + website/package-lock.json + + - name: Install dependencies + run: npm ci + + - name: Disable AppArmor + run: echo 0 | sudo tee /proc/sys/kernel/apparmor_restrict_unprivileged_userns + + - name: Build MatGUI + run: npm run build + env: + PUBLIC_PATH: /dev/main/ + + - name: Build documentation + run: | + cp -r docs/* website/docs/ + cd website + npm ci + npm run build + + - name: Prepare deployment directory + run: | + mkdir -p deployment/dev/main + cp -r website/build/* deployment/ + cp -r build/* deployment/dev/main/ + + - name: Project name + id: project + run: echo "name=$(echo '${{ github.event.repository.name }}' | tr '[:upper:]' '[:lower:]')" >> "$GITHUB_OUTPUT" + + - name: Deploy to Cloudflare Pages + id: deploy + uses: cloudflare/wrangler-action@v4 + with: + apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }} + accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} + gitHubToken: ${{ secrets.GITHUB_TOKEN }} + preCommands: wrangler pages project create ${{ steps.project.outputs.name }} --production-branch=main || true + command: pages deploy deployment --project-name=${{ steps.project.outputs.name }} --branch=${{ github.ref_name }} --commit-hash=${{ github.sha }} --commit-dirty=true + + - name: Preview URLs + run: | + echo "### Cloudflare Pages" >> "$GITHUB_STEP_SUMMARY" + echo "- Branch alias: ${{ steps.deploy.outputs.pages-deployment-alias-url }}" >> "$GITHUB_STEP_SUMMARY" + echo "- This deployment: ${{ steps.deploy.outputs.deployment-url }}" >> "$GITHUB_STEP_SUMMARY" From 58f36c890b6d75517b9357bcd27b4c9e32ac2ae4 Mon Sep 17 00:00:00 2001 From: Mathias Vanden Auweele Date: Tue, 24 Nov 2026 22:25:29 +0000 Subject: [PATCH 2/4] ci: retry creating the Cloudflare Pages project The Cloudflare API can fail the create call with code 8000000, which left the project missing and the deploy failing. --- .github/workflows/deploy-cloudflare.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/deploy-cloudflare.yml b/.github/workflows/deploy-cloudflare.yml index 2b854356..fc6b64be 100644 --- a/.github/workflows/deploy-cloudflare.yml +++ b/.github/workflows/deploy-cloudflare.yml @@ -84,7 +84,9 @@ jobs: apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }} accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} gitHubToken: ${{ secrets.GITHUB_TOKEN }} - preCommands: wrangler pages project create ${{ steps.project.outputs.name }} --production-branch=main || true + # Create the project on the first run. The Cloudflare API sometimes fails + # this call (code 8000000), so retry, and accept "already exists". + preCommands: for i in 1 2 3; do out=$(npx wrangler pages project create ${{ steps.project.outputs.name }} --production-branch=main 2>&1) && break; echo "$out"; echo "$out" | grep -qi "already exists" && break; sleep 15; done command: pages deploy deployment --project-name=${{ steps.project.outputs.name }} --branch=${{ github.ref_name }} --commit-hash=${{ github.sha }} --commit-dirty=true - name: Preview URLs From 8715b10c9d3024c362ccf02b5dd576ff1df76c36 Mon Sep 17 00:00:00 2001 From: Mathias Vanden Auweele Date: Thu, 26 Nov 2026 12:34:12 +0000 Subject: [PATCH 3/4] ci: delete a branch's Cloudflare Pages previews when the branch is deleted --- .../workflows/cleanup-cloudflare-previews.yml | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 .github/workflows/cleanup-cloudflare-previews.yml diff --git a/.github/workflows/cleanup-cloudflare-previews.yml b/.github/workflows/cleanup-cloudflare-previews.yml new file mode 100644 index 00000000..a18b2da9 --- /dev/null +++ b/.github/workflows/cleanup-cloudflare-previews.yml @@ -0,0 +1,58 @@ +name: Clean up Cloudflare Pages previews + +# When a branch is deleted, remove that branch's preview deployments from the +# Cloudflare Pages project (named after the repository), so old previews stop +# being reachable. Production deployments of main are never touched. +on: + delete: + +permissions: {} + +jobs: + cleanup: + if: github.event.ref_type == 'branch' + runs-on: ubuntu-latest + steps: + - name: Delete the branch's preview deployments + env: + CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }} + CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} + BRANCH: ${{ github.event.ref }} + REPOSITORY: ${{ github.event.repository.name }} + run: | + if [ -z "$CLOUDFLARE_API_TOKEN" ] || [ -z "$CLOUDFLARE_ACCOUNT_ID" ]; then + echo "::notice::Cloudflare secrets are not available; nothing to clean up." + exit 0 + fi + project=$(echo "$REPOSITORY" | tr '[:upper:]' '[:lower:]') + api="https://api.cloudflare.com/client/v4/accounts/$CLOUDFLARE_ACCOUNT_ID/pages/projects/$project/deployments" + auth="Authorization: Bearer $CLOUDFLARE_API_TOKEN" + + # Collect first, delete after: deleting while paging would shift the pages. + : > ids.txt + page=1 + while :; do + body=$(curl -sS -H "$auth" "$api?env=preview&per_page=25&page=$page") + if [ "$(echo "$body" | jq -r '.success')" != "true" ]; then + if echo "$body" | jq -r '.errors[].message' | grep -qiE 'not found|does not exist'; then + echo "::notice::Cloudflare Pages project '$project' does not exist; nothing to clean up." + exit 0 + fi + echo "$body" | jq '.errors' + exit 1 + fi + count=$(echo "$body" | jq '.result | length') + [ "$count" -eq 0 ] && break + echo "$body" | jq -r --arg b "$BRANCH" \ + '.result[] | select(.deployment_trigger.metadata.branch == $b) | .id' >> ids.txt + page=$((page + 1)) + done + + total=$(wc -l < ids.txt) + echo "Found $total preview deployment(s) of '$BRANCH' in '$project'." + while read -r id; do + curl -fsS -X DELETE -H "$auth" "$api/$id?force=true" > /dev/null + echo "Deleted $id" + done < ids.txt + echo "### Cloudflare Pages cleanup" >> "$GITHUB_STEP_SUMMARY" + echo "Deleted $total preview deployment(s) of \`$BRANCH\` from \`$project\`." >> "$GITHUB_STEP_SUMMARY" From d4afce49760c3bcbed5c18c85d3f290fab5824f3 Mon Sep 17 00:00:00 2001 From: Mathias Vanden Auweele Date: Thu, 26 Nov 2026 13:25:33 +0000 Subject: [PATCH 4/4] ci: deploy to the matgui-doc Cloudflare Pages project Cloudflare Pages projects can't be renamed, so the workflows now use the project name matgui-doc instead of the repository name. The project is created on the next push; the old matgui project can then be deleted. --- .github/workflows/cleanup-cloudflare-previews.yml | 5 ++--- .github/workflows/deploy-cloudflare.yml | 6 +++--- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/.github/workflows/cleanup-cloudflare-previews.yml b/.github/workflows/cleanup-cloudflare-previews.yml index a18b2da9..742d3c98 100644 --- a/.github/workflows/cleanup-cloudflare-previews.yml +++ b/.github/workflows/cleanup-cloudflare-previews.yml @@ -1,7 +1,7 @@ name: Clean up Cloudflare Pages previews # When a branch is deleted, remove that branch's preview deployments from the -# Cloudflare Pages project (named after the repository), so old previews stop +# matgui-doc Cloudflare Pages project, so old previews stop # being reachable. Production deployments of main are never touched. on: delete: @@ -18,13 +18,12 @@ jobs: CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }} CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} BRANCH: ${{ github.event.ref }} - REPOSITORY: ${{ github.event.repository.name }} run: | if [ -z "$CLOUDFLARE_API_TOKEN" ] || [ -z "$CLOUDFLARE_ACCOUNT_ID" ]; then echo "::notice::Cloudflare secrets are not available; nothing to clean up." exit 0 fi - project=$(echo "$REPOSITORY" | tr '[:upper:]' '[:lower:]') + project=matgui-doc api="https://api.cloudflare.com/client/v4/accounts/$CLOUDFLARE_ACCOUNT_ID/pages/projects/$project/deployments" auth="Authorization: Bearer $CLOUDFLARE_API_TOKEN" diff --git a/.github/workflows/deploy-cloudflare.yml b/.github/workflows/deploy-cloudflare.yml index fc6b64be..a38924fc 100644 --- a/.github/workflows/deploy-cloudflare.yml +++ b/.github/workflows/deploy-cloudflare.yml @@ -1,8 +1,8 @@ name: Deploy to Cloudflare Pages # Every branch gets its own preview at https://..pages.dev, -# main is the production deployment. The Cloudflare Pages project is named -# after the repository and is created on the first run. +# main is the production deployment. The Cloudflare Pages project is called +# matgui-doc (it serves matgui-doc.matdata.eu) and is created on the first run. on: push: branches: ["**"] @@ -75,7 +75,7 @@ jobs: - name: Project name id: project - run: echo "name=$(echo '${{ github.event.repository.name }}' | tr '[:upper:]' '[:lower:]')" >> "$GITHUB_OUTPUT" + run: echo "name=matgui-doc" >> "$GITHUB_OUTPUT" - name: Deploy to Cloudflare Pages id: deploy