Jelajahi Sumber

Jenkinsfile dev

Vadim Surkov 7 tahun lalu
induk
melakukan
5ac8a710e1
1 mengubah file dengan 102 tambahan dan 0 penghapusan
  1. 102 0
      Jenkinsfile

+ 102 - 0
Jenkinsfile

@@ -0,0 +1,102 @@
+pipeline {
+  agent {
+    label "swarm"
+  }
+  environment {
+    DOCKER_REGISTRY='dev-registry.infoclinica.ru:5000'
+    DOCKER_IMAGE='admin'
+    SWARM_GIT_URL='ssh://git@git.sdsys.ru:8022/iru/stack-deploy.git'
+    SWARM_GIT_NAME='stack-deploy'
+    JENKINS_MAIL='jenkins@sdsys.ru'
+  }
+  parameters {
+    string(
+      name: "repo",
+      defaultValue: "prod",
+      description: "Repository to build and/or deploy from."
+    )
+    string(
+      name: "service_update",
+      defaultValue: "info_admin",
+      description: "Services to update."
+    )
+    string(
+      name: "mailto",
+      defaultValue: "admin@sdsys.ru",
+      description: "Email which has to be notified."
+    )
+  }
+  stages {
+    stage("Build") {
+      steps {
+        echo "Building ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${repo}-${BUILD_NUMBER}."
+        sh "docker build --build-arg repo=${repo} --no-cache -t ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${repo}-${BUILD_NUMBER} ."
+      }
+    }
+    stage("Publish") {
+      steps {
+        echo "Publishing ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${repo}-${BUILD_NUMBER}"
+        sh "docker push ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${repo}-${BUILD_NUMBER}"
+      }
+    }
+    stage("Update") {
+      when { expression { service_update != "" } }
+      steps {
+        script {
+          for (String item : service_update.split()) {
+            try{
+              echo "Updating $item"
+              sh "docker service update $item --image ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${repo}-${BUILD_NUMBER}"
+            }
+            catch(err){
+              echo "Recovering service $item"
+              sh "docker service rollback $item"
+              throw err
+            }
+          }
+        }
+      }
+    }
+    stage("Tagging"){
+      steps{
+        echo "Setting latest tag"
+        sh '''docker tag ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${repo}-${BUILD_NUMBER} ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:latest
+              docker push ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:latest'''
+        echo "Updating tag info in ${SWARM_GIT_NAME} repository"
+        withCredentials([sshUserPrivateKey(credentialsId: 'provision', keyFileVariable: 'GIT_SSH_KEY', passphraseVariable: '', usernameVariable: 'GIT_SSH_USERNAME')]) {
+          sh '''GIT_SSH_COMMAND='ssh -i ${GIT_SSH_KEY} -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' \
+                  git clone ${SWARM_GIT_URL}
+                cd ${SWARM_GIT_NAME}
+                echo -n ${BUILD_NUMBER} > tags/${DOCKER_IMAGE}.version
+                git add -A
+                git config --global user.email "${JENKINS_MAIL}"
+                git config --global user.name "Jenkins"
+                git commit -m 'Version update'
+                GIT_SSH_COMMAND='ssh -i ${GIT_SSH_KEY} -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' \
+                  git push origin master
+                ls'''
+        }
+      }
+    }
+  }
+  post {
+    always {
+      echo "CleaningUp work diretory"
+      deleteDir()
+    }
+    failure {
+      mail charset: 'UTF-8',
+           subject: "Jenkins build ERROR",
+           mimeType: 'text/html',
+           to: "${mailto}",
+           body: "<b>ATTENTION!!!</b> <b><br> Jenkins job failed.\n\n <b><br>Project Name:</b> ${env.JOB_NAME} <b><br>\nBuild Number:</b> ${env.BUILD_NUMBER} <b><br>\nURL Build:</b> ${RUN_DISPLAY_URL}"
+    }
+    success {
+      mail charset: 'UTF-8',
+           subject: "Jenkins build SUSCCESS",
+           mimeType: 'text/html',
+           to: "${mailto}",
+           body: "<b>Congradulations!!!</b> <b><br> Jenkins job succefully finished.\n\n <b><br>Project Name:</b> ${env.JOB_NAME} <b><br>\nBuild Number:</b> ${env.BUILD_NUMBER} <b><br>\nURL Build:</b> ${RUN_DISPLAY_URL}"
+    }
+  }
+}