Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(build): Build multi-architecture docker images for aztec-sandbox #2305

Merged
merged 4 commits into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 59 additions & 7 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@ jobs:
name: "Build and test"
command: build aztec-rpc

aztec-sandbox:
aztec-sandbox-base:
machine:
image: ubuntu-2004:202010-01
resource_class: large
Expand All @@ -581,7 +581,40 @@ jobs:
- *setup_env
- run:
name: "Build and test"
command: force_deploy_build aztec-sandbox
command: force_deploy_build aztec-sandbox-base false

aztec-sandbox-x86_64:
machine:
image: ubuntu-2004:202010-01
resource_class: large
steps:
- *checkout
- *setup_env
- run:
name: "Build and test"
command: force_deploy_build aztec-sandbox false x86_64

aztec-sandbox-arm64:
machine:
image: ubuntu-2204:2023.07.1
resource_class: arm.large
steps:
- *checkout
- *setup_env
- run:
name: "Build and test"
command: force_deploy_build aztec-sandbox false arm64

aztec-sandbox-ecr-manifest:
machine:
image: ubuntu-2004:202010-01
resource_class: large
steps:
- *checkout
- *setup_env
- run:
name: "Create ECR manifest"
command: create_ecr_manifest aztec-sandbox-base aztec-sandbox x86_64,arm64

circuits-js:
machine:
Expand Down Expand Up @@ -1146,6 +1179,7 @@ jobs:
command: |
deploy_ecr aztec-sandbox
deploy_npm aztec-sandbox

deploy-dockerhub:
machine:
image: ubuntu-2004:202010-01
Expand All @@ -1157,8 +1191,11 @@ jobs:
name: "deploy-sandbox"
working_directory: aztec-sandbox
command: |
deploy_ecr aztec-sandbox
deploy_dockerhub aztec-sandbox
deploy_ecr aztec-sandbox x86_64
deploy_ecr aztec-sandbox arm64
deploy_dockerhub aztec-sandbox x86_64
deploy_dockerhub aztec-sandbox arm64
create_dockerhub_manifest aztec-sandbox x86_64,arm64

deploy-end:
docker:
Expand Down Expand Up @@ -1373,9 +1410,24 @@ workflows:
- types: *yarn_project
- circuits-js: *yarn_project
- rollup-provider: *yarn_project
- aztec-sandbox: *yarn_project
- aztec-sandbox-base: *yarn_project
- canary: *yarn_project

- aztec-sandbox-x86_64:
requires:
- aztec-sandbox-base
<<: *defaults
- aztec-sandbox-arm64:
requires:
- aztec-sandbox-base
<<: *defaults

- aztec-sandbox-ecr-manifest:
requires:
- aztec-sandbox-x86_64
- aztec-sandbox-arm64
<<: *defaults

- e2e-join:
requires:
- aztec-js
Expand All @@ -1399,7 +1451,7 @@ workflows:
- types
- circuits-js
- rollup-provider
- aztec-sandbox
- aztec-sandbox-ecr-manifest
- canary
<<: *defaults

Expand Down Expand Up @@ -1472,7 +1524,7 @@ workflows:
- guides-sample-dapp
<<: *defaults

# Deployment and Canary tests
# Deployment and Canary tests
- deploy-dockerhub:
requires:
- e2e-end
Expand Down
8 changes: 7 additions & 1 deletion build-system/scripts/build
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ set -euo pipefail

REPOSITORY=$1
FORCE_BUILD=${2:-"false"}
ARCH=${3:-""}
DOCKERFILE=$(query_manifest dockerfile $REPOSITORY)
PROJECT_DIR=$(query_manifest projectDir $REPOSITORY)
BUILD_DIR=$(query_manifest buildDir $REPOSITORY)
Expand All @@ -34,6 +35,7 @@ echo "Repository: $REPOSITORY"
echo "Working directory: $PWD"
echo "Dockerfile: $DOCKERFILE"
echo "Build directory: $BUILD_DIR"
echo "Arch: $ARCH"

