Search code examples
amazon-web-servicesdockerjenkinsaws-ecr

Push to ECR from Jenkins pipeline


I have Jenkins server on-preminse. I have Jenkins file which create Docker image now i want to push that image to AWS ECR.Do i have to create a special IAM user and provide its access and secret access keys ? Or what will be the best way to do this.

I found below on internet

  withAWS(role:'Jenkins', roleAccount:'XXXX216610',duration: 900, roleSessionName: 'jenkinssession')
  sh ' eval \$(aws ecr get-login --no-include-email --region us-east-2) '

But as my jenkins server is onprem how role will work ?


Solution

  • Instead of eval, you now can use the Jenkins ‘amazon-ecr’ plugin from https://plugins.jenkins.io/amazon-ecr/ for ECR deployments.

    pipeline {
      environment {
        registry = '1111111111111.dkr.ecr.eu-central-1.amazonaws.com/myRepo'
        registryCredential = 'ID_OF_MY_AWS_JENKINS_CREDENTIAL'
        dockerImage = ''
      }
      agent any
      stages {
        stage('Building image') {
          steps{
            script {
              dockerImage = docker.build registry + ":$BUILD_NUMBER"
            }
          }
        }
        stage('Deploy image') {
            steps{
                script{
                    docker.withRegistry("https://" + registry, "ecr:eu-central-1:" + registryCredential) {
                        dockerImage.push()
                    }
                }
            }
        }
      }
    }