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.

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