# Fetch images with retries
function fetch_image() {
Expand Down Expand Up @@ -112,7 +114,8 @@ for PARENT in $PARENTS; do
echo "Pulling dependency $PARENT_REPO..."
fetch_image $PARENT_IMAGE_URI
# Tag it to look like an official release as that's what we use in Dockerfiles.
retry docker tag $PARENT_IMAGE_URI $ECR_DEPLOY_URL/$PARENT
TAG=$ECR_DEPLOY_URL/$PARENT
retry docker tag $PARENT_IMAGE_URI $TAG
done


Expand Down Expand Up @@ -183,6 +186,9 @@ fi

# Build the actual image and give it a commit tag.
IMAGE_COMMIT_URI=$ECR_URL/$REPOSITORY:cache-$CONTENT_HASH
if [[ -n "$ARCH" ]]; then
IMAGE_COMMIT_URI=$IMAGE_COMMIT_URI-$ARCH
fi
echo "Building image: $IMAGE_COMMIT_URI"
# Build our dockerfile, add timing information
docker build -t $IMAGE_COMMIT_URI -f $DOCKERFILE $CACHE_FROM --build-arg COMMIT_TAG=$COMMIT_TAG_VERSION --build-arg ARG_CONTENT_HASH=$CONTENT_HASH . \
Expand Down
82 changes: 82 additions & 0 deletions build-system/scripts/create_dockerhub_manifest
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#!/bin/bash
# This script:
# 1. Logs into DockerHub
# 2. Creates 2 manifest lists, the first is version tagged, the second is 'latest' tagged
# 3. Adds the arch specific tagged image to each list
# 4. Pushes the 2 lists

set -eu

if [ -z "$COMMIT_TAG" ]; then
echo "Will only push tagged builds to dockerhub. Skipping."
exit 0
fi

REPOSITORY=$1
ARCH_LIST=$2

echo "Repo: $REPOSITORY"
echo "Arch list: $ARCH_LIST"

ACCOUNT="aztecprotocol"
USERNAME="aztecprotocolci"

COMMIT_TAG_VERSION=$COMMIT_TAG # default unless repo-specific
# Check if it's a repo-specific tag
if [[ "$COMMIT_TAG" == *"/"* ]]; then
REPO_NAME="${COMMIT_TAG%%/*}"
COMMIT_TAG_VERSION="${COMMIT_TAG#*/}"
echo "Tag was made for: $REPO_NAME"
echo "Version: $COMMIT_TAG_VERSION"

# Check if REPO_NAME is equal to REPOSITORY
if [ "$REPO_NAME" != "$REPOSITORY" ]; then
echo "REPO_NAME ($REPO_NAME) does not match REPOSITORY ($REPOSITORY). Exiting..."
exit 1
fi
fi

# Check it's a valid semver.
VERSION=$(npx semver $COMMIT_TAG_VERSION)
if [ -z "$VERSION" ]; then
echo "$COMMIT_TAG_VERSION is not a semantic version."
exit 1
fi

# We now have the tage for each image
IMAGE_TAG=$COMMIT_TAG_VERSION

MANIFEST_DEPLOY_URI=$ACCOUNT/$REPOSITORY:$IMAGE_TAG
MANIFEST_LATEST_URI=$ACCOUNT/$REPOSITORY:latest

# Login to dockerhub.
echo "$DOCKERHUB_PASSWORD" | docker login -u $USERNAME --password-stdin

export DOCKER_CLI_EXPERIMENTAL=enabled

OLD_IFS=$IFS
IFS=','

# For each arch, add the tagged image to 2 manifest lists. One tagged with the version, the other with 'latest'
for A in $ARCH_LIST
do
IMAGE_DEPLOY_URI=$ACCOUNT/$REPOSITORY:$IMAGE_TAG-$A
echo "Adding image $IMAGE_DEPLOY_URI to manifest list $MANIFEST_DEPLOY_URI"
docker manifest create $MANIFEST_DEPLOY_URI \
--amend $IMAGE_DEPLOY_URI

echo "Adding image $IMAGE_DEPLOY_URI to manifest list $MANIFEST_LATEST_URI"
docker manifest create $MANIFEST_LATEST_URI \
--amend $IMAGE_DEPLOY_URI
done

IFS=$OLD_IFS
unset OLD_IFS

echo "Pushing manifest list $MANIFEST_DEPLOY_URI..."
# Push the version tagged list
docker manifest push --purge $MANIFEST_DEPLOY_URI

echo "Pushing manifest list $MANIFEST_LATEST_URI..."
# Push the latest tagged list
docker manifest push --purge $MANIFEST_LATEST_URI
38 changes: 38 additions & 0 deletions build-system/scripts/create_ecr_manifest
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/bin/bash
# This script:
# 1. Logs into ECR and ensures we have the given repository
# 2. Computes the image uri of the cached images for the given repository given the list of architectures
# 3. Creates a manifest list using a platform agnositc image uri, adds each image to it
# 4. Pushes the manifest list

set -e

REPOSITORY=$1
FINAL_IMAGE_NAME=$2
ARCH_LIST=$3

# Ensure ECR repository exists.
retry ensure_repo $REPOSITORY $ECR_REGION refresh_lifecycle

CONTENT_HASH=$(calculate_content_hash $REPOSITORY)
echo "Content hash: $CONTENT_HASH"

FINAL=$ECR_URL/$FINAL_IMAGE_NAME:cache-$CONTENT_HASH

echo "Creating manifest list $FINAL..."

export DOCKER_CLI_EXPERIMENTAL=enabled

OLD_IFS=$IFS
IFS=','
for A in $ARCH_LIST
do
IMAGE=$ECR_URL/$FINAL_IMAGE_NAME:cache-$CONTENT_HASH-$A
echo "Adding image $IMAGE to manifest list"
docker manifest create $FINAL \
--amend $IMAGE
done
IFS=$OLD_IFS
unset OLD_IFS

docker manifest push --purge $FINAL
27 changes: 24 additions & 3 deletions build-system/scripts/deploy_dockerhub
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,22 @@ if [ -z "$COMMIT_TAG" ]; then
fi

REPOSITORY=$1
ARCH=${2:-""}

echo "Repo: $REPOSITORY"
echo "Arch: $ARCH"

ACCOUNT="aztecprotocol"
USERNAME="aztecprotocolci"

IMAGE_COMMIT_URI=$ECR_DEPLOY_URL/$REPOSITORY:$COMMIT_HASH
IMAGE_LATEST_URI=aztecprotocol/$REPOSITORY:latest

IMAGE_LATEST_URI=$ACCOUNT/$REPOSITORY:latest
if [[ -n "$ARCH" ]]; then
IMAGE_COMMIT_URI=$IMAGE_COMMIT_URI-$ARCH
IMAGE_LATEST_URI=$IMAGE_LATEST_URI-$ARCH
fi

COMMIT_TAG_VERSION=$COMMIT_TAG # default unless repo-specific
# Check if it's a repo-specific tag
if [[ "$COMMIT_TAG" == *"/"* ]]; then
Expand All @@ -24,7 +38,10 @@ if [[ "$COMMIT_TAG" == *"/"* ]]; then
fi
fi
IMAGE_TAG=$COMMIT_TAG_VERSION
IMAGE_DEPLOY_URI=aztecprotocol/$REPOSITORY:$IMAGE_TAG
IMAGE_DEPLOY_URI=$ACCOUNT/$REPOSITORY:$IMAGE_TAG
if [[ -n "$ARCH" ]]; then
IMAGE_DEPLOY_URI=$IMAGE_DEPLOY_URI-$ARCH
fi

# Check it's a valid semver.
VERSION=$(npx semver $COMMIT_TAG_VERSION)
Expand All @@ -39,13 +56,17 @@ echo "Deploying to dockerhub: $IMAGE_DEPLOY_URI"
retry ensure_repo $REPOSITORY $ECR_DEPLOY_REGION

# Login to dockerhub.
echo "$DOCKERHUB_PASSWORD" | docker login -u aztecprotocolci --password-stdin
echo "$DOCKERHUB_PASSWORD" | docker login -u $USERNAME --password-stdin

echo "Pulling $IMAGE_COMMIT_URI"
# Pull image.
retry docker pull $IMAGE_COMMIT_URI

echo "Tagging $IMAGE_COMMIT_URI as $IMAGE_DEPLOY_URI"
# Tag with commit tag
retry docker tag $IMAGE_COMMIT_URI $IMAGE_DEPLOY_URI

echo "Tagging $IMAGE_COMMIT_URI as $IMAGE_LATEST_URI"
# Tag with :latest
retry docker tag $IMAGE_COMMIT_URI $IMAGE_LATEST_URI

Expand Down
11 changes: 9 additions & 2 deletions build-system/scripts/deploy_ecr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
set -eu

REPOSITORY=$1
ARCH=${2:-""}
IMAGE_COMMIT_URI=$(calculate_image_uri $REPOSITORY)
if [[ -n "$ARCH" ]]; then
IMAGE_COMMIT_URI=$IMAGE_COMMIT_URI-$ARCH
fi

# Login to build region and pull the build.
retry ensure_repo $REPOSITORY $ECR_REGION
Expand All @@ -11,9 +15,12 @@ retry docker pull $IMAGE_COMMIT_URI > /dev/null 2>&1
# Ensure ECR repository exists in deployment region.
retry ensure_repo $REPOSITORY $ECR_DEPLOY_REGION

# Push image to deployment repo with commit hash tag e.g:
# falafel:deadbeefcafebabe1337c0de
# Push image to deployment repo with commit hash tag and optional architecture e.g:
# falafel:deadbeefcafebabe1337c0de-x86_64
IMAGE_DEPLOY_COMMIT_URI=$ECR_DEPLOY_URL/$REPOSITORY:$COMMIT_HASH
if [[ -n "$ARCH" ]]; then
IMAGE_DEPLOY_COMMIT_URI=$IMAGE_DEPLOY_COMMIT_URI-$ARCH
fi
retry docker tag $IMAGE_COMMIT_URI $IMAGE_DEPLOY_COMMIT_URI
retry docker push $IMAGE_DEPLOY_COMMIT_URI

Expand Down
16 changes: 9 additions & 7 deletions build-system/scripts/force_deploy_build
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@
#
# This script forces a build if we're in a deployment run, otherwise runs `build` as usual
#
# usage: ./deploy_force_build <repository>
# example: ./deploy_force_build aztec-sandbox
# usage: ./deploy_force_build <repository> <force-build>
# example: ./deploy_force_build aztec-sandbox true
set -e

REPOSITORY=$1
FORCE_BUILD=${2:-"false"}
shift
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

curious, why are we starting to use shift for positional args in our scripts?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's when you want to pass arguments past a certain point through to child scripts. So in this script, the first 2 args are consumed by this script and then any remaining args are passed to the build script via $@ (meaning all the current args).

FORCE_BUILD=$1
shift

# if FORCE_BUILD is already set, just continue with it
if [[ $FORCE_BUILD == 'true' ]]; then
build $REPOSITORY true
build $REPOSITORY true $@
exit 0
fi

Expand All @@ -37,12 +39,12 @@ if [[ -n "${COMMIT_TAG:-}" ]]; then
VERSION=$(npx semver $COMMIT_TAG_VERSION)
if [ -z "$VERSION" ]; then
# Not a version tag, build normally
build $REPOSITORY
build $REPOSITORY false $@
else
# Force build
build $REPOSITORY true
build $REPOSITORY true $@
fi
else
# Not a tagged commit, build normally
build $REPOSITORY
build $REPOSITORY false $@
fi
Loading