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.

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