Jenkinsfile.generate 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. 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()
  32. def container_id = sh (script: "DOCKER_HOST=tcp://${NODE_IP}:2376 DOCKER_TLS_VERIFY=1 docker ps -q -f label=com.docker.swarm.service.name=\${SERVICE_NAME}" , returnStdout: true).trim()
  33. sh "DOCKER_HOST=tcp://${NODE_IP}:2376 DOCKER_TLS_VERIFY=1 docker exec -i ${container_id} /tmp/generate.sh ${domain}"
  34. }
  35. }
  36. }
  37. }
  38. post {
  39. always {
  40. echo "CleaningUp work directory"
  41. deleteDir()
  42. }
  43. success {
  44. mail charset: 'UTF-8',
  45. subject: "Jenkins build SUCCESS",
  46. mimeType: 'text/html',
  47. to: "${mailto}",
  48. 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}"
  49. }
  50. failure {
  51. mail charset: 'UTF-8',
  52. subject: "Jenkins build ERROR",
  53. mimeType: 'text/html',
  54. to: "${mailto}",
  55. 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}"
  56. }
  57. aborted {
  58. mail charset: 'UTF-8',
  59. subject: "Jenkins build ERROR",
  60. mimeType: 'text/html',
  61. to: "${mailto}",
  62. 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}"
  63. }
  64. }
  65. }