From 0d30b13b82d06825bf2db92cd8924fa7fdce12f2 Mon Sep 17 00:00:00 2001 From: Sebastian Ostrowski Date: Sun, 3 Dec 2023 13:49:46 -0600 Subject: [PATCH 1/2] Add Slack to Jenkins pipeline --- Jenkinsfile | 83 +++++++++++++++++++++++++++++++++++------------------ 1 file changed, 55 insertions(+), 28 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index f0e2d04..b0074ef 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -5,63 +5,81 @@ pipeline { stage('build (stage)') { when { branch 'stage' } steps { + slackMessage("Building..", "good") checkout scm script { - head_hash = sh(script:"git rev-parse HEAD | head -c 8", returnStdout:true).trim() - sh """ - docker build --build-arg MATOMO_SITE_ID=10 -t annorep:${head_hash} -f Dockerfile . - """ - } + try { + head_hash = sh(script:"git rev-parse HEAD | head -c 8", returnStdout:true).trim() + sh """ + docker build --build-arg MATOMO_SITE_ID=10 -t annorep:${head_hash} -f Dockerfile . + """ + } catch (Exception e) { + slackMessage(e.toString(), "danger") + } + } } } stage('build (main)') { when { branch 'main' } steps { + slackMessage("Building..", "good") checkout scm script { - head_hash = sh(script:"git rev-parse HEAD | head -c 8", returnStdout:true).trim() - sh """ - docker build --build-arg MATOMO_SITE_ID=4 -t annorep:${head_hash} -f Dockerfile . - """ + try { + head_hash = sh(script:"git rev-parse HEAD | head -c 8", returnStdout:true).trim() + sh """ + docker build --build-arg MATOMO_SITE_ID=4 -t annorep:${head_hash} -f Dockerfile . + """ + } catch (Exception e) { + slackMessage(e.toString(), "danger") + } } } } stage('push') { when { anyOf { branch 'stage'; branch 'main' } } steps { + slackMessage("Pushing..", "good") checkout scm - withCredentials([ string(credentialsId: 'AWS_REGION', variable: 'AWS_REGION'), string(credentialsId: 'AWS_ACCOUNT_ID', variable: 'AWS_ACCOUNT_ID'), string(credentialsId: 'ANNOREP_ECR_NAME', variable: 'ANNOREP_ECR_NAME'), string(credentialsId: 'ANNOREP_DEPLOY_USER', variable: 'ANNOREP_DEPLOY_USER'), ]) { - script { - head_hash = sh(script:"git rev-parse HEAD | head -c 8", returnStdout:true).trim() - sh """ - aws ecr get-login-password --region ${AWS_REGION} | docker login --username AWS --password-stdin ${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com - docker tag annorep:${head_hash} ${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com/${ANNOREP_ECR_NAME}:${head_hash} - docker push ${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com/${ANNOREP_ECR_NAME}:${head_hash} - """ + script { + try { + head_hash = sh(script:"git rev-parse HEAD | head -c 8", returnStdout:true).trim() + sh """ + aws ecr get-login-password --region ${AWS_REGION} | docker login --username AWS --password-stdin ${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com + docker tag annorep:${head_hash} ${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com/${ANNOREP_ECR_NAME}:${head_hash} + docker push ${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com/${ANNOREP_ECR_NAME}:${head_hash} + """ + } catch (Exception e) { + slackMessage(e.toString(), "danger") + } + } } - } } } stage('deploy (stage)') { when { branch 'stage' } steps { + slackMessage("Deploying..", "good") checkout scm - withCredentials([ string(credentialsId: 'ANNOREP_DEPLOY_USER', variable: 'ANNOREP_DEPLOY_USER'), string(credentialsId: 'QDR_STAGE', variable: 'QDR_STAGE'), ]) { script { - head_hash = sh(script:"git rev-parse HEAD | head -c 8", returnStdout:true).trim() - sh """ - ssh ${ANNOREP_DEPLOY_USER}@${QDR_STAGE} annorep-deploy ${head_hash} - """ + try { + head_hash = sh(script:"git rev-parse HEAD | head -c 8", returnStdout:true).trim() + sh """ + ssh ${ANNOREP_DEPLOY_USER}@${QDR_STAGE} annorep-deploy ${head_hash} + """ + } catch (Exception e) { + slackMessage(e.toString(), "danger") + } } } } @@ -69,20 +87,29 @@ pipeline { stage('deploy (main)') { when { branch 'main' } steps { + slackMessage("Deploying..", "good") checkout scm - withCredentials([ string(credentialsId: 'ANNOREP_DEPLOY_USER', variable: 'ANNOREP_DEPLOY_USER'), string(credentialsId: 'QDR_PROD', variable: 'QDR_PROD'), ]) { script { - head_hash = sh(script:"git rev-parse HEAD | head -c 8", returnStdout:true).trim() - sh """ - ssh ${ANNOREP_DEPLOY_USER}@${QDR_PROD} annorep-deploy ${head_hash} - """ + try { + head_hash = sh(script:"git rev-parse HEAD | head -c 8", returnStdout:true).trim() + sh """ + ssh ${ANNOREP_DEPLOY_USER}@${QDR_PROD} annorep-deploy ${head_hash} + """ + } catch (Exception e) { + slackMessage(e.toString(), "danger") + } } } } } } } + +@NonCPS +def slackMessage(String message, String color) { + slackSend message: "<$JOB_URL|$JOB_NAME>: ${message}", color: "${color}" +} From bd8eb21a3b1c3667fee1f6fbd09b062b8e3a540f Mon Sep 17 00:00:00 2001 From: Sebastian Ostrowski Date: Sun, 3 Dec 2023 14:05:42 -0600 Subject: [PATCH 2/2] Add final slack message --- Jenkinsfile | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Jenkinsfile b/Jenkinsfile index b0074ef..61bee82 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -82,6 +82,7 @@ pipeline { } } } + slackMessage("Success", "good") } } stage('deploy (main)') { @@ -104,6 +105,7 @@ pipeline { } } } + slackMessage("Success", "good") } } }