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 . Kindly help
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)'))
CodePudding user response:
Most possibly you also need to programatically(i.e., in your code) add a token for your session.
CodePudding user response:
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()
}
}
}
}
}
}