Browse Source

Implementing NEWTAG, code optimization

Vadim Surkov 6 years ago
parent
commit
ca2b1fec64
1 changed files with 24 additions and 33 deletions
  1. 24 33
      Jenkinsfile

+ 24 - 33
Jenkinsfile

@@ -11,6 +11,7 @@ pipeline {
     JENKINS_MAIL='jenkins@sdsys.ru'
     CLUSTER_DEV='dev-iru-swarm.infoclinica.lan'
     CLUSTER_PROD='iru-swarm.infoclinica.ru'
+    PACKAGE_NAME='web-registry-admin'
   }
   parameters {
     string(
@@ -40,61 +41,51 @@ pipeline {
         echo "Building ${DOCKER_REGISTRY}/iru/${DOCKER_IMAGE}:${repo}-${BUILD_NUMBER}."
         sh "docker build --build-arg repo=${repo} --no-cache -t ${DOCKER_REGISTRY}/iru/${DOCKER_IMAGE}:${repo}-${BUILD_NUMBER} ."
         script {
-           NEWTAG = sh script: "docker run --rm ${DOCKER_REGISTRY}/iru/${DOCKER_IMAGE}:${repo}-${BUILD_NUMBER} rpm -q --qf %{VERSION}-%{RELEASE} web-registry-admin 2> /dev/null", returnStdout: true
-           NEWTAG = NEWTAG + "_" + BUILD_NUMBER
+           NEWTAG = sh script: "docker run --rm ${DOCKER_REGISTRY}/iru/${DOCKER_IMAGE}:${repo}-${BUILD_NUMBER} rpm -q --qf %{VERSION}-%{RELEASE} ${PACKAGE_NAME} 2> /dev/null", returnStdout: true
+           NEWTAG = repo + "-" + NEWTAG + "_" + BUILD_NUMBER
            echo "NEWTAG is $NEWTAG"
         }
+        sh "docker tag ${DOCKER_REGISTRY}/iru/${DOCKER_IMAGE}:${repo}-${BUILD_NUMBER} ${DOCKER_REGISTRY}/iru/${DOCKER_IMAGE}:${NEWTAG}"
       }
     }
     stage("Publish") {
       steps {
-        echo "Tag is $NEWTAG"
-        error "Stop pipeline"
-        echo "Publishing ${DOCKER_REGISTRY}/iru/${DOCKER_IMAGE}:${repo}-${BUILD_NUMBER}"
-        sh "docker push ${DOCKER_REGISTRY}/iru/${DOCKER_IMAGE}:${repo}-${BUILD_NUMBER}"
+        echo "Publishing ${DOCKER_REGISTRY}/iru/${DOCKER_IMAGE}:${NEWTAG}"
+        sh "docker push ${DOCKER_REGISTRY}/iru/${DOCKER_IMAGE}:${NEWTAG}"
       }
     }
     stage("Update") {
       when { expression { service_update != "" } }
       steps {
         script {
+          switch (cluster) {
+             case 'prod':
+                CLUSTER=CLUSTER_PROD
+                break
+             case 'dev':
+                CLUSTER=CLUSTER_DEV
+                break
+             default:
+                currentBuild.result = 'ABORTED'
+                error('Unknown cluster')
+          }
           for (String item : service_update.split()) {
             try{
               echo "Updating $item"
-              if (cluster == 'prod') {
-                withEnv(["service_name=${item}"]) {
+              withEnv(["service_name=${item}"]) {
                   sh '''export DOCKER_CERT_PATH=/run/secrets/swarm
-                        export DOCKER_HOST=tcp://${CLUSTER_PROD}:2376 DOCKER_TLS_VERIFY=1
-                        docker service update ${service_name} --image ${DOCKER_REGISTRY}/iru/${DOCKER_IMAGE}:${repo}-${BUILD_NUMBER}
+                        export DOCKER_HOST=tcp://${CLUSTER}:2376 DOCKER_TLS_VERIFY=1
+                        docker service update ${service_name} --image ${DOCKER_REGISTRY}/iru/${DOCKER_IMAGE}:${NEWTAG}
                       '''
                 }
-              }
-              if (cluster == 'dev') {
-                withEnv(["service_name=${item}"]) {
-                  sh '''export DOCKER_CERT_PATH=/run/secrets/swarm
-                        export DOCKER_HOST=tcp://${CLUSTER_DEV}:2376 DOCKER_TLS_VERIFY=1
-                        docker service update ${service_name} --image ${DOCKER_REGISTRY}/iru/${DOCKER_IMAGE}:${repo}-${BUILD_NUMBER}
-                     '''
-                }
-              }
             }
             catch(err){
               echo "Recovering service $item"
-              if (cluster == 'prod') {
-                withEnv(["service_name=${item}"]) {
+              withEnv(["service_name=${item}"]) {
                   sh '''export DOCKER_CERT_PATH=/run/secrets/swarm
-                        export DOCKER_HOST=tcp://${CLUSTER_PROD}:2376 DOCKER_TLS_VERIFY=1
+                        export DOCKER_HOST=tcp://${CLUSTER}:2376 DOCKER_TLS_VERIFY=1
                         docker service rollback ${service_name}
                      '''
-                }
-              }
-              if (cluster == 'dev') {
-                withEnv(["service_name=${item}"]) {
-                  sh '''export DOCKER_CERT_PATH=/run/secrets/swarm
-                        export DOCKER_HOST=tcp://${CLUSTER_DEV}:2376 DOCKER_TLS_VERIFY=1
-                        docker service rollback ${service_name}
-                     '''
-                }
               }
               throw err
             }
@@ -105,14 +96,14 @@ pipeline {
     stage("Tagging"){
       steps{
         echo "Setting latest tag"
-        sh '''docker tag ${DOCKER_REGISTRY}/iru/${DOCKER_IMAGE}:${repo}-${BUILD_NUMBER} ${DOCKER_REGISTRY}/iru/${DOCKER_IMAGE}:latest
+        sh '''docker tag ${DOCKER_REGISTRY}/iru/${DOCKER_IMAGE}:${NEWTAG} ${DOCKER_REGISTRY}/iru/${DOCKER_IMAGE}:latest
               docker push ${DOCKER_REGISTRY}/iru/${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 ${repo}-${BUILD_NUMBER} > tags/${DOCKER_IMAGE}.version
+                echo -n ${NEWTAG} > tags/${DOCKER_IMAGE}.version
                 git add -A
                 git config --global user.email "${JENKINS_MAIL}"
                 git config --global user.name "Jenkins"