Jenkinsfile 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. def SERIAL
  2. def ENAMES = [ 'prod', 'dev' ]
  3. //def ENAMES = [ 'dev' ]
  4. def CLUSTERS = ['prod': 'iru-swarm.infoclinica.lan', 'dev': 'dev-iru-swarm.infoclinica.lan']
  5. def REGISTRIES = ['prod': 'registry.infoclinica.ru:5000', 'dev': 'dev-registry.infoclinica.ru:5000']
  6. pipeline {
  7. agent {
  8. label "swarm"
  9. }
  10. environment {
  11. NGINX_GOST_GIT_URL='https://git.sdsys.ru/iru/nginx-gost.git'
  12. DOCKER_IMAGE='letsencrypt'
  13. SERVICE_NAME='proxy_letsencrypt'
  14. DOCKER_CERT_PATH='/run/secrets/swarm'
  15. JENKINS_MAIL='jenkins.dev@sdsys.ru'
  16. SWARM_GIT_NAME='stack-deploy'
  17. SWARM_GIT_URL='ssh://git@git.sdsys.ru:8022/iru/stack-deploy.git'
  18. }
  19. parameters {
  20. string(
  21. name: "branch",
  22. defaultValue: "master",
  23. description: "Which branch to use."
  24. )
  25. string(
  26. name: "mailto",
  27. defaultValue: "admin@sdsys.ru",
  28. description: "Email which has to be notified."
  29. )
  30. }
  31. stages {
  32. stage ("Discover SERIAL") {
  33. steps {
  34. script {
  35. SERIAL = sh script: "echo -n `date +%y%m%d``printf %03d $BUILD_NUMBER`", returnStdout: true
  36. }
  37. }
  38. }
  39. stage ("Build Image") {
  40. steps {
  41. echo "\u001B[32m \u2600 Building \u001B[35m ${DOCKER_IMAGE}:${SERIAL}. \u001B[0m"
  42. sh "docker build --no-cache -t ${DOCKER_IMAGE}:${SERIAL} ."
  43. }
  44. }
  45. stage ("Push to registry") {
  46. steps {
  47. script {
  48. ENAMES.each { item ->
  49. echo "Pushing to: ${item}, REGISTRIES ${REGISTRIES.get((item))}"
  50. sh """docker tag ${DOCKER_IMAGE}:${SERIAL} ${REGISTRIES.get((item))}/${DOCKER_IMAGE}:${SERIAL}
  51. docker push ${REGISTRIES.get((item))}/${DOCKER_IMAGE}:${SERIAL}
  52. """
  53. }
  54. }
  55. }
  56. }
  57. stage ("Deploy") {
  58. steps {
  59. script {
  60. ENAMES.each { item ->
  61. echo "Deploy to: ${item}, CLUSTERS ${CLUSTERS.get((item))}"
  62. try{
  63. sh "DOCKER_HOST=tcp://${CLUSTERS.get((item))}:2376 DOCKER_TLS_VERIFY=1 docker service update ${SERVICE_NAME} --image ${REGISTRIES.get((item))}/${DOCKER_IMAGE}:${SERIAL}"
  64. }
  65. catch(err){
  66. echo "Recovering service $item"
  67. sh "DOCKER_HOST=tcp://${CLUSTERS.get((item))}:2376 DOCKER_TLS_VERIFY=1 docker service rollback ${SERVICE_NAME}"
  68. throw err
  69. }
  70. }
  71. }
  72. }
  73. }
  74. stage("Tagging") {
  75. steps {
  76. echo "Updating tag info in ${SWARM_GIT_NAME} repository"
  77. withCredentials([sshUserPrivateKey(credentialsId: 'provision', keyFileVariable: 'GIT_SSH_KEY', passphraseVariable: '', usernameVariable: 'GIT_SSH_USERNAME')]) {
  78. sh """GIT_SSH_COMMAND='ssh -i $GIT_SSH_KEY -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' \
  79. git clone \${SWARM_GIT_URL} && cd \${SWARM_GIT_NAME}
  80. if [ \$(git branch --list -a | grep -q ${branch}; echo \$?) == 0 ];then echo "${branch} is already exist";git checkout ${branch}; \
  81. else echo "${branch} does not exist!!!"; git checkout -b \${branch};fi
  82. echo -n ${SERIAL} > tags/${DOCKER_IMAGE}.version
  83. git add -A
  84. git config --global user.email "${JENKINS_MAIL}"
  85. git config --global user.name "Jenkins"
  86. git commit -m 'Version update'
  87. GIT_SSH_COMMAND='ssh -i ${GIT_SSH_KEY} -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' \
  88. git push origin ${branch}
  89. """
  90. }
  91. }
  92. }
  93. }
  94. post {
  95. always {
  96. echo "CleaningUp work directory"
  97. deleteDir()
  98. sh "docker image rm -f `docker image ls -q ${DOCKER_IMAGE}:${SERIAL}`"
  99. }
  100. failure {
  101. mail charset: 'UTF-8',
  102. subject: "Jenkins build ERROR",
  103. mimeType: 'text/html',
  104. to: "${mailto}",
  105. 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}"
  106. }
  107. success {
  108. mail charset: 'UTF-8',
  109. subject: "Jenkins build SUSCCESS",
  110. mimeType: 'text/html',
  111. to: "${mailto}",
  112. 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}"
  113. }
  114. }
  115. }