-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from premiscale/PLT-37
PLT-37: Create an AWS ECR docker push job
- Loading branch information
Showing
5 changed files
with
127 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,4 +10,5 @@ display: | |
|
||
orbs: | ||
helm: circleci/[email protected] | ||
circleci-cli: circleci/[email protected] | ||
circleci-cli: circleci/[email protected] | ||
aws-cli: circleci/[email protected] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
description: |+ | ||
Build an push a Docker image to AWS ECR. | ||
Requires a context with $AWS_ACCESS_KEY_ID, $AWS_SECRET_ACCESS_KEY, $AWS_DEFAULT_REGION, $AWS_ACCOUNT_ID set. | ||
executor: | ||
name: machine | ||
caching: << parameters.docker-layer-caching >> | ||
resource_class: << parameters.resource-class >> | ||
parameters: | ||
resource-class: | ||
type: enum | ||
enum: | ||
- medium | ||
- large | ||
- xlarge | ||
- 2xlarge | ||
default: medium | ||
description: Resource class to run as. | ||
image-name: | ||
description: Name of the image, and also the ECR repository. | ||
default: '' | ||
type: string | ||
tag: | ||
description: Name of the tag for the image. | ||
default: latest | ||
type: string | ||
commit-tag: | ||
description: Whether or not to push an additional tag to the registry with the commit hash as the tag. | ||
default: false | ||
type: boolean | ||
branch-tag: | ||
description: Whether or not to push an additional tag to the registry with the branch name as the tag. | ||
default: false | ||
type: boolean | ||
docker-layer-caching: | ||
description: Enable DLC on the machine executor. Costs 200 credits / run, however. | ||
default: false | ||
type: boolean | ||
args: | ||
description: Additional args string to add to the build command. (E.g., '--build-arg=HELLO=WORLD'.) | ||
default: '' | ||
type: string | ||
path: | ||
description: Path to a particular Dockerfile or containing directory with a Dockerfile present. | ||
default: Dockerfile | ||
type: string | ||
aws-access-key-id: | ||
description: AWS Access Key ID. | ||
type: string | ||
default: $AWS_ACCESS_KEY_ID | ||
aws-secret-access-key: | ||
description: AWS Secret Access Key ID. | ||
type: string | ||
default: $AWS_SECRET_ACCESS_KEY | ||
aws-region: | ||
description: Region of the ECR registry. | ||
type: string | ||
default: $AWS_DEFAULT_REGION | ||
aws-account-id: | ||
description: AWS Account ID. | ||
type: string | ||
default: $AWS_ACCOUNT_ID | ||
aws-cli-version: | ||
description: Version of the AWS CLI to install. | ||
type: string | ||
default: latest | ||
buildkit: | ||
description: Enable buildkit (https://docs.docker.com/build/buildkit/#getting-started). | ||
type: boolean | ||
default: true | ||
steps: | ||
- checkout | ||
- aws-cli/install: | ||
version: << parameters.aws-cli-version >> | ||
- run: | ||
name: Docker login (ECR via 'aws ecr') | ||
command: |+ | ||
aws ecr get-login-password --region << parameters.aws-region >> | docker login --username AWS --password-stdin << parameters.aws-account-id >>.dkr.ecr.<< parameters.aws-region >>.amazonaws.com | ||
- run: | ||
name: Check ECR repository target | ||
command: |+ | ||
# If it does not exist, just create it. | ||
if [ -z "$(aws ecr describe-repositories | jq -rMC ".repositories[] | select(.repositoryName == \"<< parameters.image-name >>\") | .repositoryName")" ]; then | ||
printf "WARNING: Repository \"<< parameters.image-name >>\" does not exist. Creating.\\n" >&2 | ||
aws ecr create-repository --repository-name "<< parameters.image-name >>" | ||
else | ||
printf "INFO: Repository \"<< parameters.image-name >>\" already exists.\\n" | ||
fi | ||
- run: | ||
name: Build and push tag | ||
command: |+ | ||
if [[ "<< parameters.tag >>" =~ v[0-9]+.[0-9]+.[0-9]+ ]]; then | ||
export _DOCKER_TAG="$(printf "%s" "<< parameters.tag >>" | grep -oP "(?<=v).*" | awk NF)" | ||
else | ||
export _DOCKER_TAG="<< parameters.tag >>" | ||
fi | ||
if [ "<< parameters.buildkit >>" = "true" ]; then | ||
export DOCKER_BUILDKIT=1 | ||
fi | ||
if [ "$(echo "<< parameters.path >>" | grep -oP "Dockerfile")" != "Dockerfile" ]; then | ||
# Path does not contain the dockerfile, so we default to "Dockerfile". | ||
docker build . -f ./<< parameters.path >>/Dockerfile -t << parameters.aws-account-id >>.dkr.ecr.<< parameters.aws-region >>.amazonaws.com/<< parameters.image-name >>:"$_DOCKER_TAG" << parameters.args >> | ||
docker push << parameters.aws-account-id >>.dkr.ecr.<< parameters.aws-region >>.amazonaws.com/<< parameters.image-name >>:"$_DOCKER_TAG" | ||
else | ||
# Path does specify the Dockerfile explictly, don't default to "Dockerfile". E.g., "docker/Dockerfile.develop". | ||
docker build . -f ./<< parameters.path >> -t << parameters.aws-account-id >>.dkr.ecr.<< parameters.aws-region >>.amazonaws.com/<< parameters.image-name >>:"$_DOCKER_TAG" << parameters.args >> | ||
docker push << parameters.aws-account-id >>.dkr.ecr.<< parameters.aws-region >>.amazonaws.com/<< parameters.image-name >>:"$_DOCKER_TAG" | ||
fi | ||
if [ "<< parameters.commit-tag >>" = "true" ]; then | ||
printf "INFO: Pushing Docker image tag based on git commit SHA at user's request.\\n" | ||
docker tag << parameters.aws-account-id >>.dkr.ecr.<< parameters.aws-region >>.amazonaws.com/<< parameters.image-name >>:"$_DOCKER_TAG" << parameters.aws-account-id >>.dkr.ecr.<< parameters.aws-region >>.amazonaws.com/<< parameters.image-name >>:$CIRCLE_SHA1 | ||
docker push << parameters.aws-account-id >>.dkr.ecr.<< parameters.aws-region >>.amazonaws.com/<< parameters.image-name >>:$CIRCLE_SHA1 | ||
fi | ||
if [ "<< parameters.branch-tag >>" = "true" ]; then | ||
printf "INFO: Pushing Docker image tag based on git branch name at user's request.\\n" | ||
docker tag << parameters.aws-account-id >>.dkr.ecr.<< parameters.aws-region >>.amazonaws.com/<< parameters.image-name >>:"$_DOCKER_TAG" << parameters.aws-account-id >>.dkr.ecr.<< parameters.aws-region >>.amazonaws.com/<< parameters.image-name >>:$CIRCLE_BRANCH | ||
docker push << parameters.aws-account-id >>.dkr.ecr.<< parameters.aws-region >>.amazonaws.com/<< parameters.image-name >>:$CIRCLE_BRANCH | ||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters