Created
August 13, 2020 07:13
-
-
Save ismailyenigul/2578d05849e7acc7bf812008479c3eab to your computer and use it in GitHub Desktop.
Jenkins pipeline to build docker image and push to ecr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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