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.

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