Skip to content

Commit

Permalink
circleci: Add docker-build job to CircleCI
Browse files Browse the repository at this point in the history
  • Loading branch information
ezdac authored and karlb committed Jan 14, 2025
1 parent e7935e3 commit 9ea853f
Showing 1 changed file with 138 additions and 0 deletions.
138 changes: 138 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: <<pipeline.parameters.base_image>>
resource_class: "<<parameters.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="<<parameters.registry>>"
export REPOSITORY="<<parameters.repo>>"
export IMAGE_TAGS="$(echo -ne "<<parameters.docker_tags>>" | 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="<<parameters.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 "^<<parameters.docker_name>>/" || true)
echo "Filtered tags: $filtered_tags"
if [ -z "$filtered_tags" ]; then
export GIT_VERSION="untagged"
else
sorted_tags=$(echo "$filtered_tags" | sed "s/<<parameters.docker_name>>\///" | 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 [ "<<parameters.publish>>" == "true" ]; then
gcloud auth configure-docker <<parameters.registry>>
echo "Building for platforms $PLATFORMS and then publishing to registry"
DOCKER_OUTPUT_DESTINATION="--push"
if [ "<<parameters.save_image_tag>>" != "" ]; then
echo "ERROR: cannot save image to docker when publishing to registry"
exit 1
fi
else
if [ "<<parameters.save_image_tag>>" == "" ]; 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 \
<<parameters.docker_name>>
no_output_timeout: 45m
- when:
condition: "<<parameters.save_image_tag>>"
steps:
- run:
name: Save
command: |
IMAGE_NAME="<<parameters.registry>>/<<parameters.repo>>/<<parameters.docker_name>>:<<parameters.save_image_tag>>"
docker save -o /tmp/docker_images/<<parameters.docker_name>>.tar $IMAGE_NAME
- persist_to_workspace:
root: /tmp/docker_images
paths: # only write the one file, to avoid concurrent workspace-file additions
- "<<parameters.docker_name>>.tar"


docker-build:
environment:
Expand Down

0 comments on commit 9ea853f

Please sign in to comment.