Jenkinsfile 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. NEWTAG=''
  15. }
  16. parameters {
  17. string(
  18. name: "repo",
  19. defaultValue: "prod",
  20. description: "Repository to build and/or deploy from. Values - dev, prod."
  21. )
  22. string(
  23. name: "service_update",
  24. defaultValue: "info_admin",
  25. description: "Service name to update."
  26. )
  27. string(
  28. name: "mailto",
  29. defaultValue: "admin@sdsys.ru",
  30. description: "Email which has to be notified."
  31. )
  32. }
  33. stages {
  34. stage("Build") {
  35. steps {
  36. echo "Building ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${repo}-${BUILD_NUMBER}."
  37. sh "docker build --build-arg repo=${repo} --no-cache -t ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${repo}-${BUILD_NUMBER} ."
  38. script {
  39. 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
  40. NEWTAG = repo + "-" + NEWTAG + "_" + BUILD_NUMBER
  41. echo "NEWTAG is $NEWTAG"
  42. }
  43. sh "docker tag ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${repo}-${BUILD_NUMBER} ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${NEWTAG}"
  44. }
  45. }
  46. stage("Publish") {
  47. steps {
  48. echo "Publishing ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${NEWTAG}"
  49. sh "docker push ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${NEWTAG}"
  50. }
  51. }
  52. stage("Update") {
  53. when { expression { service_update != "" } }
  54. steps {
  55. script {
  56. for (String item : service_update.split()) {
  57. echo "Updating ${item}"
  58. try{
  59. sh "DOCKER_HOST=${DHOST} docker service update ${item} --image ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${NEWTAG}"
  60. }
  61. catch(err){
  62. echo "Recovering service $item"
  63. sh "DOCKER_HOST=${DHOST} docker service rollback ${item}"
  64. throw err
  65. }
  66. }
  67. }
  68. }
  69. }
  70. stage("Tagging"){
  71. steps{
  72. echo "Setting latest tag"
  73. sh """ docker tag ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${NEWTAG} ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:latest
  74. docker push ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:latest """
  75. echo "Updating tag info in ${SWARM_GIT_NAME} repository"
  76. withCredentials([sshUserPrivateKey(credentialsId: 'provision', keyFileVariable: 'GIT_SSH_KEY', passphraseVariable: '', usernameVariable: 'GIT_SSH_USERNAME')]) {
  77. sh """GIT_SSH_COMMAND='ssh -i ${GIT_SSH_KEY} -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' \
  78. git clone ${SWARM_GIT_URL}
  79. cd ${SWARM_GIT_NAME}
  80. echo -n ${NEWTAG} > tags/${DOCKER_IMAGE}.dev-version
  81. git add -A
  82. git config --global user.email "${JENKINS_MAIL}"
  83. git config --global user.name "Jenkins"
  84. git commit -m 'Version update'
  85. GIT_SSH_COMMAND='ssh -i ${GIT_SSH_KEY} -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' \
  86. git push origin master
  87. ls"""
  88. }
  89. }
  90. }
  91. }
  92. post {
  93. always {
  94. echo "CleaningUp work diretory"
  95. deleteDir()
  96. sh "docker image rm -f `docker image ls -q ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${repo}-${BUILD_NUMBER}`"
  97. }
  98. failure {
  99. mail charset: 'UTF-8',
  100. subject: "Jenkins build ERROR",
  101. mimeType: 'text/html',
  102. to: "${mailto}",
  103. 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}"
  104. }
  105. success {
  106. mail charset: 'UTF-8',
  107. subject: "Jenkins build SUSCCESS",
  108. mimeType: 'text/html',
  109. to: "${mailto}",
  110. 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}"
  111. }
  112. }
  113. }