Browse Source

Update Jenkinsfile to alpha version

Владимир Томишинец 5 years ago
parent
commit
ccb716cce5
1 changed files with 106 additions and 18 deletions
  1. 106 18
      updateVersionJenkinsfile

+ 106 - 18
updateVersionJenkinsfile

@@ -1,8 +1,12 @@
 // def ENAMES = [ 'prod', 'dev' ]
 // def ENAMES = [ 'prod', 'dev' ]
-def ENAMES = [ 'dev' ]
+def ENAMES = [ 'prod' ]
 def CLUSTERS = ['prod': 'iru-swarm.infoclinica.lan', 'dev': 'dev-iru-swarm.infoclinica.lan']
 def CLUSTERS = ['prod': 'iru-swarm.infoclinica.lan', 'dev': 'dev-iru-swarm.infoclinica.lan']
 def REGISTRIES = ['prod': 'registry.infoclinica.ru:5000', 'dev': 'dev-registry.infoclinica.ru:5000']
 def REGISTRIES = ['prod': 'registry.infoclinica.ru:5000', 'dev': 'dev-registry.infoclinica.ru:5000']
-
+def DHOST = ['prod': 'tcp://iru-swarm.infoclinica.lan:2376', 'dev': 'tcp://dev-iru-swarm.infoclinica.lan:2376']
+def LIST_SERVICE = []
+def TO_ROLLBACK = []
+def ERROR_JOB
+def VERSION_TO_UPDATE
 pipeline {
 pipeline {
   agent {
   agent {
     label "swarm"
     label "swarm"
@@ -10,8 +14,9 @@ pipeline {
   environment {
   environment {
     SWARM_GIT_URL='ssh://git@git.sdsys.ru:8022/labportal/stack-deploy.git'
     SWARM_GIT_URL='ssh://git@git.sdsys.ru:8022/labportal/stack-deploy.git'
     SWARM_GIT_NAME='stack-deploy'
     SWARM_GIT_NAME='stack-deploy'
-    JENKINS_MAIL='jenkins@sdsys.ru'
-    DHOST='tcp://dev-iru-swarm.infoclinica.lan:2376 DOCKER_TLS_VERIFY=1'
+    JENKINS_MAIL='jenkins.dev@sdsys.ru'
+    DOCKER_CERT_PATH='/run/secrets/swarm'
+    DOCKER_IMAGE='analis-wineservice'
   }
   }
   parameters {
   parameters {
     string(
     string(
@@ -19,34 +24,117 @@ pipeline {
       defaultValue: "tomishinets.v@sdsys.ru",
       defaultValue: "tomishinets.v@sdsys.ru",
       description: "Email which has to be notified."
       description: "Email which has to be notified."
     )
     )
+    choice (
+      choices: 'up\ndown',
+      description: 'What you want? Up version or down?',
+      name: 'TASK_ACTION'
+    )
   }
   }
   stages {
   stages {
     stage("Check Stable Version") {
     stage("Check Stable Version") {
       steps {
       steps {
-        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_NAME}
-             """
-          def STABLE_VERSION = sh (script: "cat ${SWARM_GIT_NAME}/tags/lab/analis-wineservice.stable-version" , returnStdout: true).trim()
-          echo STABLE_VERSION
+        script {
+          withCredentials([sshUserPrivateKey(credentialsId: 'provision', keyFileVariable: 'GIT_SSH_KEY', passphraseVariable: '', usernameVariable: 'GIT_SSH_USERNAME')]) {
+            sh """set +x && GIT_SSH_COMMAND='ssh -i ${GIT_SSH_KEY} -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' \
+                    git clone ${SWARM_GIT_URL}
+               """
+          }
+          switch (TASK_ACTION) {
+            case 'up':
+              VERSION_TO_UPDATE = sh (script: "set +x && cat $SWARM_GIT_NAME/tags/lab/analis-wineservice.build-version" , returnStdout: true).trim()
+              sh "set +x && echo \"Update version of Apps to $VERSION_TO_UPDATE\" > commit.txt"
+              break
+            case 'down':
+              VERSION_TO_UPDATE = sh (script: "set +x && cat $SWARM_GIT_NAME/tags/lab/analis-wineservice.old-stable-version" , returnStdout: true).trim()
+              sh "set +x && echo \"Downgrade version of Apps to $VERSION_TO_UPDATE\" > commit.txt"
+              break
+          }
+        }
+      }
+    }
+    stage("Get Names Of Running Services") {
+      steps {
+        script {
+          ENAMES.each { item ->
+            echo "Viewing: ${item} services, on ${CLUSTERS.get((item))}"
+            LIST_SERVICE = sh (script: "set +x && DOCKER_HOST=${DHOST.get((item))} DOCKER_TLS_VERIFY=1 docker service ls -f label=com.docker.stack.namespace=analis-wineservice --format '{{.Name}}'" , returnStdout: true).split('\n')
+            switch (TASK_ACTION) {
+              case 'up':
+                echo "These services will be updated $LIST_SERVICE to version $VERSION_TO_UPDATE"
+                break
+              case 'down':
+                echo "These services will be downgraded $LIST_SERVICE to version $VERSION_TO_UPDATE"
+                break
+            }
+          }
         }
         }
       }
       }
     }
     }
+    stage("Update Services") {
+      steps {
+        script {
+          ENAMES.each { item ->
+            try {
+              for (name in LIST_SERVICE) {
+                TO_ROLLBACK.add(name)
+                sh "set +x && DOCKER_HOST=${DHOST.get((item))} DOCKER_TLS_VERIFY=1 docker service update $name --image ${REGISTRIES.get((item))}/lab/$DOCKER_IMAGE:$VERSION_TO_UPDATE"
+              }
+            }
+            catch (err) {
+              def STABLE_VERSION = sh (script: "set +x && cat $SWARM_GIT_NAME/tags/lab/analis-wineservice.stable-version" , returnStdout: true).trim()
+              for (names_falure in TO_ROLLBACK) {
+                echo "Recoverig $names_falure to $STABLE_VERSION"
+                sh "set +x && DOCKER_HOST=${DHOST.get((item))} DOCKER_TLS_VERIFY=1 docker service rollback $names_falure"
+              }
+              ERROR_JOB = TO_ROLLBACK.last()
+              currentBuild.result = 'FAILURE'
+              error ("Failure on update $ERROR_JOB. The service $TO_ROLLBACK was rollback!")
+            }
+          }
+        }
+      }
+    }
+    stage("Update Tags") {
+      steps {
+        script {
+          def STABLE_VERSION = sh (script: "set +x && cat $SWARM_GIT_NAME/tags/lab/analis-wineservice.stable-version" , returnStdout: true).trim()
+          def OLD_STABLE_VERSION = sh (script: "set +x && cat $SWARM_GIT_NAME/tags/lab/analis-wineservice.old-stable-version" , returnStdout: true).trim()
+          sh "set +x && echo $STABLE_VERSION > $SWARM_GIT_NAME/tags/lab/analis-wineservice.old-stable-version"
+          sh "set +x && echo $VERSION_TO_UPDATE > $SWARM_GIT_NAME/tags/lab/analis-wineservice.stable-version" 
+        }
+      }
+    }
+    stage("Push New Versions To Git") {
+      steps {
+        script {
+          sh "cat commit.txt"
+          sh "cat $SWARM_GIT_NAME/tags/lab/analis-wineservice.stable-version"
+          sh "cat $SWARM_GIT_NAME/tags/lab/analis-wineservice.old-stable-version"
+/*          withCredentials([sshUserPrivateKey(credentialsId: 'provision', keyFileVariable: 'GIT_SSH_KEY', passphraseVariable: '', usernameVariable: 'GIT_SSH_USERNAME')]) {
+            sh """cd $SWARM_GIT_NAME
+                  git add -A
+                  git config --global user.email "${JENKINS_MAIL}"
+                  git config --global user.name "Jenkins"
+                  git commit -F ../commit.txt
+                  GIT_SSH_COMMAND='ssh -i ${GIT_SSH_KEY} -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' \
+                  git push origin master
+               """
+          }
+*/        }
+      }
+    }
   }
   }
   post {
   post {
+    always {
+      echo "CleaningUp work directory"
+      deleteDir()
+    }
     failure {
     failure {
       mail charset: 'UTF-8',
       mail charset: 'UTF-8',
            subject: "Jenkins build ERROR",
            subject: "Jenkins build ERROR",
            mimeType: 'text/html',
            mimeType: 'text/html',
            to: "${mailto}",
            to: "${mailto}",
-           body: "<b>ATTENTION!!!</b> Jenkins job <b>FAILED.</b>\n\n <br>Project Name: ${env.JOB_NAME}\n <br>Build TAG: <b>${REV_NUMBER}-${BUILD_NUMBER}</b>\n <br>Build URL: ${RUN_DISPLAY_URL}"
-    }
-    success {
-      mail charset: 'UTF-8',
-           subject: "Jenkins build SUSCCESS",
-           mimeType: 'text/html',
-           to: "${mailto}",
-           body: "<b>Congradulations!!!</b> Jenkins job successfully finished\n\n <br>Project Name: ${env.JOB_NAME}\n <br>Build TAG: <b>${REV_NUMBER}-${BUILD_NUMBER}</b>\n <br>Build URL: ${RUN_DISPLAY_URL}"
+           body: "<b>ATTENTION!!!</b> <br> <b>Failure update:</b> $ERROR_JOB <br><b>Was rollback:</b> $TO_ROLLBACK <b><br>Project Name:</b> ${env.JOB_NAME} <b><br>Build Number:</b> ${env.BUILD_NUMBER} <b><br>URL Build:</b> ${RUN_DISPLAY_URL}"
     }
     }
   }
   }
 }
 }