Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

ci(platform): cache the e2e Docker layers in GHCR instead of the Actions cache (#14892)

Bently0a6d563772f3
diff --git a/.github/workflows/platform-fullstack-ci.yml b/.github/workflows/platform-fullstack-ci.yml
index 479918d361..bc844abc4d 100644
--- a/.github/workflows/platform-fullstack-ci.yml
+++ b/.github/workflows/platform-fullstack-ci.yml
@@ -30,7 +30,7 @@ on:
workflow_dispatch:
inputs:
publish_build_cache:
- description: Export this test commit's Docker build cache
+ description: Write this test commit's Docker build cache to GHCR
required: false
default: false
type: boolean
@@ -167,7 +167,13 @@ jobs:
e2e_test:
name: end-to-end tests
runs-on: ubuntu-latest
+ permissions:
+ contents: read
+ # Only dev pushes and the merge queue write the GHCR build cache. Every
+ # other run just reads it, and fork PRs get a read-only token anyway.
+ packages: write
env:
+ BUILD_CACHE_IMAGE: ghcr.io/significant-gravitas/autogpt-platform-e2e-buildcache
BUILDKIT_IMAGE: docker.io/moby/buildkit:v0.32.0@sha256:1f8167fcb0eca5b7126353d35299386945cbb8949cc516c592a49f80cfce4fa2
steps:
@@ -209,16 +215,22 @@ jobs:
# Used by E2E test data script to generate embeddings for approved store agents
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
- - name: Set up Platform - Expose GHA cache to docker buildx CLI
- uses: crazy-max/ghaction-github-runtime@v4
+ # A failed login leaves buildx anonymous, which still reads a public cache.
+ # A cache that can't be read or written only makes the build cold.
+ - name: Set up Platform - Log in to the GHCR build cache
+ continue-on-error: true
+ uses: docker/login-action@dbcb813823bdd20940b903addbd779551569679f # v4.6.0
+ with:
+ registry: ghcr.io
+ username: ${{ github.actor }}
+ password: ${{ secrets.GITHUB_TOKEN }}
- name: Set up Platform - Prepare Docker build
working-directory: autogpt_platform
env:
NEXT_PUBLIC_PW_TEST: true
NEXT_PUBLIC_SOURCEMAPS: true
- GIT_REF: ${{ github.ref }}
- PUBLISH_BUILD_CACHE: ${{ (github.event_name == 'push' && github.ref == 'refs/heads/dev') || (github.event_name == 'workflow_dispatch' && inputs.publish_build_cache) }}
+ WRITE_BUILD_CACHE: ${{ (github.event_name == 'push' && github.ref == 'refs/heads/dev') || github.event_name == 'merge_group' || (github.event_name == 'workflow_dispatch' && inputs.publish_build_cache) }}
run: |
pip install pyyaml
@@ -238,24 +250,16 @@ jobs:
# The resolved compose file already hands the secret to the container.
sed -i 's/^BETTER_AUTH_SECRET=.*/BETTER_AUTH_SECRET=/' frontend/.env
- cache_to=""
- cache_to_components="backend frontend"
- if [[ "$PUBLISH_BUILD_CACHE" == "true" ]]; then
- cache_to="type=gha,mode=max"
- elif [[ "$GITHUB_EVENT_NAME" == "pull_request" ]]; then
- # Just the final frontend image, so a push that leaves the frontend alone reuses it
- cache_to="type=gha,mode=min"
- cache_to_components="frontend"
+ # Pull requests read the cache and never write it
+ write_cache=()
+ if [[ "$WRITE_BUILD_CACHE" == "true" ]]; then
+ write_cache=(--write-cache)
fi
python ../.github/workflows/scripts/docker-ci-fix-compose-build-cache.py \
--source docker-compose.resolved.yml \
- --cache-from "type=gha" \
- --backend-hash "${{ hashFiles('autogpt_platform/backend/Dockerfile', 'autogpt_platform/backend/poetry.lock', 'autogpt_platform/backend/backend/**') }}" \
- --frontend-hash "${{ hashFiles('autogpt_platform/frontend/Dockerfile', 'autogpt_platform/frontend/pnpm-lock.yaml', 'autogpt_platform/frontend/src/**') }}-sourcemaps" \
- --git-ref "$GIT_REF" \
- --cache-to "$cache_to" \
- --cache-to-components $cache_to_components
+ --cache-image "$BUILD_CACHE_IMAGE" \
+ "${write_cache[@]}"
- name: Set up Platform - Wait for BuildKit image
wait: [buildkit-image]
diff --git a/.github/workflows/scripts/docker-ci-fix-compose-build-cache.py b/.github/workflows/scripts/docker-ci-fix-compose-build-cache.py
index 415fc942c9..3b058d000f 100644
--- a/.github/workflows/scripts/docker-ci-fix-compose-build-cache.py
+++ b/.github/workflows/scripts/docker-ci-fix-compose-build-cache.py
@@ -1,7 +1,13 @@
#!/usr/bin/env python3
"""
-Add cache configuration to a resolved docker-compose file for all services
-that have a build key, and ensure image names match what docker compose expects.
+Add registry cache configuration to a resolved docker-compose file for all
+services that have a build key, and ensure image names match what docker
+compose expects.
+
+Each build target gets its own tag in one registry cache image, e.g.
+ghcr.io/org/buildcache:backend-server. Every run reads it; only runs given
+--write-cache write it. Cache export errors are ignored, so a missing package
+or permission costs a cold build, never a failed one.
"""
import argparse
@@ -9,13 +15,13 @@ import argparse
import yaml
-DEFAULT_BRANCH = "dev"
CACHE_BUILDS_FOR_COMPONENTS = ["backend", "frontend"]
+CACHE_TO_OPTIONS = "mode=max,oci-mediatypes=true,image-manifest=false,ignore-error=true"
def main():
parser = argparse.ArgumentParser(
- description="Add cache config to a resolved compose file"
+ description="Add registry cache config to a resolved compose file"
)
parser.add_argument(
"--source",
@@ -23,40 +29,17 @@ def main():
help="Source compose file to read (should be output of `docker compose config`)",
)
parser.add_argument(
- "--cache-from",
- default="type=gha",
- help="Cache source configuration",
- )
- parser.add_argument(
- "--cache-to",
- default="type=gha,mode=max",
- help="Cache destination configuration; pass an empty value to disable export",
- )
- parser.add_argument(
- "--cache-to-components",
- nargs="*",
- choices=CACHE_BUILDS_FOR_COMPONENTS,
- default=CACHE_BUILDS_FOR_COMPONENTS,
- help="Components that export cache (default: all)",
+ "--cache-image",
+ required=True,
+ help="Registry image that holds the build cache, one tag per build target",
)
- for component in CACHE_BUILDS_FOR_COMPONENTS:
- parser.add_argument(
- f"--{component}-hash",
- default="",
- help=f"Hash for {component} cache scope (e.g., from hashFiles())",
- )
parser.add_argument(
- "--git-ref",
- default="",
- help="Git ref for branch-based cache scope (e.g., refs/heads/master)",
+ "--write-cache",
+ action="store_true",
+ help="Also export the build cache to the registry (mode=max)",
)
args = parser.parse_args()
- # Normalize git ref to a safe scope name (e.g., refs/heads/master -> master)
- git_ref_scope = ""
- if args.git_ref:
- git_ref_scope = args.git_ref.replace("refs/heads/", "").replace("/", "-")
-
with open(args.source, "r") as f:
compose = yaml.safe_load(f)
@@ -125,58 +108,16 @@ def main():
del service_config["build"]
continue
- # This service will do the actual build - add cache config
- cache_from_list = []
- cache_to_list = []
-
component = get_component(dockerfile)
if not component:
# Skip services that don't clearly match frontend/backend
continue
- # Get the hash for this component
- component_hash = getattr(args, f"{component}_hash")
-
- # Scope format: platform-{component}-{target}-{hash|ref}
- # Example: platform-backend-server-abc123
-
- if "type=gha" in args.cache_from:
- # 1. Primary: exact hash match (most specific)
- if component_hash:
- hash_scope = f"platform-{component}-{target}-{component_hash}"
- cache_from_list.append(f"{args.cache_from},scope={hash_scope}")
-
- # 2. Fallback: branch-based cache
- if git_ref_scope:
- ref_scope = f"platform-{component}-{target}-{git_ref_scope}"
- cache_from_list.append(f"{args.cache_from},scope={ref_scope}")
-
- # 3. Fallback: dev branch cache (for PRs/feature branches)
- if git_ref_scope and git_ref_scope != DEFAULT_BRANCH:
- master_scope = f"platform-{component}-{target}-{DEFAULT_BRANCH}"
- cache_from_list.append(f"{args.cache_from},scope={master_scope}")
-
- cache_to = args.cache_to if component in args.cache_to_components else ""
-
- if cache_to and "type=gha" in cache_to:
- # Write to both hash-based and branch-based scopes
- if component_hash:
- hash_scope = f"platform-{component}-{target}-{component_hash}"
- cache_to_list.append(f"{cache_to},scope={hash_scope}")
-
- if git_ref_scope:
- ref_scope = f"platform-{component}-{target}-{git_ref_scope}"
- cache_to_list.append(f"{cache_to},scope={ref_scope}")
-
- # Ensure we have at least one cache source/target
- if not cache_from_list:
- cache_from_list.append(args.cache_from)
- if cache_to and not cache_to_list:
- cache_to_list.append(cache_to)
-
- build_config["cache_from"] = cache_from_list
- if cache_to_list:
- build_config["cache_to"] = cache_to_list
+ # Example: ghcr.io/significant-gravitas/autogpt-platform-e2e-buildcache:backend-server
+ cache_ref = f"type=registry,ref={args.cache_image}:{component}-{target}"
+ build_config["cache_from"] = [cache_ref]
+ if args.write_cache:
+ build_config["cache_to"] = [f"{cache_ref},{CACHE_TO_OPTIONS}"]
else:
build_config.pop("cache_to", None)
modified_services.append(service_name)