Pushing images to ECR with the Spotify Docker Maven plugin and BitBucket Pipelines

We had to do this recently for a client and basically ended up following this guide, however there were a few “gotchas” related to using ECS/ECR.

ECR “repositories” are basically the quivalent of “images” in the regular registry sense. This is important as the “username”/”namespace” bit of the URL is now the “image” name.

The authentication has to be done with:

aws ecr get-login --region ap-southeast-2

which actually just produces a “docker login …” command for the repo.

BitBucket Pipeline configuration file (bitbucket-pipelines.yml):

image: maven:3.3.9

# enable Docker for your repository
options:
  docker: true

pipelines:
  default:
    - step:
      caches:
        - maven
      script:
        - curl "https://s3.amazonaws.com/aws-cli/awscli-bundle.zip" -o "awscli-bundle.zip"
        - apt-get update && apt-get install -y jq
        - unzip awscli-bundle.zip
        - ./awscli-bundle/install -b ~/bin/aws
        - export PATH=~/bin:$PATH
        - eval $(aws ecr get-login --region ${AWS_DEFAULT_REGION} --no-include-email)
        - mvn -DbuildNumber=$BITBUCKET_BUILD_NUMBER clean install docker:build docker:push
        - export IMAGE_NAME=123450486789.dkr.ecr.ap-southeast-2.amazonaws.com/REPONAMEHERE:$BITBUCKET_BUILD_NUMBER
        - export TASK_VERSION=$(aws ecs register-task-definition --family TASKDEFINITION --container-definitions "[{"name":"NAME","image":"$IMAGE_NAME","cpu":10,"memory":10,"essential":true}]" | jq --raw-output '.taskDefinition.revision')
        - echo "task version is $TASK_VERSION"
        - aws ecs update-service --cluster CLUSTERNAME --service SERVICENAME --task-definition TASK_DEFINITION_NAME:$TASK_VERSION

Note that we have to run the “eval” command prior to running Maven to ensure that the credentials end up in our “~/.docker/config.json” file.

Also, the “Service” referred to in the last step is assumed to be created already before running the build.

NOTE: Yes, I do know that the plugin in question is no longer recommended to be used, we are planning to migrate off of it

NOTE: This link also helped out a lot https://medium.com/thomas-strohmeier/how-to-push-docker-images-to-amazon-ecr-with-bitbucket-pipeline-84658626ca29

 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.