Skip to content

Instantly share code, notes, and snippets.

@ismailyenigul
Created August 13, 2020 07:13
Show Gist options
  • Save ismailyenigul/2578d05849e7acc7bf812008479c3eab to your computer and use it in GitHub Desktop.
Save ismailyenigul/2578d05849e7acc7bf812008479c3eab to your computer and use it in GitHub Desktop.
Jenkins pipeline to build docker image and push to ecr
pipeline {
environment {
imagename = "ismailtest"
ecrurl = "https://828556645578.dkr.ecr.us-east-2.amazonaws.com"
ecrcredentials = "ecr:us-east-2:ecr-ismail"
dockerImage = ''
}
agent any
stages {
stage('Cloning Git') {
steps {
checkout scm
}
}
stage('Building image') {
steps{
script {
dockerImage = docker.build imagename
}
}
}
stage('Deploy Master Image') {
when {
anyOf {
branch 'master'
}
}
steps{
script {
docker.withRegistry(ecrurl, ecrcredentials) {
dockerImage.push("$BUILD_NUMBER")
dockerImage.push('latest')
}
}
}
}
stage('Remove Unused docker image - Master') {
when {
anyOf {
branch 'master'
}
}
steps{
sh "docker rmi $imagename:$BUILD_NUMBER"
sh "docker rmi $imagename:latest"
}
} // End of remove unused docker image for master
}
} //end of pipeline
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment