pipeline {
  agent {
    label "swarm"
  }
  options {
        disableConcurrentBuilds()
        timeout(time: 20, unit: 'MINUTES')
  }
  environment {
    DOCKER_IMAGE='lab/analis-wineservice'
    DOCKER_REGISTRY='registry.infoclinica.ru:5000'
    SWARM_GIT_URL='ssh://git@git.sdsys.ru:8022/labportal/stack-deploy.git'
    SWARM_GIT_NAME='stack-deploy'
    JENKINS_MAIL='jenkins@sdsys.ru'
    DHOST='tcp://dev-iru-swarm.infoclinica.lan:2376 DOCKER_TLS_VERIFY=1'
    NEWTAG=''
    REV_NUMBER=''
  }
  parameters {
    string(
      name: "mailto",
      defaultValue: "admin@sdsys.ru",
      description: "Email which has to be notified."
    )
  }
  stages {
    stage("SVN Checkout") {
      steps {
        sh """[ -d analis/.svn ] && svn cleanup analis
              svn co --force --no-auth-cache svn://192.168.21.250/programs/trunk/Services/Analis/Win32 analis --username public --password "sds#7753435"
           """
      }
    }
    stage("Build") {
      steps {
        script {
          // Discover revision number
          REV_NUMBER=sh (script: "svn info --show-item last-changed-revision analis", returnStdout: true ).trim()
          echo "Last-Changed-Revision number is '$REV_NUMBER'"
        }
        sh "docker build -t ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${REV_NUMBER}-${BUILD_NUMBER} ."
        sh "docker tag ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${REV_NUMBER}-${BUILD_NUMBER} ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:latest"
      }
    }
    stage("Publish") {
      steps {
        sh "docker push ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${REV_NUMBER}-${BUILD_NUMBER}"
        sh "docker push ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:latest"
      }
    }
    stage("Tagging"){
      steps{
        withCredentials([sshUserPrivateKey(credentialsId: 'provision', keyFileVariable: 'GIT_SSH_KEY', passphraseVariable: '', usernameVariable: 'GIT_SSH_USERNAME')]) {
          sh """[ -d ${SWARM_GIT_NAME} ] && rm -rf ${SWARM_GIT_NAME}
                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 ${REV_NUMBER}-${BUILD_NUMBER} > tags/${DOCKER_IMAGE}.build-version
                git add -A
                git config --global user.email "${JENKINS_MAIL}"
                git config --global user.name "Jenkins"
                git commit -m "Version update tags/lab/${DOCKER_IMAGE}.build-version = ${REV_NUMBER}-${BUILD_NUMBER}"
                GIT_SSH_COMMAND='ssh -i ${GIT_SSH_KEY} -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' \
                git push origin master
                cd ..
                rm -rf ${SWARM_GIT_NAME}
                ls"""
        }
      }
    }
  }
  post {
    failure {
      mail charset: 'UTF-8',
           subject: "Jenkins build ERROR",
           mimeType: 'text/html',
           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}"
    }
  }
}