Pages

Thursday, December 17, 2020

Jenkins Pipeline to update Docker Image

 We basically pull the Docker image from DEV ECR and Docker push to the platform & Region's ECR and then update the Deployment file and apply the kubernetes deployment.

Keywords : Pipeline, Multiple Condition(SwitchCase), e-mail

pipeline {

  agent {

    label 'appjenkins-slave'

  }

    options {

        ansiColor('xterm')

        timestamps ()

    }

    parameters {

    choice( choices: ['preprod-us' , 'preprod-eu' , 'preprod-de'], description: 'Region', name: 'EnV')

    string(name: 'DockerVersion', defaultValue: 'v1.1', description:'Docker image Version')

string(name: 'AccountNumber', defaultValue: '061320014550', description:'Account Number')

    }


stages {

stage('Docker Pull') {

steps {

withCredentials([[$class: 'AmazonWebServicesCredentialsBinding', accessKeyVariable: 'AWS_ACCESS_KEY_ID', credentialsId: 'app-terraform@506077034049@APPortal-DEV', secretKeyVariable: 'AWS_SECRET_ACCESS_KEY']]) {

sh label: '', script: '''#!/bin/bash -e

echo $(/usr/local/bin/aws ecr get-authorization-token --region us-east-1 --output text --query 'authorizationData[].authorizationToken' | base64 -d | cut -d: -f2) | docker login -u AWS https://506077034049.dkr.ecr.us-east-1.amazonaws.com --password-stdin

docker pull 506077034049.dkr.ecr.us-east-1.amazonaws.com/ssp/backend:${DockerVersion}

'''

}

}

}

stage('Analyze Platform') {

parallel {

stage ("PREPROD-US") {

when { expression { params.EnV == 'preprod-us' } }

steps {

withCredentials([[$class: 'AmazonWebServicesCredentialsBinding', accessKeyVariable: 'AWS_ACCESS_KEY_ID', credentialsId: 'app-terraform@061320014550@APPortal-PREPROD', secretKeyVariable: 'AWS_SECRET_ACCESS_KEY']]) {

sh label: '', script: '''#!/bin/bash -e

SCM_URI="${AccountNumber}.dkr.ecr.us-east-1.amazonaws.com"

docker tag 506077034049.dkr.ecr.us-east-1.amazonaws.com/ssp/backend:${DockerVersion} ${SCM_URI}/ssp/backend:${DockerVersion}

echo $(/usr/local/bin/aws ecr get-authorization-token --region us-east-1 --output text --query 'authorizationData[].authorizationToken' | base64 -d | cut -d: -f2) | docker login -u AWS https://${SCM_URI} --password-stdin

docker push ${SCM_URI}/ssp/backend

sudo -u ssp-${EnV} /bin/bash -c "id; pwd"

                         echo; echo Update IMAGE URL; echo '======================'

sudo grep image: /home/ssp-${EnV}/sspbackend.yaml | cut -d":" -f 3

IMAGEVER=`sudo grep image: /home/ssp-${EnV}/sspbackend.yaml | cut -d":" -f 3`

echo "Existing Version: "$IMAGEVER

  echo "Need to Replace with: "$DockerVersion

sudo sed -i "s/$IMAGEVER/$DockerVersion/g" /home/ssp-${EnV}/sspbackend.yaml

sudo cat /home/ssp-${EnV}/sspbackend.yaml

sudo -u ssp-${EnV} /usr/local/bin/kubectl apply -f /home/ssp-${EnV}/sspbackend.yaml

'''

}

}

}

stage ("PREPROD-EU") {

when { expression { params.EnV == 'preprod-eu' } }

steps {

withCredentials([[$class: 'AmazonWebServicesCredentialsBinding', accessKeyVariable: 'AWS_ACCESS_KEY_ID', credentialsId: 'app-terraform@061320014550@APPortal-PREPROD', secretKeyVariable: 'AWS_SECRET_ACCESS_KEY']]) {

sh label: '', script: '''#!/bin/bash -e

SCM_URI="${AccountNumber}.dkr.ecr.eu-west-1.amazonaws.com"

docker tag 506077034049.dkr.ecr.us-east-1.amazonaws.com/ssp/backend:${DockerVersion} ${SCM_URI}/ssp/backend:${DockerVersion}

echo $(/usr/local/bin/aws ecr get-authorization-token --region eu-west-1 --output text --query 'authorizationData[].authorizationToken' | base64 -d | cut -d: -f2) | docker login -u AWS https://${SCM_URI} --password-stdin

docker push ${SCM_URI}/ssp/backend

sudo -u ssp-${EnV} /bin/bash -c "id; pwd"

                         echo; echo Update IMAGE URL; echo '======================'

sudo grep image: /home/ssp-${EnV}/sspbackend.yaml | cut -d":" -f 3

IMAGEVER=`sudo grep image: /home/ssp-${EnV}/sspbackend.yaml | cut -d":" -f 3`

echo "Existing Version: "$IMAGEVER

  echo "Need to Replace with: "$DockerVersion

sudo sed -i "s/$IMAGEVER/$DockerVersion/g" /home/ssp-${EnV}/sspbackend.yaml

sudo cat /home/ssp-${EnV}/sspbackend.yaml

sudo -u ssp-${EnV} /usr/local/bin/kubectl apply -f /home/ssp-${EnV}/sspbackend.yaml

'''

}

}

}

stage ("PREPROD-DE") {

when { expression { params.EnV == 'preprod-de' } }

steps {

withCredentials([[$class: 'AmazonWebServicesCredentialsBinding', accessKeyVariable: 'AWS_ACCESS_KEY_ID', credentialsId: 'app-terraform@061320014550@APPortal-PREPROD', secretKeyVariable: 'AWS_SECRET_ACCESS_KEY']]) {

sh label: '', script: '''#!/bin/bash -e

SCM_URI="${AccountNumber}.dkr.ecr.eu-central-1.amazonaws.com"

docker tag 506077034049.dkr.ecr.us-east-1.amazonaws.com/ssp/backend:${DockerVersion} ${SCM_URI}/ssp/backend:${DockerVersion}

echo $(/usr/local/bin/aws ecr get-authorization-token --region eu-central-1 --output text --query 'authorizationData[].authorizationToken' | base64 -d | cut -d: -f2) | docker login -u AWS https://${SCM_URI} --password-stdin

docker push ${SCM_URI}/ssp/backend

sudo -u ssp-${EnV} /bin/bash -c "id; pwd"

                         echo; echo Update IMAGE URL; echo '======================'

sudo grep image: /home/ssp-${EnV}/sspbackend.yaml | cut -d":" -f 3

IMAGEVER=`sudo grep image: /home/ssp-${EnV}/sspbackend.yaml | cut -d":" -f 3`

echo "Existing Version: "$IMAGEVER

  echo "Need to Replace with: "$DockerVersion

sudo sed -i "s/$IMAGEVER/$DockerVersion/g" /home/ssp-${EnV}/sspbackend.yaml

sudo cat /home/ssp-${EnV}/sspbackend.yaml

  sudo -u ssp-${EnV} /usr/local/bin/kubectl apply -f /home/ssp-${EnV}/sspbackend.yaml

'''

}

}

}

}

}

stage("Mail Notification") {

steps {

emailext body: '''Deployed the ${DockerVersion} Docker image for SSP-${EnV}-API \n Build Status : ${currentBuild.currentResult} \n Job name: ${JOB_NAME} \n Build Number: ${BUILD_NUMBER} \n More info at: ${BUILD_URL}''', recipientProviders: [[$class: 'RequesterRecipientProvider'], [$class: 'DevelopersRecipientProvider']], replyTo: 'tilak.bhandari@hidglobal.com, naveen.kumar@hidglobal.com, laxmappa.malligawad@hidglobal.com, Sangeeth.VeluthedathSahadevan@hidglobal.com, spurtiganga.hulamani@hidglobal.com, ashwin.nagaraja@hidglobal.com', subject: '${DEFAULT_SUBJECT}', mimeType: 'text/html', to: 'joelandrews.jamesselvam@hidglobal.com, tilak.bhandari@hidglobal.com, naveen.kumar@hidglobal.com, laxmappa.malligawad@hidglobal.com, Sangeeth.VeluthedathSahadevan@hidglobal.com, spurtiganga.hulamani@hidglobal.com, ashwin.nagaraja@hidglobal.com'

}

}

}

}

No comments:

Post a Comment