| 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). |
| 11 | set -euo pipefail |
| 12 | ctx=containers/runner |
| 13 | name=diffinery-runner |
| 14 | [ -f "$ctx/dist/agent.mjs" ] || { echo "::error::$ctx/dist/agent.mjs is missing; run pnpm -r build first"; exit 1; } |
| 15 | |
| 16 | tag=$( |
| 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}" |
| 23 | image="registry.cloudflare.com/$CLOUDFLARE_ACCOUNT_ID/$name:$tag" |
| 24 | wrangler() { (cd apps/diffinery && pnpm exec wrangler "$@"); } |
| 25 | |
| 26 | if ! 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="" |
| 29 | fi |
| 30 | if grep -q "\"$tag\"" <<<"$listed"; then |
| 31 | echo "reusing $name:$tag: the runner hasn't changed since it was built" |
| 32 | else |
| 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 |
| 37 | fi |
| 38 | [ -z "${GITHUB_ENV:-}" ] || echo "RUNNER_IMAGE=$image" >> "$GITHUB_ENV" |
| 39 | echo "$image" |