Diffinery

codician/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.

346 lines · 19.0 KBRaw History
1name: Live test
2
3# End-to-end checks against the real diffinery.com after each deploy: email
4# sending, hosted Actions jobs in Cloudflare Containers (with Docker builds and services), the container registry, agent sandboxes,
5# webhooks, scheduled workflows, Git LFS, and pull requests (test merge commits, pull_request_target and the merge
6# queue). Each run works in a throwaway private repository that it
7# deletes at the end.
8#
9# Needs the repository secret DIFFINERY_LIVE_TOKEN: a Diffinery token (Settings ->
10# Tokens) with the admin scope and access to all repositories.
11on:
12 workflow_dispatch:
13 workflow_run:
14 workflows: [Deploy]
15 types: [completed]
16 branches: [main]
17
18permissions:
19 contents: read
20
21concurrency:
22 group: live-test
23 cancel-in-progress: false
24
25jobs:
26 live:
27 name: Containers, registry, sandboxes, webhooks, schedules, LFS and pull requests
28 # Runs on the host that deploys production (see the gate in deploy.yml).
29 if: ${{ github.event_name == 'workflow_dispatch' || (github.server_url == 'https://diffinery.com' && github.event.workflow_run.conclusion == 'success') }}
30 runs-on: ubuntu-latest
31 timeout-minutes: 45
32 env:
33 DIFFINERY: https://diffinery.com
34 TOKEN: ${{ secrets.DIFFINERY_LIVE_TOKEN }}
35 REPO: live-${{ github.run_id }}-${{ github.run_attempt }}
36 steps:
37 - name: Check the token
38 id: token
39 run: |
40 if [ -z "$TOKEN" ]; then
41 echo "::warning::DIFFINERY_LIVE_TOKEN is not set, so the live test is skipped. Create an admin token at $DIFFINERY/settings/tokens and save it as a repository secret."
42 echo "skip=true" >> "$GITHUB_OUTPUT"
43 exit 0
44 fi
45 login=$(curl -fsS "$DIFFINERY/api/v3/user" -H "Authorization: Bearer $TOKEN" | jq -r .login)
46 echo "OWNER=$login" >> "$GITHUB_ENV"
47 echo "testing as $login in $login/$REPO"
48
49 - name: Email sending is configured
50 if: steps.token.outputs.skip != 'true'
51 run: |
52 set -euo pipefail
53 s=$(curl -fsS "$DIFFINERY/api/v3/diffinery/admin/email" -H "Authorization: Bearer $TOKEN")
54 echo "$s" | jq '{sending, queue, from, last_24h}'
55 [ "$(jq -r .sending <<<"$s")" = true ] || { echo "::error::the Worker has no Email Sending binding"; exit 1; }
56 [ "$(jq -r .queue <<<"$s")" = true ] || { echo "::error::the Worker has no mail queue binding"; exit 1; }
57 failed=$(jq -r .last_24h.failed <<<"$s")
58 [ "$failed" = 0 ] || echo "::warning::$failed emails failed in the last 24 hours; see Settings -> Email in site administration"
59
60 - name: Create a throwaway repository with a webhook
61 if: steps.token.outputs.skip != 'true'
62 run: |
63 set -euo pipefail
64 api() { curl -fsS -X "$1" "$DIFFINERY/api/v3$2" -H "Authorization: Bearer $TOKEN" -H "content-type: application/json" ${3:+--data "$3"}; }
65 api POST /user/repos "{\"name\":\"$REPO\",\"private\":true}" >/dev/null
66 api POST "/repos/$OWNER/$REPO/hooks" "{\"config\":{\"url\":\"$DIFFINERY/healthz\"},\"events\":[\"push\"]}" >/dev/null
67 echo "created $OWNER/$REPO"
68
69 - name: Push workflows and an LFS file
70 if: steps.token.outputs.skip != 'true'
71 run: |
72 set -euo pipefail
73 mkdir -p src/.github/workflows && cd src
74 git init -q -b main
75 git config user.name "Live test" && git config user.email "live-test@diffinery.com"
76 git lfs install --local >/dev/null
77 git lfs track "*.bin" >/dev/null
78 head -c 3145728 /dev/urandom > blob.bin
79 sha256sum blob.bin | cut -d' ' -f1 > ../blob.sha
80 echo "# $REPO" > README.md
81 cat > .github/workflows/ci.yml <<'EOF'
82 on: push
83 jobs:
84 hosted:
85 runs-on: ubuntu-latest
86 strategy:
87 matrix:
88 n: [1, 2]
89 steps:
90 - uses: actions/checkout@v4
91 - run: echo "live ok ${{ '${{' }} matrix.n }} $(cat README.md)"
92 docker:
93 runs-on: ubuntu-latest
94 services:
95 cache:
96 image: redis:7-alpine
97 ports: ["6379"]
98 options: --health-cmd "redis-cli ping" --health-interval 1s --health-retries 30
99 steps:
100 - run: |
101 printf 'FROM alpine:3.20\nRUN echo built > /msg\nCMD ["cat", "/msg"]\n' > Dockerfile
102 docker build -q -t live-image .
103 echo "docker ok $(docker run --rm live-image)"
104 exec 3<>/dev/tcp/127.0.0.1/${{ '${{' }} job.services.cache.ports['6379'] }}
105 printf 'PING\r\n' >&3
106 read -r line <&3
107 echo "service ok ${line%$'\r'}"
108 # Container registry: a layer over Cloudflare's 100 MB request limit, pushed with
109 # the job's GITHUB_TOKEN, plus the docker/*-action route most ghcr.io workflows use.
110 publish:
111 runs-on: ubuntu-latest
112 permissions:
113 packages: write
114 steps:
115 - run: |
116 image="diffinery.com/${GITHUB_REPOSITORY,,}"
117 echo "${{ '${{' }} secrets.GITHUB_TOKEN }}" | docker login diffinery.com -u "${{ '${{' }} github.actor }}" --password-stdin
118 head -c 150000000 /dev/urandom > big.bin
119 sha256sum big.bin | cut -d' ' -f1 > big.sha
120 printf 'FROM scratch\nCOPY big.bin big.sha /\n' > Dockerfile
121 docker build -q -t "$image:live" .
122 docker push "$image:live"
123 echo "pushed $(cat big.sha)"
124 buildx:
125 runs-on: ubuntu-latest
126 permissions:
127 packages: write
128 steps:
129 - run: |
130 echo "IMAGE=diffinery.com/${GITHUB_REPOSITORY,,}" >> "$GITHUB_ENV"
131 head -c 120000000 /dev/urandom > big.bin
132 printf 'FROM scratch\nCOPY big.bin /\n' > Dockerfile
133 - uses: docker/setup-buildx-action@v3
134 - uses: docker/login-action@v3
135 with:
136 registry: diffinery.com
137 username: ${{ '${{' }} github.actor }}
138 password: ${{ '${{' }} secrets.GITHUB_TOKEN }}
139 - uses: docker/build-push-action@v6
140 with:
141 context: .
142 push: true
143 tags: ${{ '${{' }} env.IMAGE }}:buildx
144 pull:
145 needs: [publish, buildx]
146 runs-on: ubuntu-latest
147 permissions:
148 packages: read
149 steps:
150 - run: |
151 image="diffinery.com/${GITHUB_REPOSITORY,,}"
152 echo "${{ '${{' }} secrets.GITHUB_TOKEN }}" | docker login diffinery.com -u "${{ '${{' }} github.actor }}" --password-stdin
153 docker pull -q "$image:live"
154 id=$(docker create "$image:live" true)
155 docker cp "$id:/big.bin" big.bin && docker cp "$id:/big.sha" big.sha
156 [ "$(sha256sum big.bin | cut -d' ' -f1)" = "$(cat big.sha)" ] && echo "registry ok $(stat -c %s big.bin) bytes"
157 docker pull -q "$image:buildx"
158 id=$(docker create "$image:buildx" true)
159 docker cp "$id:/big.bin" buildx.bin
160 echo "buildx ok $(stat -c %s buildx.bin) bytes"
161 EOF
162 cat > .github/workflows/tick.yml <<'EOF'
163 on:
164 schedule:
165 - cron: "*/5 * * * *"
166 jobs:
167 tick:
168 runs-on: ubuntu-latest
169 steps:
170 - run: echo "scheduled ok"
171 EOF
172 git add -A && git commit -q -m "Live test"
173 git push -q "https://x-token:${TOKEN}@${DIFFINERY#https://}/$OWNER/$REPO.git" main
174 git lfs ls-files
175
176 - name: Hosted Actions jobs (and Docker) run in Cloudflare Containers
177 if: steps.token.outputs.skip != 'true'
178 run: |
179 set -euo pipefail
180 api() { curl -fsS "$DIFFINERY/api/v3$1" -H "Authorization: Bearer $TOKEN"; }
181 for i in $(seq 1 90); do
182 run=$(api "/repos/$OWNER/$REPO/actions/runs?event=push" | jq -c '.workflow_runs[0] // empty')
183 [ -n "$run" ] && [ "$(jq -r .status <<<"$run")" = completed ] && break
184 sleep 10
185 done
186 echo "$run" | jq '{id, status, conclusion}'
187 [ "$(jq -r .conclusion <<<"$run")" = success ] || { echo "::error::the push run did not succeed"; exit 1; }
188 id=$(jq -r .id <<<"$run")
189 jobs=$(api "/repos/$OWNER/$REPO/actions/runs/$id/jobs")
190 for job in $(jq -r '.jobs[] | select(.name | startswith("hosted")) | .id' <<<"$jobs"); do
191 log=$(api "/repos/$OWNER/$REPO/actions/jobs/$job/logs")
192 grep -F "live ok" <<<"$log" | grep -F "# $REPO" || { echo "::error::job $job did not print its line (checkout or run step failed)"; exit 1; }
193 done
194 job=$(jq -r '.jobs[] | select(.name == "docker") | .id' <<<"$jobs")
195 log=$(api "/repos/$OWNER/$REPO/actions/jobs/$job/logs")
196 grep -F "docker ok built" <<<"$log" || { echo "::error::docker build or run failed on a hosted runner"; exit 1; }
197 grep -F "service ok +PONG" <<<"$log" || { echo "::error::the redis service container was not reachable"; exit 1; }
198 job=$(jq -r '.jobs[] | select(.name == "pull") | .id' <<<"$jobs")
199 log=$(api "/repos/$OWNER/$REPO/actions/jobs/$job/logs")
200 grep -F "registry ok 150000000 bytes" <<<"$log" || { echo "::error::the image pushed with a 150 MB layer did not come back intact from the registry"; exit 1; }
201 grep -F "buildx ok 120000000 bytes" <<<"$log" || { echo "::error::the image pushed by docker/build-push-action did not come back from the registry"; exit 1; }
202 api "/users/$OWNER/packages/container/${REPO,,}/versions" | jq -c '.[] | {tags: .metadata.container.tags, size, platforms}'
203
204 - name: Git LFS round trip
205 if: steps.token.outputs.skip != 'true'
206 run: |
207 set -euo pipefail
208 git clone -q "https://x-token:${TOKEN}@${DIFFINERY#https://}/$OWNER/$REPO.git" clone
209 [ "$(sha256sum clone/blob.bin | cut -d' ' -f1)" = "$(cat blob.sha)" ] || { echo "::error::the LFS file came back different"; exit 1; }
210 echo "LFS file matches ($(stat -c %s clone/blob.bin) bytes)"
211
212 - name: Webhook delivered
213 if: steps.token.outputs.skip != 'true'
214 run: |
215 set -euo pipefail
216 for i in $(seq 1 30); do
217 hook=$(curl -fsS "$DIFFINERY/api/v3/repos/$OWNER/$REPO/hooks" -H "Authorization: Bearer $TOKEN" | jq -c '.[0]')
218 [ "$(jq -r '.lastDeliveryAt // empty' <<<"$hook")" != "" ] && break
219 sleep 5
220 done
221 echo "$hook" | jq '{url, lastStatus, lastDeliveryAt}'
222 [ "$(jq -r '.lastDeliveryAt // empty' <<<"$hook")" != "" ] || { echo "::error::the push webhook was never delivered"; exit 1; }
223
224 - name: Agent sandbox
225 if: steps.token.outputs.skip != 'true'
226 run: |
227 set -euo pipefail
228 base="$DIFFINERY/api/v3/repos/$OWNER/$REPO/diffinery/sandboxes"
229 id=$(curl -fsS -X POST "$base" -H "Authorization: Bearer $TOKEN" -H "content-type: application/json" --data '{}' | jq -r .id)
230 echo "sandbox $id"
231 for i in $(seq 1 40); do
232 out=$(curl -sS -X POST "$base/$id/exec" -H "Authorization: Bearer $TOKEN" -H "content-type: application/json" --data '{"cmd":"cat README.md && git lfs ls-files | wc -l"}' || true)
233 jq -e '.exitCode == 0' >/dev/null 2>&1 <<<"$out" && break
234 sleep 6
235 done
236 echo "$out" | jq '{exitCode, stdout}'
237 curl -sS -X POST "$base/$id/close" -H "Authorization: Bearer $TOKEN" >/dev/null || true
238 jq -e --arg r "# $REPO" '.exitCode == 0 and (.stdout | contains($r))' >/dev/null <<<"$out" || { echo "::error::the sandbox did not run the command in the repository"; exit 1; }
239
240 - name: Pull requests and the merge queue
241 if: steps.token.outputs.skip != 'true'
242 run: |
243 set -euo pipefail
244 Q="$REPO-pr"
245 api() { curl -fsS -X "$1" "$DIFFINERY/api/v3$2" -H "Authorization: Bearer $TOKEN" -H "content-type: application/json" ${3:+--data "$3"}; }
246 api POST /user/repos "{\"name\":\"$Q\",\"private\":true}" >/dev/null
247 echo "PR_REPO=$Q" >> "$GITHUB_ENV"
248 api PUT "/repos/$OWNER/$Q/actions/secrets/TOPSECRET" '{"value":"s3cret"}' >/dev/null
249 remote="https://x-token:${TOKEN}@${DIFFINERY#https://}/$OWNER/$Q.git"
250 mkdir -p pr/.github/workflows && cd pr
251 git init -q -b main
252 git config user.name "Live test" && git config user.email "live-test@diffinery.com"
253 printf 'a\nb\nc\n' > app.txt
254 cat > .github/workflows/ci.yml <<'EOF'
255 name: CI
256 on:
257 pull_request:
258 merge_group:
259 jobs:
260 test:
261 runs-on: ubuntu-latest
262 steps:
263 - uses: actions/checkout@v4
264 - run: echo "pr ok event=${{ '${{' }} github.event_name }} ref=${{ '${{' }} github.ref }} sha=${{ '${{' }} github.sha }} head=$(git rev-parse HEAD) app=$(tr '\n' ' ' < app.txt)"
265 EOF
266 cat > .github/workflows/target.yml <<'EOF'
267 name: Target
268 on: pull_request_target
269 jobs:
270 t:
271 runs-on: ubuntu-latest
272 steps:
273 - run: echo "target ok secret=${{ '${{' }} secrets.TOPSECRET == 's3cret' }} ref=${{ '${{' }} github.ref }}"
274 EOF
275 git add -A && git commit -q -m "PR test" && git push -q "$remote" main
276 # The pull request changes app.txt and tries to rewrite the pull_request_target workflow.
277 git checkout -q -b change
278 printf 'A\nb\nc\n' > app.txt
279 sed -i 's/target ok/PWNED/' .github/workflows/target.yml
280 git commit -q -am "Change" && git push -q "$remote" change
281 head=$(git rev-parse HEAD)
282 # Main moves on, so the test merge commit holds both sides.
283 git checkout -q main
284 printf 'a\nb\nC\n' > app.txt
285 git commit -q -am "Main moves" && git push -q "$remote" main
286 n=$(api POST "/repos/$OWNER/$Q/pulls" '{"title":"Change","head":"change","base":"main"}' | jq -r .number)
287 echo "opened pull request #$n (head $head)"
288
289 wait_run() { # event -> the completed run JSON
290 for i in $(seq 1 60); do
291 run=$(api GET "/repos/$OWNER/$Q/actions/runs?event=$1" | jq -c '.workflow_runs[0] // empty')
292 [ -n "$run" ] && [ "$(jq -r .status <<<"$run")" = completed ] && { echo "$run"; return; }
293 sleep 10
294 done
295 echo "::error::no completed $1 run" >&2; exit 1
296 }
297 log_of() { for job in $(api GET "/repos/$OWNER/$Q/actions/runs/$1/jobs" | jq -r '.jobs[].id'); do api GET "/repos/$OWNER/$Q/actions/jobs/$job/logs"; done; }
298 pr=$(wait_run pull_request)
299 echo "$pr" | jq '{event, ref, sha, head_sha, conclusion}'
300 [ "$(jq -r .conclusion <<<"$pr")" = success ] || { echo "::error::the pull_request run failed"; exit 1; }
301 [ "$(jq -r .ref <<<"$pr")" = "refs/pull/$n/merge" ] && [ "$(jq -r .head_sha <<<"$pr")" = "$head" ] && [ "$(jq -r .sha <<<"$pr")" != "$head" ] || { echo "::error::the pull_request run did not use the test merge commit"; exit 1; }
302 log_of "$(jq -r .id <<<"$pr")" | grep -F "pr ok event=pull_request ref=refs/pull/$n/merge" | grep -F "app=A b C" || { echo "::error::the pull_request job did not check out the merged code"; exit 1; }
303 target=$(wait_run pull_request_target)
304 echo "$target" | jq '{event, ref, sha, head_sha, conclusion}'
305 tlog=$(log_of "$(jq -r .id <<<"$target")")
306 grep -F "target ok secret=true ref=refs/heads/main" <<<"$tlog" || { echo "::error::pull_request_target did not run the base workflow with secrets"; exit 1; }
307 ! grep -q PWNED <<<"$tlog" || { echo "::error::pull_request_target ran the pull request's workflow"; exit 1; }
308
309 api PUT "/repos/$OWNER/$Q/branches/main/protection" '{"requirePullRequest":true,"requiredChecks":["CI / test"],"mergeQueue":{"method":"squash","checkTimeoutMinutes":20}}' >/dev/null
310 code=$(curl -sS -o /dev/null -w '%{http_code}' -X PUT "$DIFFINERY/api/v3/repos/$OWNER/$Q/pulls/$n/merge" -H "Authorization: Bearer $TOKEN" -H "content-type: application/json" --data '{}')
311 [ "$code" = 405 ] || { echo "::error::a direct merge into a merge queue branch returned $code"; exit 1; }
312 api PUT "/repos/$OWNER/$Q/pulls/$n/merge-queue" '{"delete_branch":true}' | jq '{state, position: .entry.position, merge_group: .entry.merge_group}'
313 for i in $(seq 1 60); do
314 merged=$(api GET "/repos/$OWNER/$Q/pulls/$n" | jq -r .merged)
315 [ "$merged" = true ] && break
316 sleep 10
317 done
318 [ "$merged" = true ] || { echo "::error::the merge queue did not merge #$n"; api GET "/repos/$OWNER/$Q/merge-queue/main" | jq .; exit 1; }
319 group=$(wait_run merge_group)
320 echo "$group" | jq '{event, ref, sha, conclusion}'
321 [[ "$(jq -r .ref <<<"$group")" == refs/heads/gh-readonly-queue/main/pr-$n-* ]] || { echo "::error::the merge_group run is not on a queue branch"; exit 1; }
322 sha=$(api GET "/repos/$OWNER/$Q/branches/main" | jq -r .commit.sha)
323 [ "$sha" = "$(jq -r .sha <<<"$group")" ] || { echo "::error::main ($sha) is not the tested merge group"; exit 1; }
324 git fetch -q "$remote" main && [ "$(git show FETCH_HEAD:app.txt | tr '\n' ' ')" = "A b C " ] || { echo "::error::main does not hold the merged change"; exit 1; }
325 echo "pull request #$n merged through the queue as $sha"
326
327 - name: Scheduled workflow
328 if: steps.token.outputs.skip != 'true'
329 run: |
330 set -euo pipefail
331 # The cron trigger fires every 5 minutes, so allow two windows.
332 for i in $(seq 1 75); do
333 run=$(curl -fsS "$DIFFINERY/api/v3/repos/$OWNER/$REPO/actions/runs?event=schedule" -H "Authorization: Bearer $TOKEN" | jq -c '.workflow_runs[0] // empty')
334 [ -n "$run" ] && [ "$(jq -r .status <<<"$run")" = completed ] && break
335 sleep 10
336 done
337 [ -n "$run" ] || { echo "::error::no scheduled run started"; exit 1; }
338 echo "$run" | jq '{id, event, status, conclusion}'
339 [ "$(jq -r .conclusion <<<"$run")" = success ] || { echo "::error::the scheduled run did not succeed"; exit 1; }
340
341 - name: Delete the throwaway repositories
342 if: always() && steps.token.outputs.skip != 'true'
343 run: |
344 curl -sS -o /dev/null -w 'package %{http_code}\n' -X DELETE "$DIFFINERY/api/v3/users/$OWNER/packages/container/${REPO,,}" -H "Authorization: Bearer $TOKEN"
345 curl -sS -o /dev/null -w '%{http_code}\n' -X DELETE "$DIFFINERY/api/v3/repos/$OWNER/$REPO" -H "Authorization: Bearer $TOKEN"
346 [ -z "${PR_REPO:-}" ] || curl -sS -o /dev/null -w '%{http_code}\n' -X DELETE "$DIFFINERY/api/v3/repos/$OWNER/$PR_REPO" -H "Authorization: Bearer $TOKEN"