I'm trying to integrate Amazon's new Elastic Container Registry (ECR) with my Jenkins build service. I'm using the Cloudbees Docker Build & Publish plugin to build container images and publish them to a registry.
To use ECR instead of my private registry, I've ran the AWS CLI command aws --region us-east-1 ecr get-login
which spews a docker login
command to run - but I just copied out the password and created a Jenkins credentials of type "Username with password" from that password (the username is always "AWS").
And that works fine! The problem is that the ECR password generates by the AWS CLI is only valid for 12 hours. So right now, I have to manually regenerate the password twice a day and update the Jenkins credentials screen manually, otherwise my builds start failing.
Is there a way to generate permanent ECR login tokens, or somehow automate the token generation?
This is now possible using amazon-ecr-credential-helper as described in https://aws.amazon.com/blogs/compute/authenticating-amazon-ecr-repositories-for-docker-cli-with-credential-helper/.
The short of it is:
{"credsStore": "ecr-login"}
As @Connor McCarthy said, while waiting for Amazon to come up with a better solution for more permanent keys, in the mean time we'd need to generate the keys on the Jenkins server ourselves somehow.
My solution is to have a periodic job that updates the Jenkins credentials for ECR every 12 hours automatically, using the Groovy API. This is based on this very detailed answer, though I did a few things differently and I had to modify the script.
Steps:
ecr:GetAuthorizationToken
to the server role. [update] To get any pushes complete successfully, you'd also need to grant these permissions:ecr:InitiateLayerUpload, ecr:UploadLayerPart, ecr:CompleteLayerUpload, ecr:BatchCheckLayerAvailability, ecr:PutImage
. Amazon has a built-in policy that offers these capabilities, calledAmazonEC2ContainerRegistryPowerUser
.dpkg -l python-pip >/dev/null 2>&1 || sudo apt-get install python-pip -y; pip list 2>/dev/null | grep -q awscli || pip install awscli
Please note:
"AWS"
as the username for the ECR credentials - this is how ECR works, but if you have multiple credentials with the username "AWS", then you'd need to update the script to locate the credentials based on the description field or something.null
for the ID (as in the answer I linked before), then a new ID will be created and the setting of the credentials in the docker build step will be lost.And that's it - the script should be able to run every 12 hours and refresh the ECR credentials, and we can continue to use the Docker plugins.
I was looking into this exact same issue too. I didn't come up with the answer either of us was looking for, but I was able to create a workaround with shell scripting. Until AWS comes out with a better solution to ECR credentials, I plan on doing something along these lines.
I replaced the Docker Build and Publish step of the Jenkins job with and Execute Shell step. I used the following script (could probably be written better) to build and publish my container to ECR. Replace the variables in < > brackets as needed:
Using https://wiki.jenkins-ci.org/display/JENKINS/Amazon+ECR with the Docker Build and Publish plugin works just fine.