'not able login to aws ecr from jenkins pipeline

pipeline {
    agent {
        label 'label'
    }

     environment {
         AWS_ACCESS_ID = credentials('aws-access-key')
    AWS_SECRET_KEY = credentials('aws-secret-key')

    DKR_AWS_CLI = 'docker run ' +
      '-e AWS_ACCESS_KEY_ID=${AWS_ACCESS_ID} ' +
      '-e AWS_SECRET_ACCESS_KEY=${AWS_SECRET_KEY} ' +
      '-e AWS_DEFAULT_REGION=eu-central-1 ' +
      //'-v `pwd`:/project ' +
      'image1/aws-cli'

}

  stages {

stage('pull latest aws-cli docker image') {

            steps {

                sh "docker pull image1/aws-cli"
            }
        }
        stage('logging in to AWS ECR') {

            steps {

                script {

                    def ECR_LOGIN = sh(
                            script: "${DKR_AWS_CLI} ecr get-login --region=eu-central-1",
                            returnStdout: true
                    ).trim()
                    sh "${ECR_LOGIN}"
                }
            }
        }
  }

I have added my credentials to jenkins and i am not able to login to ECR getting the below error

HTTPSConnectionPool(host='ecr.eu-central-1.amazonaws.com', port=443): Max retries exceeded with url: / (Caused by ConnectTimeoutError(<botocore.awsrequest.AWSHTTPSConnection object at 0x7f9a816fa0d0>, 'Connection to ecr.eu-central-1.amazonaws.com timed out. (connect timeout=60)'))



Solution 1:[1]

Most possibly you also need to programatically(i.e., in your code) add a token for your session.

Solution 2:[2]

You can write Jenkins pipeline as above. You can store ecr credentials in Jenkins and refer Jenkins credentials in the pipeline. In this example code, I have referred to it as ecr-credentials. Make sure your IAM user has permission to access ECR.

pipeline {
        
        environment {
          registry = "xxxx.xxx.ecr.us-east-1.amazonaws.com/repo"
          dockerImage = ''
        }
      
      
        stages {
          
          stage('Create Docker image') {
              
              steps {
                  
                  script {
                      dockerImage = docker.build registry + ":$BUILD_NUMBER"
                  }
              }
          }
          
          stage('Push Docker image to Docker Registry') {
              steps {
                  script {
                      docker.withRegistry( "https://" + registry, "ecr:aws-region:ecr-credentials") {
                      dockerImage.push()
                      }
                  }
              }
          }
          
      
        }
       
    }

Solution 3:[3]

stage('Login ECR image') {
              
              steps {
                  
                  script {
                      sh "aws ecr get-login-password --region ADD_YOUR_REGION_NAME | docker login --username AWS --password-stdin ADD_YOUR_REPO_NAME"
                      sh "docker build -t image_name_containing_repo_name ."
                      sh "docker push image_name_containing_repo_name"
                  }
              }
          }

repo name starts with AWS_ACCOUNT_NO.dkr.ecr.YOUR_REGION_NAME.amazonaws.com in the login command

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Stefanos Asl.
Solution 2 Dileep Jayasundara
Solution 3