I am getting error while pushing image to AWS ECR through Jenkins Pipeline

66 Views Asked by At

I am getting error while pushing image to AWS ECR using jenkins pipeline. I have already assigned an IAM role with all permissions to the ec2 instance. It is showing retrying seconds and it will fail. This is the pipeline I am using.

pipeline {
    agent any

    environment {
        AWS_REGION = 'ap-south-1'
        ECR_REPO = 'xxxxxxxxxxxx.dkr.ecr.ap-south-1.amazonaws.com'
        REPOSITORY_URI = 'xxxxxxxxxxxx.dkr.ecr.ap-south-1.amazonaws.com/test'
        IMAGE_NAME = 'clone'
    }

        stage('Build and Push to ECR') {
            steps {
                script {
                    catchError {
                        sh "aws ecr get-login-password --region $AWS_REGION | sudo docker login --username AWS --password-stdin $ECR_REPO"
                        sh "cd tinder-clone && sudo docker build -t $IMAGE_NAME ."
                        sh "cd tinder-clone && sudo docker tag $IMAGE_NAME $REPOSITORY_URI/$IMAGE_NAME:latest"
                        sh "cd tinder-clone && sudo docker login --username AWS --password-stdin $ECR_REPO | sudo docker push $REPOSITORY_URI:$IMAGE_NAME:latest" 
                    }
                }
            }
        }
    }
}

I want to push the docker image to ecr using the jenkins pipeline.

1

There are 1 best solutions below

0
M B On

If it is timing out without giving any other errors (repo doesn't exist, credentials are invalid/insufficient), then your Jenkins host likely can't connect to ECR. Open up a terminal in the Jenkins host and use the same commands to push the image to the ECR repo. If it is still timing out, then that shows the problem is with your connectivity. Also, why are you using a | operator in your last command instead of &&?