Skip to content

Commit

Permalink
Merge pull request #4 from Sage/multiarch-image-push
Browse files Browse the repository at this point in the history
Add option to push multiarch Docker images
  • Loading branch information
timlapluie authored Aug 17, 2022
2 parents d4d5933 + cb14ea2 commit 0cc9a62
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 15 deletions.
10 changes: 5 additions & 5 deletions hooks/post-command
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ if [[ $ACTION == "push_image" ]];then
echo "--- :floppy_disk: Push $ENVIRONMENT image for $APP"

# Push the docker image
push_image --account_id $ACCOUNT_ID --app $APP --tag $DOCKER_TAG
push_image --account_id $ACCOUNT_ID --app $APP --tag $DOCKER_TAG --multiarch $MULTIARCH_IMAGE_PUSH

# For dev, other images like database and test and master should be pushed.
if [[ "$ENVIRONMENT" == "qa" ]]; then
Expand All @@ -18,13 +18,13 @@ if [[ $ACTION == "push_image" ]];then
if [[ "$BK_BRANCH" == "$BUILDKITE_PIPELINE_DEFAULT_BRANCH" ]]; then
echo "$BUILDKITE_PIPELINE_DEFAULT_BRANCH test image "
BK_BRANCH="test-$BK_BRANCH"
push_image --account_id $ACCOUNT_ID --app $APP --tag test
push_image --account_id $ACCOUNT_ID --app $APP --tag test --multiarch $MULTIARCH_IMAGE_PUSH
fi

if [[ "$HAS_DB_IMAGE" == "true" ]]; then
echo "DB image"
BK_BRANCH="database-$INITIAL_BK_BRANCH"
push_image --account_id $ACCOUNT_ID --app $APP --tag database
push_image --account_id $ACCOUNT_ID --app $APP --tag database --multiarch false
fi
fi
elif [[ $ACTION == "push_param" ]];then
Expand All @@ -48,4 +48,4 @@ elif [[ $ACTION == "build" ]];then
else
echo "Unsupport action name of $ACTION"
exit 1
fi
fi
3 changes: 2 additions & 1 deletion hooks/pre-command
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ export TARGET_TAG="${BUILDKITE_PLUGIN_SBC_SHARED_TARGET_TAG:-}"
set -u
export DOCKER_TAG="${BUILDKITE_PLUGIN_SBC_SHARED_TAG:-application}"
export HAS_DB_IMAGE="${BUILDKITE_PLUGIN_SBC_SHARED_DB_IMAGE:-false}"
export MULTIARCH_IMAGE_PUSH="${BUILDKITE_PLUGIN_SBC_SHARED_MULTIARCH_IMAGE_PUSH:-false}"

# copy scripts to be used by pipeline in order to be invoked within a docker container.
ACTION="$BUILDKITE_PLUGIN_SBC_SHARED_ACTION"
if [[ $ACTION == "publish_gem" ]];then
cp "$(dirname $BASH_SOURCE)/../lib/release_jfrog.sh" .buildkite/release.sh
fi
fi
38 changes: 29 additions & 9 deletions lib/functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ validate_switches() {
echo "Validating switches"

arr=("$@")

set +u
for item in "${arr[@]}"
do
Expand Down Expand Up @@ -112,7 +112,7 @@ buildx_and_cachex () {
fi

buildx --app $app $OPTIONAL_TARGET --tag $tag --file $file --cache_id $cache_id

cachex --app $app --tag $tag --cache_id $cache_id
}

Expand Down Expand Up @@ -144,7 +144,7 @@ cachex () {
# Push an image into a target ECR for deployments
push_image () {
switches "$@"
validate_switches account_id app tag
validate_switches account_id app tag multiarch
varx BUILDKITE_BUILD_NUMBER
varx AWS_REGION
varx BK_BRANCH
Expand All @@ -155,9 +155,29 @@ push_image () {

echo "Pushing image for $app using tag: $target_tag"

SOURCE_IMAGE=$BK_ECR:$app-$tag-build-$BUILDKITE_BUILD_NUMBER
TARGET_IMAGE=$account_id.dkr.ecr.$AWS_REGION.amazonaws.com/$REPO:$target_tag
docker pull $SOURCE_IMAGE
docker tag $SOURCE_IMAGE $TARGET_IMAGE
docker push $TARGET_IMAGE
}
local X86_64_TAG_SUFFIX=""

if [[ "$multiarch" == "true" ]]; then
X86_64_TAG_SUFFIX=-x86_64
fi

TARGET_ECR=$account_id.dkr.ecr.$AWS_REGION.amazonaws.com/$REPO:$target_tag

SOURCE_IMAGE_X86_64=$BK_ECR:$app-$tag-build-$BUILDKITE_BUILD_NUMBER
TARGET_IMAGE_X86_64=$TARGET_ECR$X86_64_TAG_SUFFIX
docker pull $SOURCE_IMAGE_X86_64
docker tag $SOURCE_IMAGE_X86_64 $TARGET_IMAGE_X86_64
docker push $TARGET_IMAGE_X86_64

if [[ "$multiarch" == "true" ]]; then
SOURCE_IMAGE_ARM64=$BK_ECR:$app-$tag-arm64-build-$BUILDKITE_BUILD_NUMBER
TARGET_IMAGE_ARM64=$TARGET_ECR-arm64
docker pull $SOURCE_IMAGE_ARM64
docker tag $SOURCE_IMAGE_ARM64 $TARGET_IMAGE_ARM64
docker push $TARGET_IMAGE_ARM64

# Create & push manifest file for multiarch image
docker manifest create $TARGET_ECR $TARGET_IMAGE_X86_64 $TARGET_IMAGE_ARM64
docker manifest push $TARGET_ECR
fi
}

0 comments on commit 0cc9a62

Please sign in to comment.