Sending Docker Images to Amazon ECR using jenkins pipeline →Aws part 11

Aakib
2 min readMay 5, 2023

--

To set up Jenkins to push Docker images to Amazon Elastic Container Registry (ECR) with Jenkins pipeline script, you can follow the steps outlined below:

Install the necessary plugins:

a. AWS Steps Plugin - This plugin provides a set of AWS steps for Jenkins Pipeline. It is required to interact with ECR.

b. Docker Pipeline Plugin - This plugin provides Docker-based Pipeline steps. It is required to build and push Docker images to ECR.

Create an Amazon ECR repository:

a. Go to the Amazon ECR console.

b. Click on the "Create repository" button.

c. Give your repository a name and click on the "Create repository" button.

Create a Jenkins Pipeline:

a. Create a new Jenkins Pipeline job.

b. In the Pipeline script section, write the Jenkins Pipeline script to build and push the Docker image to ECR.

Here’s is code given below :

pipeline { agent any environment { ECR_REGISTRY = "your_ecr_registry_here" ECR_REPOSITORY = "your_ecr_repository_here" DOCKER_IMAGE_TAG = "your_docker_image_tag_here" } stages { stage("Build Docker image") { steps { script { docker.build("${ECR_REGISTRY}/${ECR_REPOSITORY}:${DOCKER_IMAGE_TAG}") } } } stage("Push Docker image to ECR") { steps { script { withCredentials([[$class: 'AmazonWebServicesCredentialsBinding', accessKeyVariable: 'AWS_ACCESS_KEY_ID', secretKeyVariable: 'AWS_SECRET_ACCESS_KEY']]) { docker.withRegistry("https://${ECR_REGISTRY}", "ecr:us-east-1:${AWS_ACCESS_KEY_ID}") { dockerImage.push("${ECR_REGISTRY}/${ECR_REPOSITORY}:${DOCKER_IMAGE_TAG}") } } } } } } }

Set up AWS credentials in Jenkins:

a. Go to Jenkins -> Credentials.

b. Click on the "Global credentials (unrestricted)" link.

c. Click on the "Add Credentials" button.

d. Choose "AWS Credentials" from the Kind drop-down list.

e. Enter your AWS access key ID and secret access key.

f. Click on the "OK" button.

Save the Pipeline job and run it. Jenkins will build and push the Docker image to ECR.

That's it! You have now set up Jenkins to push Docker images to Amazon ECR with Jenkins Pipeline script.

--

--

Aakib

Cloud computing and DevOps Engineer and to be as a fresher I am learning and gaining experiance by doing some hands on projects on DevOps and in AWS OR GCP