Diffinery

homeflare/diffinerypublic

Open-source, agent-first GitHub and GitHub Actions alternative that runs on your own Cloudflare account: git hosting, pull requests, code review and drop-in GitHub Actions YAML on Workers, Durable Objects, R2 and Containers.

193 lines · 9.8 KBRaw History
1name: Deploy
2
3# Deploys Diffinery from main. Uses the repository secrets
4# CLOUDFLARE_API_TOKEN and CLOUDFLARE_ACCOUNT_ID, and these optional repository
5# variables (the same ones scripts/bootstrap.sh reads):
6# DIFFINERY_DOMAIN where it is served (default diffinery.com)
7# DIFFINERY_ZONE the Cloudflare zone for the route (default: the domain's last two labels)
8# MAIL_DOMAIN sends from notifications@<MAIL_DOMAIN> (default DIFFINERY_DOMAIN)
9# ACCESS_TEAM_DOMAIN e.g. example.cloudflareaccess.com
10# DIFFINERY_GITHUB_APP_ID, DIFFINERY_GITHUB_APP_SLUG, DIFFINERY_GITHUB_APP_CLIENT_ID,
11# DIFFINERY_GITHUB_APP_URL the GitHub App of GitHub runners (docs/self-hosting/configuration.md)
12#
13# It deploys a main commit only once CI (typecheck, unit tests and the Worker bundle) has
14# passed on it, so it doesn't run those itself. End-to-end tests run alongside the deploy.
15# Only main's current tip deploys: re-running an old CI run (which anyone with write access
16# can do) doesn't bring back a version from before a fix.
17#
18# The Cloudflare secrets are the production environment's, and only the steps that talk to
19# Cloudflare get them: installs, builds and third-party actions run without them.
20on:
21 workflow_run:
22 workflows: [CI]
23 types: [completed]
24 branches: [main]
25 workflow_dispatch:
26
27permissions:
28 contents: read
29
30concurrency:
31 group: deploy-production
32 cancel-in-progress: false
33
34jobs:
35 gate:
36 name: Main's current tip
37 # Every push to main deploys once CI passes on it, on GitHub or on a Diffinery
38 # instance, and so does running this workflow by hand.
39 if: ${{ github.event_name == 'workflow_dispatch' || (github.event.workflow_run.conclusion == 'success' && github.event.workflow_run.event == 'push') }}
40 runs-on: ubuntu-latest
41 timeout-minutes: 5
42 outputs:
43 deploy: ${{ steps.tip.outputs.deploy }}
44 steps:
45 - name: Deploy only main's current tip
46 id: tip
47 env:
48 GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
49 WANT: ${{ github.event.workflow_run.head_sha || github.sha }}
50 REF: ${{ github.ref }}
51 run: |
52 set -euo pipefail
53 if [ "$GITHUB_EVENT_NAME" = workflow_dispatch ] && [ "$REF" != refs/heads/main ]; then
54 echo "::error::Deploy runs from main only (this run is on $REF)"; exit 1
55 fi
56 tip=$(curl -fsS "$GITHUB_API_URL/repos/$GITHUB_REPOSITORY/branches/main" -H "Authorization: Bearer $GH_TOKEN" -H "Accept: application/vnd.github+json" | jq -r .commit.sha)
57 if [ "$tip" = "$WANT" ]; then
58 echo "deploy=true" >> "$GITHUB_OUTPUT"
59 echo "deploying $WANT, main's tip"
60 else
61 # A later commit's CI run deploys it: a re-run of an old CI run deploys nothing.
62 echo "deploy=false" >> "$GITHUB_OUTPUT"
63 echo "::notice::not deploying $WANT: main is at $tip now, and that commit's own run deploys it"
64 fi
65
66 deploy:
67 name: Deploy to Cloudflare
68 needs: gate
69 if: ${{ needs.gate.outputs.deploy == 'true' }}
70 runs-on: ubuntu-latest
71 timeout-minutes: 45
72 environment:
73 name: production
74 url: https://${{ vars.DIFFINERY_DOMAIN || 'diffinery.com' }}
75 env:
76 DIFFINERY_DOMAIN: ${{ vars.DIFFINERY_DOMAIN || 'diffinery.com' }}
77 DIFFINERY_ZONE: ${{ vars.DIFFINERY_ZONE }}
78 MAIL_DOMAIN: ${{ vars.MAIL_DOMAIN }}
79 ACCESS_TEAM_DOMAIN: ${{ vars.ACCESS_TEAM_DOMAIN }}
80 # Who becomes site administrator on first sign-in. Unset keeps the deployed Worker's list (scripts/bootstrap-admins.sh).
81 BOOTSTRAP_ADMINS: ${{ vars.BOOTSTRAP_ADMINS }}
82 # The GitHub App's IDs, for scripts/render-config.mjs. Repository variables can't start with GITHUB_.
83 GITHUB_APP_ID: ${{ vars.DIFFINERY_GITHUB_APP_ID }}
84 GITHUB_APP_SLUG: ${{ vars.DIFFINERY_GITHUB_APP_SLUG }}
85 GITHUB_APP_CLIENT_ID: ${{ vars.DIFFINERY_GITHUB_APP_CLIENT_ID }}
86 GITHUB_APP_URL: ${{ vars.DIFFINERY_GITHUB_APP_URL }}
87 steps:
88 - uses: actions/checkout@v4
89 with:
90 # The commit CI passed on, not whatever main is by now.
91 ref: ${{ github.event.workflow_run.head_sha || github.sha }}
92 - uses: pnpm/action-setup@b906affcce14559ad1aafd4ab0e942779e9f58b1 # v4.3.0
93 - uses: actions/setup-node@v4
94 with:
95 node-version: 22
96 cache: pnpm
97 - run: pnpm install --frozen-lockfile
98 - name: Build runner agent and web islands
99 run: pnpm -r --if-present build
100 - name: Runner image (built only when the runner changed)
101 env:
102 CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
103 CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
104 run: scripts/runner-image.sh
105
106 - name: Create or update Cloudflare resources
107 id: bootstrap
108 env:
109 CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
110 CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
111 run: scripts/bootstrap.sh
112
113 - name: Deploy Worker and runner image
114 working-directory: apps/diffinery
115 env:
116 CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
117 CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
118 run: pnpm exec wrangler deploy -c wrangler.deploy.jsonc
119
120 - name: Wait for new containers to get the new image
121 env:
122 CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
123 CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
124 # The Live test follows this workflow, and its checks must run on the new image.
125 # This job runs on an old-image container, so finishing after the rollout's last step
126 # also shows that a deploy doesn't stop a running job.
127 run: scripts/wait-container-rollout.sh
128
129 - name: Worker secrets (generated on the first deploy; TURNSTILE_SECRET when missing)
130 env:
131 CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
132 CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
133 run: scripts/ensure-secrets.sh
134
135 - name: Finish Access setup on first deploy
136 if: steps.bootstrap.outputs.access_team == ''
137 env:
138 CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
139 CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
140 run: |
141 scripts/bootstrap.sh
142 cd apps/diffinery && pnpm exec wrangler deploy -c wrangler.deploy.jsonc --containers-rollout none
143
144 - name: Smoke test
145 run: |
146 # A new custom domain can take a few minutes to get its certificate.
147 for i in $(seq 1 30); do
148 code=$(curl -s -o /dev/null -w '%{http_code}' https://$DIFFINERY_DOMAIN/healthz) && [ "$code" = 200 ] && exit 0
149 sleep 10
150 done
151 echo "::error::https://$DIFFINERY_DOMAIN/healthz did not return 200 (last: $code). Check that the $DIFFINERY_DOMAIN DNS record is proxied (orange cloud)."; exit 1
152
153 - name: Check sign-in pages
154 run: |
155 set -u
156 page=$(curl -sS https://$DIFFINERY_DOMAIN/login)
157 grep -q "Sign in with a passkey" <<<"$page" || { echo "::error::/login is not the Diffinery sign-in page"; exit 1; }
158 echo "/login serves the passkey sign-in page"
159 loc=$(curl -sS -o /dev/null -w '%{http_code} %{redirect_url}' https://$DIFFINERY_DOMAIN/auth/access)
160 echo "/auth/access -> $loc"
161 [[ "$loc" == 302\ https://*.cloudflareaccess.com/* ]] || echo "::warning::/auth/access is not behind Cloudflare Access yet, so email recovery and first admin sign-in will not work"
162 report=$(curl -sS -w '\n%{http_code}' https://$DIFFINERY_DOMAIN/security/report)
163 code=${report##*$'\n'}
164 if grep -q 'class="cf-turnstile"' <<<"$report"; then
165 echo "/security/report shows Turnstile to people who aren't signed in"
166 elif grep -q 'has no Turnstile configured' <<<"$report"; then
167 echo "::warning::/security/report has no Turnstile, so only people who are signed in can send security reports: the Worker lacks TURNSTILE_SITE_KEY or TURNSTILE_SECRET (see the bootstrap and Worker secrets steps)"
168 else
169 echo "::warning::/security/report has no Turnstile, so only people who are signed in can send security reports: it answered $code without the form"
170 fi
171 for kind in js css; do
172 asset=$(grep -o "/-/kumo/[a-z0-9]*\.$kind" <<<"$page" | head -1)
173 [ -n "$asset" ] || { echo "::error::/login does not load the Kumo $kind bundle"; exit 1; }
174 code=$(curl -sS -o /dev/null -w '%{http_code}' "https://$DIFFINERY_DOMAIN$asset")
175 [ "$code" = 200 ] || { echo "::error::$asset returned $code"; exit 1; }
176 echo "$asset -> 200"
177 done
178 - name: Check favicon and search metadata
179 run: |
180 set -u
181 for p in /favicon.ico /favicon.svg /favicon-48.png /apple-touch-icon.png /og-image.png /site.webmanifest /robots.txt /sitemap.xml; do
182 code=$(curl -sS -o /dev/null -w '%{http_code} %{content_type}' "https://$DIFFINERY_DOMAIN$p")
183 [[ "$code" == 200\ * ]] || { echo "::error::$p returned $code"; exit 1; }
184 echo "$p -> $code"
185 done
186 home=$(curl -sS https://$DIFFINERY_DOMAIN/)
187 for tag in 'rel="icon" href="/favicon.ico"' 'property="og:image"' 'rel="canonical"' 'application/ld+json'; do
188 grep -qF "$tag" <<<"$home" || { echo "::error::the home page is missing $tag"; exit 1; }
189 done
190 echo "the home page carries the favicon, canonical, Open Graph and structured data tags"
191 bm=$(curl -sS -D - -o /dev/null https://$DIFFINERY_DOMAIN/sitemap.xml | tr -d '\r' | sed -n 's/^x-d1-bookmark: //Ip')
192 [ -n "$bm" ] || { echo "::error::D1 queries don't run in a session (no x-d1-bookmark header)"; exit 1; }
193 echo "D1 sessions are on (bookmark $bm)"