Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Omisamuel/34d99fef9c21c014cd63a46522b44223 to your computer and use it in GitHub Desktop.
Save Omisamuel/34d99fef9c21c014cd63a46522b44223 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