diff --git a/.circleci/config.yml b/.circleci/config.yml index 4eba516be429a..e0c9d59faf11d 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -286,6 +286,144 @@ jobs: command: forge build ./test/kontrol/proofs working_directory: packages/contracts-bedrock - notify-failures-on-develop + docker-build: + environment: + DOCKER_BUILDKIT: 1 + parameters: + docker_tags: + description: Docker image tags, comma-separated + type: string + docker_name: + description: "Docker buildx bake target" + type: string + default: "" + registry: + description: Docker registry + type: string + default: "us-docker.pkg.dev" + repo: + description: Docker repo + type: string + default: "oplabs-tools-artifacts/images" + save_image_tag: + description: Save docker image with given tag + type: string + default: "" + platforms: + description: Platforms to build for, comma-separated + type: string + default: "linux/amd64" + publish: + description: Publish the docker image (multi-platform, all tags) + type: boolean + default: false + release: + description: Run the release script + type: boolean + default: false + resource_class: + description: Docker resoruce class + type: string + default: medium + machine: + image: <> + resource_class: "<>" + docker_layer_caching: true # we rely on this for faster builds, and actively warm it up for builds with common stages + steps: + - checkout + - attach_workspace: + at: /tmp/docker_images + - run: + command: mkdir -p /tmp/docker_images + - run: + name: Build + command: | + # Check to see if DOCKER_HUB_READ_ONLY_TOKEN is set (i.e. we are in repo) before attempting to use secrets. + # Building should work without this read only login, but may get rate limited. + if [[ -v DOCKER_HUB_READ_ONLY_TOKEN ]]; then + echo "$DOCKER_HUB_READ_ONLY_TOKEN" | docker login -u "$DOCKER_HUB_READ_ONLY_USER" --password-stdin + fi + + export REGISTRY="<>" + export REPOSITORY="<>" + export IMAGE_TAGS="$(echo -ne "<>" | sed "s/[^a-zA-Z0-9\n,]/-/g")" + export GIT_COMMIT="$(git rev-parse HEAD)" + export GIT_DATE="$(git show -s --format='%ct')" + export PLATFORMS="<>" + + echo "Checking git tags pointing at $GIT_COMMIT:" + tags_at_commit=$(git tag --points-at $GIT_COMMIT) + echo "Tags at commit:\n$tags_at_commit" + + filtered_tags=$(echo "$tags_at_commit" | grep "^<>/" || true) + echo "Filtered tags: $filtered_tags" + + if [ -z "$filtered_tags" ]; then + export GIT_VERSION="untagged" + else + sorted_tags=$(echo "$filtered_tags" | sed "s/<>\///" | sort -V) + echo "Sorted tags: $sorted_tags" + + # prefer full release tag over "-rc" release candidate tag if both exist + full_release_tag=$(echo "$sorted_tags" | grep -v -- "-rc" || true) + if [ -z "$full_release_tag" ]; then + export GIT_VERSION=$(echo "$sorted_tags" | tail -n 1) + else + export GIT_VERSION=$(echo "$full_release_tag" | tail -n 1) + fi + fi + + echo "Setting GIT_VERSION=$GIT_VERSION" + + # Create, start (bootstrap) and use a *named* docker builder + # This allows us to cross-build multi-platform, + # and naming allows us to use the DLC (docker-layer-cache) + docker buildx create --driver=docker-container --name=buildx-build --bootstrap --use + + DOCKER_OUTPUT_DESTINATION="" + if [ "<>" == "true" ]; then + gcloud auth configure-docker <> + echo "Building for platforms $PLATFORMS and then publishing to registry" + DOCKER_OUTPUT_DESTINATION="--push" + if [ "<>" != "" ]; then + echo "ERROR: cannot save image to docker when publishing to registry" + exit 1 + fi + else + if [ "<>" == "" ]; then + echo "Running $PLATFORMS build without destination (cache warm-up)" + DOCKER_OUTPUT_DESTINATION="" + elif [[ $PLATFORMS == *,* ]]; then + echo "ERROR: cannot perform multi-arch (platforms: $PLATFORMS) build while also loading the result into regular docker" + exit 1 + else + echo "Running single-platform $PLATFORMS build and loading into docker" + DOCKER_OUTPUT_DESTINATION="--load" + fi + fi + + # Let them cook! + docker buildx bake \ + --progress plain \ + --builder=buildx-build \ + -f docker-bake.hcl \ + $DOCKER_OUTPUT_DESTINATION \ + <> + + no_output_timeout: 45m + - when: + condition: "<>" + steps: + - run: + name: Save + command: | + IMAGE_NAME="<>/<>/<>:<>" + docker save -o /tmp/docker_images/<>.tar $IMAGE_NAME + - persist_to_workspace: + root: /tmp/docker_images + paths: # only write the one file, to avoid concurrent workspace-file additions + - "<>.tar" + docker-build: environment: