diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1df22409..794b7582 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -43,7 +43,10 @@ jobs: # One manifest per kind of build (kache's prefetch list and its shards), so clippy's doesn't overwrite the # tests'. visual-capture and journeys build the same binaries and share one. The bucket (docspring-kache, near # GitHub's runners) is shared by every DocSpring Rust project under one prefix: its entries are addressed by - # their inputs, so sharing them is safe; each project's manifest keys start with its name. + # their inputs, so sharing them is safe; each project's manifest keys start with its name. For the same reason + # a writer could poison every project's builds (an entry's content isn't checked against its key), so the + # read-write key goes only to main-branch jobs of DocSpring's own repositories, in a protected environment; + # forks and repositories that run outside contributors' code on main get the read-only key at most. manifest-key: visualci-clippy s3-access-key-id: ${{ github.ref == 'refs/heads/main' && secrets.KACHE_RW_ACCESS_KEY_ID || secrets.KACHE_RO_ACCESS_KEY_ID }} s3-secret-access-key: ${{ github.ref == 'refs/heads/main' && secrets.KACHE_RW_SECRET_ACCESS_KEY || secrets.KACHE_RO_SECRET_ACCESS_KEY }} @@ -442,14 +445,56 @@ jobs: fi npx --yes wrangler@4.168.0 deploy + # The app's and the renderer's release binaries, built with the compile cache in the rust image the Dockerfiles use + # (so the same Debian and glibc as the runtime images), for `images` to put in the images. Starts with the run, in + # parallel with the tests. Pull requests build them too, and `images` builds without pushing there, so a change that + # breaks an image fails its pull request instead of main's deploy. + release-bins: + runs-on: ${{ fromJSON(vars.CI_RUNNER || '"ubuntu-latest"') }} + environment: ${{ github.ref == 'refs/heads/main' && 'kache-write' || '' }} + container: rust:1.99.1-trixie + timeout-minutes: 30 + steps: + - uses: actions/checkout@v4 + - uses: kunobi-ninja/kache-action@061011817fd04b0e460f79d6bb3c115b1fc8f029 # v1 + with: + version: "2.4.0" + cache-key-prefix: kache-${{ github.job }} + save-cache: ${{ github.ref == 'refs/heads/main' }} + pr-comment: false + s3-bucket: ${{ vars.KACHE_BUCKET }} + s3-endpoint: ${{ vars.KACHE_ENDPOINT }} + s3-region: auto + s3-prefix: rust + manifest-key: visualci-release + s3-access-key-id: ${{ github.ref == 'refs/heads/main' && secrets.KACHE_RW_ACCESS_KEY_ID || secrets.KACHE_RO_ACCESS_KEY_ID }} + s3-secret-access-key: ${{ github.ref == 'refs/heads/main' && secrets.KACHE_RW_SECRET_ACCESS_KEY || secrets.KACHE_RO_SECRET_ACCESS_KEY }} + - run: cargo build --release --locked -p visualci-app --bin visualci-app + env: + BUILD_SHA: ${{ github.sha }} + # Built on its own, as its Dockerfile does, so the app's dependency features don't reach it. + - run: cargo build --release --locked -p visualci-renderer + - run: mkdir -p tmp/bin && cp target/release/visualci-app target/release/visualci-renderer tmp/bin/ + - uses: actions/upload-artifact@v4 + with: + name: release-bins + path: tmp/bin + if-no-files-found: error + retention-days: 3 + images: - needs: [lint, test, app, clients] - if: github.ref == 'refs/heads/main' && github.event_name != 'pull_request' + needs: [lint, test, app, clients, release-bins] runs-on: ${{ fromJSON(vars.CI_RUNNER || '"ubuntu-latest"') }} - timeout-minutes: 60 + # main pushes the images and writes BuildKit's layer cache (in the kache bucket, under buildkit/) with the read-write + # key of the kache-write environment; pull requests build them with the read-only key and push nothing. + environment: ${{ github.ref == 'refs/heads/main' && 'kache-write' || '' }} + timeout-minutes: 30 permissions: contents: read packages: write + env: + PUSH: ${{ github.ref == 'refs/heads/main' && github.event_name != 'pull_request' }} + S3_CACHE: type=s3,region=auto,bucket=${{ vars.KACHE_BUCKET }},endpoint_url=${{ vars.KACHE_ENDPOINT }},use_path_style=true,prefix=buildkit/,access_key_id=${{ github.ref == 'refs/heads/main' && secrets.KACHE_RW_ACCESS_KEY_ID || secrets.KACHE_RO_ACCESS_KEY_ID }},secret_access_key=${{ github.ref == 'refs/heads/main' && secrets.KACHE_RW_SECRET_ACCESS_KEY || secrets.KACHE_RO_SECRET_ACCESS_KEY }} outputs: app: ${{ steps.app.outputs.digest }} renderer: ${{ steps.renderer.outputs.digest }} @@ -457,8 +502,14 @@ jobs: - uses: actions/checkout@v4 - id: version run: echo "renderer=$(cat docker/renderer.version)" >>"$GITHUB_OUTPUT" + - uses: actions/download-artifact@v4 + with: + name: release-bins + path: tmp/bin + - run: chmod +x tmp/bin/* - uses: docker/setup-buildx-action@v3 - - uses: docker/login-action@v3 + - if: env.PUSH == 'true' + uses: docker/login-action@v3 with: registry: ghcr.io username: ${{ github.actor }} @@ -469,14 +520,15 @@ jobs: with: context: . file: docker/app.Dockerfile - push: true + build-contexts: bin=tmp/bin + push: ${{ env.PUSH == 'true' }} build-args: | BUILD_SHA=${{ github.sha }} RENDERER_VERSION=${{ steps.version.outputs.renderer }} VISUALCI_ANALYTICS=1 tags: ghcr.io/docspring/visualci-app:${{ github.sha }} - cache-from: type=gha,scope=app - cache-to: type=gha,mode=max,scope=app + cache-from: ${{ env.S3_CACHE }},name=visualci-app + cache-to: ${{ env.PUSH == 'true' && format('{0},name=visualci-app,mode=max,ignore-error=true', env.S3_CACHE) || '' }} # The renderer is rebuilt only when its inputs change, so its digest (and the running renderers) stay put # through deploys that only touch the app. Rebuilding an unchanged renderer would give a new digest and roll # the renderers mid-build for nothing. Its inputs: the workspace crates it's built from, the toolchain, the @@ -492,7 +544,8 @@ jobs: paths=$(echo "$tree" | sed -n 's|.* (\(/.*\))$|\1|p' | sed "s|^$PWD/||") deps=$(echo "$tree" | grep -v ' (/') # shellcheck disable=SC2086 - hash=$( { git ls-files -s docker/renderer.Dockerfile docker/renderer.version rust-toolchain.toml $paths; grep -v '^version = ' Cargo.toml; echo "$deps"; } | sha256sum | cut -c1-24) + # The image release-bins compiles it in (its `container:`) is an input too. + hash=$( { git ls-files -s docker/renderer.Dockerfile docker/renderer.version rust-toolchain.toml $paths; grep -v '^version = ' Cargo.toml; echo "$deps"; grep -m1 'container: rust:' .github/workflows/ci.yml; } | sha256sum | cut -c1-24) echo "tag=ghcr.io/docspring/visualci-renderer:inputs-$hash" >>"$GITHUB_OUTPUT" if digest=$(docker buildx imagetools inspect "ghcr.io/docspring/visualci-renderer:inputs-$hash" --format '{{json .Manifest}}' 2>/dev/null | python3 -c 'import json,sys; print(json.load(sys.stdin)["digest"])'); then echo "existing=$digest" >>"$GITHUB_OUTPUT" @@ -504,13 +557,14 @@ jobs: with: context: . file: docker/renderer.Dockerfile - push: true + build-contexts: bin=tmp/bin + push: ${{ env.PUSH == 'true' }} build-args: RENDERER_VERSION=${{ steps.version.outputs.renderer }} tags: | ${{ steps.renderer_inputs.outputs.tag }} ghcr.io/docspring/visualci-renderer:${{ github.sha }} - cache-from: type=gha,scope=renderer - cache-to: type=gha,mode=max,scope=renderer + cache-from: ${{ env.S3_CACHE }},name=visualci-renderer + cache-to: ${{ env.PUSH == 'true' && format('{0},name=visualci-renderer,mode=max,ignore-error=true', env.S3_CACHE) || '' }} - id: renderer run: echo "digest=${{ steps.renderer_inputs.outputs.existing || steps.renderer_build.outputs.digest }}" >>"$GITHUB_OUTPUT" diff --git a/docker/app.Dockerfile b/docker/app.Dockerfile index 546e34ed..d16dbecb 100644 --- a/docker/app.Dockerfile +++ b/docker/app.Dockerfile @@ -37,11 +37,17 @@ ARG BUILD_SHA=dev ENV BUILD_SHA=${BUILD_SHA} RUN cargo build --release --locked -p visualci-app --bin visualci-app +# The binary the runtime image takes. CI builds it with its compile cache, in this same rust image, and passes it in as +# the named build context `bin` (docker build --build-context bin=), which replaces this stage: the Rust stages +# above then don't run. A plain docker build compiles it here. +FROM scratch AS bin +COPY --from=build /src/target/release/visualci-app /visualci-app + FROM debian:trixie-slim RUN apt-get update && apt-get install -y --no-install-recommends ca-certificates && rm -rf /var/lib/apt/lists/* \ && useradd --uid 10001 --create-home visualci WORKDIR /app -COPY --from=build /src/target/release/visualci-app /usr/local/bin/visualci-app +COPY --from=bin /visualci-app /usr/local/bin/visualci-app COPY --from=frontend /src/frontend/dist ./public # Laravel's storage directory (framework files). The pods' root filesystem is read-only, so the deployment mounts an # emptyDir here; caches, sessions and the queue are in Postgres (CACHE_STORE, SESSION_DRIVER, QUEUE_CONNECTION). diff --git a/docker/renderer.Dockerfile b/docker/renderer.Dockerfile index c6e5da15..8d5970ab 100644 --- a/docker/renderer.Dockerfile +++ b/docker/renderer.Dockerfile @@ -10,6 +10,12 @@ WORKDIR /src COPY . . RUN cargo build --release --locked -p visualci-renderer +# The binary the runtime image takes. CI builds it with its compile cache, in this same rust image, and passes it in as +# the named build context `bin` (docker build --build-context bin=), which replaces this stage: the build stage +# above then doesn't run. A plain docker build compiles it here. +FROM scratch AS bin +COPY --from=build /src/target/release/visualci-renderer /visualci-renderer + FROM debian:trixie-slim ARG PDFBOX_VERSION=3.0.8 ARG PDFBOX_SHA256=f6b3a80c39747ff0ef5d06708bc03882152403de58dbef4a1fbffbee568eceb1 @@ -62,7 +68,7 @@ RUN printf '%s\n' '#!/bin/sh' \ && useradd --uid 10001 --create-home renderer \ && chown -R renderer /opt/pdfbox -COPY --from=build /src/target/release/visualci-renderer /usr/local/bin/visualci-renderer +COPY --from=bin /visualci-renderer /usr/local/bin/visualci-renderer # Bump docker/renderer.version whenever anything above changes pixels: it's part of every render cache key. ARG RENDERER_VERSION RUN test -n "$RENDERER_VERSION"