Skip to content

Commit c448687

Browse files
authored
fix(ci): create the Cloudflare Pages project on the first deploy (#15)
Refs #14 Signed-off-by: Bugs5382 <12115015+Bugs5382@users.noreply.github.com> Co-authored-by: Bugs5382 <12115015+Bugs5382@users.noreply.github.com>
1 parent e83e350 commit c448687

3 files changed

Lines changed: 48 additions & 13 deletions

File tree

‎.github/workflows/job-deploy-release.yaml‎

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ name: Deploy
55
# push, no pull_request, no workflow_dispatch. Publishing the release is the
66
# one and only "go live" action for this site.
77
#
8-
# The Cloudflare Pages project (cryptos-pki-www) and its custom domains
9-
# (cryptos-pki.com, www.cryptos-pki.com) are created by hand in the
10-
# Cloudflare dashboard, not by this workflow -- see README.md. The org
11-
# secrets CLOUDFLARE_API_TOKEN and CLOUDFLARE_ACCOUNT_ID must hold real
12-
# values before the first release is published; this workflow never prints
13-
# either one.
8+
# The workflow creates the Cloudflare Pages project (cryptos-pki-www) the
9+
# first time, when it doesn't exist yet. Its custom domains (cryptos-pki.com,
10+
# www.cryptos-pki.com) are still attached once by hand in the Cloudflare
11+
# dashboard -- see docs/deploy.md. The org secrets CLOUDFLARE_API_TOKEN and
12+
# CLOUDFLARE_ACCOUNT_ID must hold real values before the first release is
13+
# published; this workflow never prints either one.
1414
on:
1515
release:
1616
types: [published]
@@ -49,6 +49,37 @@ jobs:
4949
- name: Build (prerender)
5050
run: npm run build
5151

52+
- name: List the Cloudflare Pages projects
53+
id: projects
54+
uses: cloudflare/wrangler-action@953926a2e2182532811c01a25e53647d93bf07c0 # v4.1.3
55+
with:
56+
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
57+
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
58+
command: pages project list --json
59+
60+
# Checked first so every release can run this step safely: the project
61+
# is created once, on the first deploy, and left alone after that.
62+
- name: Check for the cryptos-pki-www project
63+
id: exists
64+
env:
65+
PROJECTS: ${{ steps.projects.outputs.command-output }}
66+
run: |
67+
set -euo pipefail
68+
# Wrangler may print its banner ahead of the JSON array.
69+
if printf '%s\n' "$PROJECTS" | sed -n '/^\[/,$p' | jq -e 'any(.[]; ."Project Name" == "cryptos-pki-www")' >/dev/null; then
70+
echo "found=true" >> "$GITHUB_OUTPUT"
71+
else
72+
echo "found=false" >> "$GITHUB_OUTPUT"
73+
fi
74+
75+
- name: Create the cryptos-pki-www project
76+
if: steps.exists.outputs.found == 'false'
77+
uses: cloudflare/wrangler-action@953926a2e2182532811c01a25e53647d93bf07c0 # v4.1.3
78+
with:
79+
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
80+
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
81+
command: pages project create cryptos-pki-www --production-branch=main
82+
5283
- name: Deploy build/client to Cloudflare Pages
5384
uses: cloudflare/wrangler-action@953926a2e2182532811c01a25e53647d93bf07c0 # v4.1.3
5485
with:

‎README.md‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ npm run typecheck # react-router typegen + tsc --noEmit
2525

2626
The site goes live on Cloudflare Pages (project `cryptos-pki-www`, domains `cryptos-pki.com` and
2727
`www.cryptos-pki.com`) the moment a GitHub Release is published here -- nothing else deploys it.
28-
See [`docs/deploy.md`](docs/deploy.md) for what that build contains, the Cloudflare side of the
29-
setup, and how to roll back.
28+
The first deploy creates the Pages project if it doesn't exist yet; the custom domains are attached
29+
once by hand in Cloudflare after that. See [`docs/deploy.md`](docs/deploy.md) for what that build
30+
contains, the Cloudflare side of the setup, and how to roll back.
3031

3132
## 🙏 Acknowledgements
3233

‎docs/deploy.md‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,14 @@ tag can't silently move the code I'm trusting with deploy credentials.
1515

1616
## Where it lives
1717

18-
- **Cloudflare Pages project:** `cryptos-pki-www`.
19-
- **Domains:** `cryptos-pki.com` and `www.cryptos-pki.com`. I attach both to the Pages project by
20-
hand in the Cloudflare dashboard (Custom domains); because the domains' DNS already runs on
21-
Cloudflare, attaching a domain creates its DNS record and certificate automatically. This
22-
workflow never touches DNS or creates the Pages project itself.
18+
- **Cloudflare Pages project:** `cryptos-pki-www`. Before it deploys, the workflow lists the
19+
account's Pages projects and creates `cryptos-pki-www` (production branch `main`) only when it
20+
isn't there yet, so the first published release sets the project up and every later release
21+
skips that step.
22+
- **Domains:** `cryptos-pki.com` and `www.cryptos-pki.com`. I attach both to the Pages project
23+
once, by hand, in the Cloudflare dashboard (Custom domains), after the first deploy has created
24+
the project. Because the domains' DNS already runs on Cloudflare, attaching a domain creates its
25+
DNS record and certificate automatically. The workflow never touches DNS or custom domains.
2326
- **Credentials:** the org secrets `CLOUDFLARE_API_TOKEN` and `CLOUDFLARE_ACCOUNT_ID`, scoped to
2427
Cloudflare Pages Edit on my account only. They live in GitHub org secrets, never in this repo or
2528
its workflow logs.

0 commit comments

Comments
 (0)