Jenkinsfile 4.7 KB

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