Jenkinsfile 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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/node'
  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-portal'
  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: "repo",
  23. defaultValue: "prod",
  24. description: "Repository to build and/or deploy from. Values - dev, prod."
  25. )
  26. string(
  27. name: "service_update",
  28. defaultValue: "info_node info_node-api",
  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}:${repo}-${BUILD_NUMBER}."
  41. sh "docker build --build-arg repo=${repo} --no-cache -t ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${repo}-${BUILD_NUMBER} ."
  42. script {
  43. NEWTAG = sh script: "docker run --rm --entrypoint=\"\" ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${repo}-${BUILD_NUMBER} rpm -q --qf %{VERSION}-%{RELEASE} ${PACKAGE_NAME} 2> /dev/null", returnStdout: true
  44. NEWTAG = repo + "-" + NEWTAG + "_" + BUILD_NUMBER
  45. echo "NEWTAG is $NEWTAG"
  46. }
  47. sh "docker tag ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${repo}-${BUILD_NUMBER} ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${NEWTAG}"
  48. }
  49. }
  50. stage("Publish") {
  51. steps {
  52. echo "Publishing ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${NEWTAG}"
  53. sh "docker push ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${NEWTAG}"
  54. }
  55. }
  56. stage("Update") {
  57. when { expression { service_update != "" } }
  58. steps {
  59. script {
  60. for (String item : service_update.split()) {
  61. echo "Updating ${item}"
  62. try{
  63. sh "DOCKER_HOST=${DHOST} docker service update ${item} --image ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${NEWTAG}"
  64. }
  65. catch(err){
  66. echo "Recovering service $item"
  67. sh "DOCKER_HOST=${DHOST} docker service rollback ${item}"
  68. throw err
  69. }
  70. }
  71. }
  72. }
  73. }
  74. stage("Tagging"){
  75. steps{
  76. echo "Setting latest tag"
  77. sh """docker tag ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${NEWTAG} ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:latest
  78. docker push ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:latest"""
  79. echo "Updating tag info in ${SWARM_GIT_NAME} repository"
  80. withCredentials([sshUserPrivateKey(credentialsId: 'provision', keyFileVariable: 'GIT_SSH_KEY', passphraseVariable: '', usernameVariable: 'GIT_SSH_USERNAME')]) {
  81. sh """GIT_SSH_COMMAND='ssh -i ${GIT_SSH_KEY} -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' \
  82. git clone ${SWARM_GIT_URL}
  83. cd ${SWARM_GIT_NAME}
  84. echo -n ${NEWTAG} > tags/${DOCKER_IMAGE}.dev-version
  85. git add -A
  86. git config --global user.email "${JENKINS_MAIL}"
  87. git config --global user.name "Jenkins"
  88. git commit -m 'Version update'
  89. GIT_SSH_COMMAND='ssh -i ${GIT_SSH_KEY} -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' \
  90. git push origin master
  91. ls"""
  92. }
  93. }
  94. }
  95. }
  96. post {
  97. always {
  98. echo "CleaningUp work diretory"
  99. deleteDir()
  100. sh "docker image rm -f `docker image ls -q ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${repo}-${BUILD_NUMBER}`"
  101. }
  102. failure {
  103. mail charset: 'UTF-8',
  104. subject: "Jenkins build ERROR",
  105. mimeType: 'text/html',
  106. to: "${mailto}",
  107. 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}"
  108. }
  109. success {
  110. mail charset: 'UTF-8',
  111. subject: "Jenkins build SUSCCESS",
  112. mimeType: 'text/html',
  113. to: "${mailto}",
  114. body: "<b>Congradulations!!!</b> <b><br> Jenkins job succefully finished.\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}"
  115. }
  116. }
  117. }