Setup Jenkins with Amazon Elastic Container Registry

0
942
Setup Jenkins with Amazon Elastic Container Registry

We have discussed more about AWS services and now it’s time to discuss about next one. Yes, its Elastic container registry. Amazon Elastic Container Registry (ECR) is a fully-managed Docker container registry that makes it easy for developers to store, manage, and deploy Docker container images. Amazon ECR is integrated with Amazon Elastic Container Service (ECS), simplifying your development to production workflow. Amazon ECR eliminates the need to operate your own container repositories or worry about scaling the underlying infrastructure. Amazon ECR hosts your images in a highly available and scalable architecture, allowing you to reliably deploy containers for your applications. Integration with AWS Identity and Access Management (IAM) provides resource-level control of each repository. With Amazon ECR, there are no upfront fees or commitments. You pay only for data you store in your repositories and data transferred to the Internet.

In our previous posts, we have seen How to setup azure and google container registry on Jenkins, now its time to learn how to do in leading cloud service provider in the world. As its leading other competitor not only with service, with some easy steps too. As azure and google cloud need more steps to create the container registry but AWS have very few straight forward steps to create the registry.

Required Plugins

To access ECR from Jenkins, we need to install the following plugins.

  • docker-build-step: This plugin allows to add various Docker commands as a build step. Latest version 2.0.
  • Amazon ECR Plugin: This plugin generates Docker authentication token from Amazon Credentials to access Amazon ECR.

Create Container Registry

Login to your AWS account and in services, you can find ECR under compute section. Just click the ECR, it will take you to ECR welcome page, if you are new otherwise you can see your previous images. Once you been to welcome page as below

AWS ECRNow click Get Started for new user and create repository for existing users. Now it will take you to new page to enter repository name as below

create registryOnce you give create repository, it will create, and you can see page as below,

ECR imagesTo see push command, click on “view push command” from right side corner or click on the repository and it will unlock “view push commands” option. Click that it will show the command like below

ECR push commandSetup Jenkins Credentials

Go back to your Jenkins server, and make sure login with an admin account. Choose “Credentials” from the sidebar, then choose “System” à “Global credentials” (you can choose other domains as well) and click “Add Credentials”.

Select “AWS credentials” for the scope and other access id and secret ID fill you aws details to authenticate.

AWS credentialsSetup Docker

If your docker already works with Jenkins, you can skip this step. Or please refer

In https://foxutech.com/how-to-add-jenkins-slave-to-master/ docker on slave section. And make sure docker is installed and running.

Setup Jenkins Job to Push Image

Now it is time to create the Jenkins job. Create a freestyle Jenkins job and fill other settings with your needs (such as git repository, parameters, etc.). In the build steps, you can add Docker commands to build and push / pull docker images.

For push / pull command, Fill the parameters like this:

  • Name of the image to push/pull: image name, e.g. dockerapp
  • Tag: image tag, Jenkins variables can be used, e.g. build-$BUILD_NUMBER
  • Registry: the registry name on ECR without https, e.g. 400845174994.dkr.ecr.us-west-2.amazonaws.com/foxtech
  • Docker registry URL: ECR URL, use 400845174994.dkr.ecr.us-west-2.amazonaws.com/foxtech
  • Registry credentials: Choose the credentials we created in “Setup Jenkins Credentials” section

Using Jenkinsfile:

Along with this we can see latest trend thing like pushing image using Jenkinsfile. For that create pipeline job and use following script to do it.

node {
    stage 'build-dockerapp'

    stage 'Publish containers'
    shouldPublish = input message: 'Publish Containers?', parameters: [[$class: 'ChoiceParameterDefinition', choices: 'yes\nno', description: '', name: 'Deploy']]
    if(shouldPublish == "yes") {
     echo "Publishing docker containers"
     sh "\$(aws ecr get-login)"

     sh "docker tag dockerapp:latest 400845174994.dkr.ecr.us-west-2.amazonaws.com/foxtech:latest"
     sh "docker push 400845174994.dkr.ecr.us-west-2.amazonaws.com/foxtech:latest"
    }
}

Note: Replace with your registry URL and image name.

NO COMMENTS