pipeline { agent { label "swarm" } environment { CLUSTER_NAME='iru-swarm1.infoclinica.lan' DOCKER_CERT_PATH='/run/secrets/swarm' SERVICE_NAME='proxy_letsencrypt' JENKINS_MAIL='jenkins.dev@sdsys.ru' SWARM_GIT_NAME='stack-deploy' SWARM_GIT_URL='ssh://git@git.sdsys.ru:8022/iru/stack-deploy.git' } parameters { string( name: "domain", defaultValue: "", description: "Domain names" ) string( name: "mailto", defaultValue: "admin@sdsys.ru", description: "Email which has to be notified." ) } stages { stage("Generate Cert and Key") { steps { script { 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() sh "if [ -z ${NODE} ]; then echo '${SERVICE_NAME} does not running'; exit 1; fi" NODE_NAME_LOCAL = NODE.replace(".ru", ".lan") // 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() 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() sh "DOCKER_HOST=tcp://${NODE_NAME_LOCAL}:2376 DOCKER_TLS_VERIFY=1 docker exec -i ${container_id} /tmp/generate.sh ${domain}" } } } } post { always { echo "CleaningUp work directory" deleteDir() } success { mail charset: 'UTF-8', subject: "Jenkins build SUCCESS", mimeType: 'text/html', to: "${mailto}", body: "ATTENTION!!!
Jenkins job successed.\n\n
Project Name:
${env.JOB_NAME}
Generate certs and key for ${domain}

\nBuild Number:
${env.BUILD_NUMBER}
\nURL Build:
${RUN_DISPLAY_URL}" } failure { mail charset: 'UTF-8', subject: "Jenkins build ERROR", mimeType: 'text/html', to: "${mailto}", body: "ATTENTION!!!
Jenkins job failed.\n\n
Project Name:
${env.JOB_NAME}
\nBuild Number:
${env.BUILD_NUMBER}
\nURL Build:
${RUN_DISPLAY_URL}" } aborted { mail charset: 'UTF-8', subject: "Jenkins build ERROR", mimeType: 'text/html', to: "${mailto}", body: "ATTENTION!!!
Jenkins job aborted.\n\n
The CNAME ${key_name} is already exists!\n\n
Project Name:
${env.JOB_NAME}
\nBuild Number:
${env.BUILD_NUMBER}
\nURL Build:
${RUN_DISPLAY_URL}" } } }