From 004ce478440e727f3a9864badd6a886d8933e7af Mon Sep 17 00:00:00 2001 From: Xi Xu Date: Sun, 1 Feb 2026 13:20:22 +0800 Subject: [PATCH] feat: add step to ensure Cloudflare Pages project exists before deployment --- .github/workflows/pages-cf.yml | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/.github/workflows/pages-cf.yml b/.github/workflows/pages-cf.yml index 9cfef27..167e4ab 100644 --- a/.github/workflows/pages-cf.yml +++ b/.github/workflows/pages-cf.yml @@ -28,6 +28,33 @@ jobs: with: ref: pages + - name: Ensure Cloudflare Pages project exists + env: + CF_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }} + CF_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} + CF_PAGES_PROJECT: xget + CF_PRODUCTION_BRANCH: main + shell: bash + run: | + set -euo pipefail + + api_base="https://api.cloudflare.com/client/v4/accounts/${CF_ACCOUNT_ID}/pages/projects" + auth_header="Authorization: Bearer ${CF_API_TOKEN}" + + echo "Checking Pages project: ${CF_PAGES_PROJECT}" + if curl -fsS -H "${auth_header}" "${api_base}/${CF_PAGES_PROJECT}" >/dev/null; then + echo "Pages project exists." + exit 0 + fi + + echo "Pages project not found. Creating..." + create_payload="$(printf '{"name":"%s","production_branch":"%s"}' "${CF_PAGES_PROJECT}" "${CF_PRODUCTION_BRANCH}")" + curl -fsS -X POST -H "${auth_header}" -H "Content-Type: application/json" \ + --data "${create_payload}" \ + "${api_base}" >/dev/null + + echo "Pages project created." + - name: Deploy to Cloudflare Pages uses: cloudflare/wrangler-action@v3 with: