Jenkinsfile-dev 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. pipeline {
  2. agent {
  3. label "swarm"
  4. }
  5. options {
  6. disableConcurrentBuilds()
  7. timeout(time: 20, unit: 'MINUTES')
  8. }
  9. environment {
  10. DOCKER_IMAGE='iru/promo'
  11. SWARM_GIT_URL='ssh://git@git.sdsys.ru:8022/iru/stack-deploy.git'
  12. SWARM_GIT_NAME='stack-deploy'
  13. JENKINS_MAIL='jenkins@sdsys.ru'
  14. PACKAGE_NAME='web-registry-promo'
  15. DOCKER_CERT_PATH='/run/secrets/swarm'
  16. DHOST='tcp://dev-iru-swarm.infoclinica.lan:2376 DOCKER_TLS_VERIFY=1'
  17. DOCKER_REGISTRY='dev-registry.infoclinica.ru:5000'
  18. NEWTAG=''
  19. }
  20. parameters {
  21. string(
  22. name: "branch",
  23. defaultValue: "release",
  24. description: "Which branch to use"
  25. )
  26. string(
  27. name: "service_update",
  28. defaultValue: "info_promo",
  29. description: "Services to update."
  30. )
  31. string(
  32. name: "mailto",
  33. defaultValue: "admin@sdsys.ru",
  34. description: "Email which has to be notified."
  35. )
  36. }
  37. stages {
  38. stage("Build") {
  39. steps {
  40. echo "Building ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${BUILD_NUMBER}."
  41. withCredentials([sshUserPrivateKey(credentialsId: 'provision', keyFileVariable: 'GIT_SSH_KEY', passphraseVariable: '', usernameVariable: 'GIT_SSH_USERNAME')]) {
  42. sh """set +x
  43. cat ${GIT_SSH_KEY} > ${WORKSPACE}/id_rsa
  44. chmod 600 ${WORKSPACE}/id_rsa
  45. """
  46. }
  47. sh "docker build --build-arg branch=${branch} --no-cache -t ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${BUILD_NUMBER} ."
  48. script {
  49. NEWTAG = sh(script: "docker run --rm ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${BUILD_NUMBER} cat /tmp/version 2> /dev/null", returnStdout: true).trim()
  50. NEWTAG = branch + "-" + NEWTAG + "_" + BUILD_NUMBER
  51. echo "NEWTAG is $NEWTAG"
  52. }
  53. sh "docker tag ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${BUILD_NUMBER} ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${NEWTAG}"
  54. }
  55. }
  56. stage("Publish") {
  57. steps {
  58. echo "Publishing ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${NEWTAG}"
  59. sh "docker push ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${NEWTAG}"
  60. }
  61. }
  62. stage("Update") {
  63. when { expression { service_update != "" } }
  64. steps {
  65. script {
  66. for (String item : service_update.split()) {
  67. echo "Updating ${item}"
  68. try{
  69. sh "DOCKER_HOST=${DHOST} docker service update ${item} --image ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${NEWTAG}"
  70. }
  71. catch(err){
  72. echo "Recovering service $item"
  73. sh "DOCKER_HOST=${DHOST} docker service rollback ${item}"
  74. throw err
  75. }
  76. }
  77. }
  78. }
  79. }
  80. stage("Tagging"){
  81. steps{
  82. echo "Setting latest tag"
  83. sh """docker tag ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${NEWTAG} ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:latest
  84. docker push ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:latest"""
  85. echo "Updating tag info in ${SWARM_GIT_NAME} repository"
  86. withCredentials([sshUserPrivateKey(credentialsId: 'provision', keyFileVariable: 'GIT_SSH_KEY', passphraseVariable: '', usernameVariable: 'GIT_SSH_USERNAME')]) {
  87. sh """GIT_SSH_COMMAND='ssh -i ${GIT_SSH_KEY} -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' \
  88. git clone ${SWARM_GIT_URL}
  89. cd ${SWARM_GIT_NAME}
  90. echo -n ${NEWTAG} > tags/${DOCKER_IMAGE}.dev-version
  91. git add -A
  92. git config --global user.email "${JENKINS_MAIL}"
  93. git config --global user.name "Jenkins"
  94. git commit -m 'Version update'
  95. GIT_SSH_COMMAND='ssh -i ${GIT_SSH_KEY} -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' \
  96. git push origin master
  97. """
  98. }
  99. }
  100. }
  101. }
  102. post {
  103. always {
  104. echo "CleaningUp work diretory"
  105. deleteDir()
  106. sh "docker image rm -f `docker image ls -q ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${BUILD_NUMBER}`"
  107. }
  108. failure {
  109. mail charset: 'UTF-8',
  110. subject: "Jenkins build ERROR",
  111. mimeType: 'text/html',
  112. to: "${mailto}",
  113. 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}"
  114. }
  115. }
  116. }