Jenkinsfile.generate 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. pipeline {
  2. agent {
  3. label "swarm"
  4. }
  5. environment {
  6. CLUSTER_NAME='iru-swarm1.infoclinica.lan'
  7. DOCKER_CERT_PATH='/run/secrets/swarm'
  8. SERVICE_NAME='proxy_letsencrypt'
  9. JENKINS_MAIL='jenkins.dev@sdsys.ru'
  10. SWARM_GIT_NAME='stack-deploy'
  11. SWARM_GIT_URL='ssh://git@git.sdsys.ru:8022/iru/stack-deploy.git'
  12. }
  13. parameters {
  14. string(
  15. name: "domain",
  16. defaultValue: "",
  17. description: "Domain names"
  18. )
  19. string(
  20. name: "mailto",
  21. defaultValue: "admin@sdsys.ru",
  22. description: "Email which has to be notified."
  23. )
  24. }
  25. stages {
  26. stage("Generate Cert and Key") {
  27. steps {
  28. script {
  29. def NODE = sh (script: "DOCKER_HOST=tcp://${CLUSTER_NAME}:2376 DOCKER_TLS_VERIFY=1 docker service ps \${SERVICE_NAME} --format '{{.Node}}' --filter desired-state=Running" , returnStdout: true).trim()
  30. sh "if [ -z ${NODE} ]; then echo '${SERVICE_NAME} does not running'; exit 1; fi"
  31. NODE_NAME_LOCAL = NODE.replace(".ru", ".lan")
  32. // def NODE_IP = sh (script: "DOCKER_HOST=tcp://${CLUSTER_NAME}:2376 DOCKER_TLS_VERIFY=1 docker node inspect ${NODE} -f '{{.Status.Addr}}'" , returnStdout: true).trim()
  33. def container_id = sh (script: "DOCKER_HOST=tcp://${NODE_NAME_LOCAL}:2376 DOCKER_TLS_VERIFY=1 docker ps -q -f label=com.docker.swarm.service.name=\${SERVICE_NAME}" , returnStdout: true).trim()
  34. sh "DOCKER_HOST=tcp://${NODE_NAME_LOCAL}:2376 DOCKER_TLS_VERIFY=1 docker exec -i ${container_id} /tmp/generate.sh ${domain}"
  35. }
  36. }
  37. }
  38. }
  39. post {
  40. always {
  41. echo "CleaningUp work directory"
  42. deleteDir()
  43. }
  44. success {
  45. mail charset: 'UTF-8',
  46. subject: "Jenkins build SUCCESS",
  47. mimeType: 'text/html',
  48. to: "${mailto}",
  49. body: "<b>ATTENTION!!!</b> <b><br> Jenkins job successed.\n\n <b><br>Project Name:</b> ${env.JOB_NAME} <b><br>Generate certs and key for ${domain}</b> <b><br>\nBuild Number:</b> ${env.BUILD_NUMBER} <b><br>\nURL Build:</b> ${RUN_DISPLAY_URL}"
  50. }
  51. failure {
  52. mail charset: 'UTF-8',
  53. subject: "Jenkins build ERROR",
  54. mimeType: 'text/html',
  55. to: "${mailto}",
  56. 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}"
  57. }
  58. aborted {
  59. mail charset: 'UTF-8',
  60. subject: "Jenkins build ERROR",
  61. mimeType: 'text/html',
  62. to: "${mailto}",
  63. body: "<b>ATTENTION!!!</b> <b><br> Jenkins job aborted.\n\n <b><br> The CNAME ${key_name} is already exists!\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}"
  64. }
  65. }
  66. }