pipeline { options { buildDiscarder logRotator(numToKeepStr: '10') disableConcurrentBuilds() timeout(time: 30, unit: 'MINUTES') } agent { label "swarm" } environment { CLUSTER_NAME='iru-swarm.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: "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" 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_IP}: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_IP}:2376 DOCKER_TLS_VERIFY=1 docker exec -i ${container_id} /tmp/renewal.sh" } } } } 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}
Renewal certs and keys

\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}" } } }