Skip to content

Commit

Permalink
Merge pull request #35 from premiscale/PLT-37
Browse files Browse the repository at this point in the history
PLT-37: Create an AWS ECR docker push job
  • Loading branch information
emmeowzing authored Apr 26, 2024
2 parents 0670e95 + 625a11b commit 8985711
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/@orb.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
2 changes: 1 addition & 1 deletion src/commands/docker/login.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
description: |
Login to Docker registry
Login to a Nexus-like Docker registry
parameters:
username:
description: Docker username.
Expand Down
2 changes: 1 addition & 1 deletion src/executors/machine.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
description: Docker build / machine executor.
parameters:
tag:
default: 2204:2022.07.1
default: 2204:2024.01.2
description: Ubuntu version string to use.
type: string
caching:
Expand Down
122 changes: 122 additions & 0 deletions src/jobs/docker/ecr.yml
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
2 changes: 1 addition & 1 deletion src/jobs/docker/hub.yml
Original file line number Diff line number Diff line change
Expand Up @@ -90,5 +90,5 @@ steps:
- run:
name: Push commit tag
command: |+
docker tag << parameters.image-name >>:"$_DOCKER_TAG" << parameters.organization >>/<< parameters.image-name >>:$CIRCLE_SHA1
docker tag << parameters.organization >>/<< parameters.image-name >>:"$_DOCKER_TAG" << parameters.organization >>/<< parameters.image-name >>:$CIRCLE_SHA1
docker push << parameters.organization >>/<< parameters.image-name >>:$CIRCLE_SHA1

0 comments on commit 8985711

Please sign in to comment.