Jenkinsfile-dev 5.0 KB

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