Skip to content

Instantly share code, notes, and snippets.

@fourgates
Last active July 18, 2022 21:29
Show Gist options
  • Save fourgates/75ad63b940577fb122e5de09e790c31f to your computer and use it in GitHub Desktop.
Save fourgates/75ad63b940577fb122e5de09e790c31f to your computer and use it in GitHub Desktop.
ECS Jenikinsfile
pipeline {
agent any
environment {
// these change whenever we redeploy IaC
taskDef = "XXX" // name of the ECS task in which we will be adding a new revision
taskFamily = "XXX" // task family for service
serviceName = "XXX" // which service to update in the cluster
dockerBuildService = "deploy-dev"// docker service that builds deployable image
dockerImage = "XXX" // name of deployable image
ecr = "XXX" // where is the ecr repo
ecrRepo = "XXX" // name of the repo in ecr, this is where the deployable image will be pushed
cluster = "XXX" // which cluster to update
jobPath= "XXX"
}
stages {
stage ('Pull Latest'){
steps{
script{
sh 'echo ${BUILD_NUMBER}'
def b = sh 'echo ${BUILD_NUMBER}'
git branch: 'develop', credentialsId: 'bit', url: 'XXX'
}
}
}
stage ('Bump Prerelease'){
steps{
dir('api'){
script{
sh 'docker run -v "${jobPath}":/build/pom.xml -w /build my/repo/maven/builder:0.6 mvn build-helper:parse-version versions:set -DnewVersion=\\\${parsedVersion.majorVersion}.\\\${parsedVersion.minorVersion}.\\\${parsedVersion.incrementalVersion}-B${BUILD_NUMBER} versions:commit'
}
}
}
}
stage ('Build Backend'){
steps{
dir("api") {
script{
docker.withRegistry('https://mydockerrepo.com', 'mydocker-repo-creds') {
sh 'docker-compose build ${dockerBuildService}'
sh 'docker tag ${dockerImage} ${ecr}/${ecrRepo}:${BUILD_NUMBER}'
}
}
}
}
}
stage ('Push to ECR'){
steps{
dir("api"){
script{
sh 'aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin ${ecr}'
sh 'docker push ${ecr}/${ecrRepo}:${BUILD_NUMBER}'
}
}
}
}
stage ('Create Task'){
steps{
dir('api'){
sh 'sed -e "s;%IMAGE_VERSION%;${BUILD_NUMBER};g" tasks/${cluster}/task.json > tasks/${cluster}/task-v_${BUILD_NUMBER}-${cluster}.json'
sh 'aws ecs register-task-definition --family ${taskFamily} --cli-input-json file://tasks/${cluster}/task-v_${BUILD_NUMBER}-${cluster}.json'
}
}
}
stage('Deploy to ECS') {
steps {
script{
def tr = sh (returnStdout: true,
script: 'aws ecs describe-task-definition --task-definition ' + env.taskDef + ' | egrep "revision" | tr "/" " " | awk \'{print $2}\' | sed \'s/"$//\' | tr "," " " | awk \'{$1=$1};1\'')
echo tr
def count = sh(returnStdout: true,
script: 'aws ecs describe-services --cluster ' + env.cluster + ' --services ' + env.serviceName + ' | egrep "desiredCount" | tr "/" " " | awk \'{print $2}\' | sed \'s/,$//\' | egrep -m 1 ""')
echo count
env.tr = tr
env.count = count
}
sh 'aws ecs update-service --cluster ${cluster} --service ${serviceName} --task-definition ${taskFamily}:$tr --desired-count $count'
}
}
}
post {
success {
echo 'This will run only if successful'
}
failure {
script{
def summary = "Job Failed~: ${env.JOB_NAME} - (${env.BUILD_URL})"
slackSend (tokenCredentialId: 'slack-token-XXX', channel: 'devops-XXX', message: summary)
}
emailext (
subject: "CI Failure: XXX -> ${env.JOB_NAME}",
body: "<br>XXX: ${env.JOB_NAME} <br>Build Number: ${env.BUILD_NUMBER} <br> URL de build: ${env.BUILD_URL}",
recipientProviders: [[$class: 'DevelopersRecipientProvider']],
to: "XXX"
)
}
unstable {
echo 'This will run only if the run was marked as unstable'
}
changed {
script{
if(currentBuild.result == "SUCCESS"){
echo 'This will run only if the state of the Pipeline has changed'
echo 'For example, if the Pipeline was previously failing but is now successful'
script{
def summary = "Job Back to Normal~: ${env.JOB_NAME} - (${env.BUILD_URL})"
slackSend (tokenCredentialId: 'slack-token-XXX', channel: 'devops-XXX', message: summary)
}
emailext (
subject: "CI Back to Normal: XXX -> ${env.JOB_NAME}",
body: "<br>XXX: ${env.JOB_NAME} <br>Build Number: ${env.BUILD_NUMBER} <br> URL de build: ${env.BUILD_URL}",
recipientProviders: [[$class: 'DevelopersRecipientProvider']],
to: "XXX"
)
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment