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.

39 lines · 1.8 KB · executableRaw History
1#!/usr/bin/env bash
2# Picks the runner image for a deploy. The tag is a hash of everything the image is built
3# from (the Dockerfile and its build context: the built agent and image/), plus the ISO
4# week so base packages are refreshed weekly. If the Cloudflare registry already has that
5# tag, the deploy reuses it; otherwise it is built and pushed now. Any change to the runner
6# changes the tag, so a changed runner is always rebuilt.
7#
8# Run from the repository root after `pnpm -r build`, with CLOUDFLARE_API_TOKEN and
9# CLOUDFLARE_ACCOUNT_ID set. Prints the image and, in Actions, sets RUNNER_IMAGE for later
10# steps (scripts/render-config.mjs points every container at it).
11set -euo pipefail
12ctx=containers/runner
13name=diffinery-runner
14[ -f "$ctx/dist/agent.mjs" ] || { echo "::error::$ctx/dist/agent.mjs is missing; run pnpm -r build first"; exit 1; }
15
16tag=$(
17 {
18 date -u +%G-W%V
19 cd "$ctx" && find Dockerfile .dockerignore dist/agent.mjs image -type f | LC_ALL=C sort | xargs sha256sum
20 } | sha256sum | cut -c1-24
21)
22: "${CLOUDFLARE_ACCOUNT_ID:?CLOUDFLARE_ACCOUNT_ID is required}"
23image="registry.cloudflare.com/$CLOUDFLARE_ACCOUNT_ID/$name:$tag"
24wrangler() { (cd apps/diffinery && pnpm exec wrangler "$@"); }
25
26if ! listed=$(wrangler containers images list --json --filter "(^|/)$name\$" 2>&1); then
27 echo "::warning::could not list the registry's images, so the runner image is built again: $(tail -n 3 <<<"$listed")"
28 listed=""
29fi
30if grep -q "\"$tag\"" <<<"$listed"; then
31 echo "reusing $name:$tag: the runner hasn't changed since it was built"
32else
33 echo "building $name:$tag"
34 # The runner reports this as its image (not part of the tag's hash, which lists its inputs).
35 echo "$name:$tag" > "$ctx/dist/image-tag"
36 wrangler containers build "$PWD/$ctx" -t "$name:$tag" -p
37fi
38[ -z "${GITHUB_ENV:-}" ] || echo "RUNNER_IMAGE=$image" >> "$GITHUB_ENV"
39echo "$image"