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.

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