Jenkinsfile 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. pipeline {
  2. agent {
  3. label "swarm"
  4. }
  5. environment {
  6. DOCKER_IMAGE='iru/admin'
  7. SWARM_GIT_URL='ssh://git@git.sdsys.ru:8022/iru/stack-deploy.git'
  8. SWARM_GIT_NAME='stack-deploy'
  9. JENKINS_MAIL='jenkins@sdsys.ru'
  10. PACKAGE_NAME='web-registry-admin'
  11. DOCKER_CERT_PATH='/run/secrets/swarm'
  12. DHOST='tcp://dev-iru-swarm.infoclinica.lan:2376 DOCKER_TLS_VERIFY=1'
  13. DOCKER_REGISTRY='dev-registry.infoclinica.ru:5000'
  14. BASE_REGISTRY='images.sdsys.ru'
  15. NEWTAG=''
  16. }
  17. parameters {
  18. string(
  19. name: "repo",
  20. defaultValue: "prod",
  21. description: "Repository to build and/or deploy from. Values - dev, prod."
  22. )
  23. string(
  24. name: "service_update",
  25. defaultValue: "info_admin",
  26. description: "Service name to update."
  27. )
  28. string(
  29. name: "mailto",
  30. defaultValue: "admin@sdsys.ru",
  31. description: "Email which has to be notified."
  32. )
  33. }
  34. stages {
  35. stage("Build") {
  36. steps {
  37. script {
  38. withCredentials([usernamePassword(credentialsId: 'registry', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) {
  39. echo "Building ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${repo}-${BUILD_NUMBER}."
  40. sh "docker login -u ${USERNAME} -p ${PASSWORD} ${BASE_REGISTRY}"
  41. sh "docker build --build-arg repo=${repo} --no-cache -t ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${repo}-${BUILD_NUMBER} ."
  42. }
  43. 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
  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. }