pipeline { agent { label "swarm" } options { ansiColor('xterm') } /*AnsiColor “\u001B[31m” = RED “\u001B[30m” = BLACK “\u001B[32m” = GREEN “\u001B[33m” = YELLOW “\u001B[34m” = BLUE “\u001B[35m” = PURPLE “\u001B[36m” = CYAN “\u001B[37m” = WHITE “\u001B[0m” is for RESET */ /*Unicode icons “\u2776” = ❶ “\u27A1” = ➡ “\u2756” = ❖ “\u273F” = ✿ “\u2795” = ➕ “\u2713” = ✓ “\u2705” = ✅ “\u274E” = ❎ “\u2717” = ✗ “\u274C” = ❌ “\u2600” = ☀ “\u2601” = ☁ “\u2622” = ☢ “\u2623” = ☣ “\u2639” = ☹ “\u263A” = ☺ */ environment { CLUSTER_NAME='dev-iru-swarm.infoclinica.lan' DOCKER_REGISTRY='dev-registry.infoclinica.ru:5000' DOCKER_IMAGE='bind' SERVICE_NAME='bind' SWARM_GIT_URL='ssh://git@git.sdsys.ru:8022/iru/stack-deploy.git' SWARM_GIT_NAME='stack-deploy' OVPN_GIT_URL='ssh://git@git.sdsys.ru:8022/iru/openvpn.git' OVPN_GIT_NAME='openvpn' BIND_GIT_URL='ssh://git@git.sdsys.ru:8022/sdsys/bind.git' JENKINS_MAIL='jenkins.dev@sdsys.ru' SMTP_SERVER='mail.sdsys.ru' MAIL_TO='admins@sdsys.ru' } stages { stage ("PULL REPOs") { steps { echo "\u001B[32m \u2600 \u001B[32m Pull repo ${SWARM_GIT_NAME} \u001B[0m" withCredentials([sshUserPrivateKey(credentialsId: 'provision', keyFileVariable: 'GIT_SSH_KEY', passphraseVariable: '', usernameVariable: 'GIT_SSH_USERNAME')]) { sh '''GIT_SSH_COMMAND='ssh -i ${GIT_SSH_KEY} -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' \ git clone ${SWARM_GIT_URL}''' } echo "\u001B[32m \u2600 Pull repo ${OVPN_GIT_NAME} \u001B[0m" withCredentials([sshUserPrivateKey(credentialsId: 'provision', keyFileVariable: 'GIT_SSH_KEY', passphraseVariable: '', usernameVariable: 'GIT_SSH_USERNAME')]) { sh '''GIT_SSH_COMMAND='ssh -i ${GIT_SSH_KEY} -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' \ git clone ${OVPN_GIT_URL}''' } } } stage("Generate zone-files") { steps { echo "\u001B[32m \u2600 Generate ptr-zones \u001B[0m" sh '''set +x serial=$(cat ${WORKSPACE}/${SWARM_GIT_NAME}/tags/bind.version) serial=$((serial+1)) ${WORKSPACE}/generate_ptr.sh 201 ${serial} ${WORKSPACE}/generate_ptr.sh 204 ${serial} ${WORKSPACE}/generate_ptr.sh 205 ${serial} ''' echo "\u001B[32m \u2600 Generate infoclinica-zone \u001B[0m" sh '''set +x serial=$(cat ${WORKSPACE}/${SWARM_GIT_NAME}/tags/bind.version) serial=$((serial+1)) ${WORKSPACE}/generate_zone.sh ${serial} ''' } } stage("Create BIND Docker Image") { steps { echo "\u001B[32m \u2600 Create image \u001B[35m ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${BUILD_NUMBER} \u001B[0m" sh '''docker build --no-cache -t ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${BUILD_NUMBER} . if [ $? != 0 ]; then echo "\u274C \u001B[31m The container was not built \u001B[0m"; exit 1; fi ''' } } stage("Staging") { steps { echo "\u001B[32m \u2600 Testing Bind \u001B[0m" sh '''set +x container_id_server=`docker run -d --rm ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${BUILD_NUMBER}` docker exec -t ${container_id_server} nslookup iru-swarm.infoclinica.lan if [ $? != 0 ]; then exit 1; else echo "\u001B[32m \u2600 DNS is working!!! \u001B[0m"; fi docker stop ${container_id_server} ''' } } stage("Publishing") { steps { echo "\u001B[32m \u2600 Publishing \u001B[35m ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${BUILD_NUMBER}. \u001B[0m" sh "docker push ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${BUILD_NUMBER}" } } stage("Prod-like") { steps { echo "\u001B[32m \u2600 Check Prod-like cluster status \u001B[0m" sh '''set +x ping -c 2 ${CLUSTER_NAME} if [ $? -eq 0 ]; then export DOCKER_CERT_PATH=/run/secrets/swarm export DOCKER_HOST=tcp://${CLUSTER_NAME}:2376 DOCKER_TLS_VERIFY=1 docker node ls --format "{{.Hostname}} {{.TLSStatus}}" | while read host status do if [ $status != Ready ]; then echo "\u274C \u001B[31m Cluster ${CLUSTER_NAME} state is inconsistent \u001B[0m"; exit 1 else echo "\u2705 \u001B[34m HOST: $host STATUS: $status \u001B[0m" fi done else echo "\u274C \u001B[31m Host not Found \u001B[0m"; exit 1 fi ''' echo "\u001B[32m \u2600 Run containers in Prod-like \u001B[0m" sh '''export DOCKER_CERT_PATH=/run/secrets/swarm export DOCKER_HOST=tcp://${CLUSTER_NAME}:2376 DOCKER_TLS_VERIFY=1 export DOCKER_HOST=tcp://$(docker info -f '{{.Name}}'):2376 DOCKER_TLS_VERIFY=1 if [ -z $(docker service ps -q ${DOCKER_IMAGE}) ];then docker service create --replicas 1 \ --name ${SERVICE_NAME} -p 53:53/tcp -p 53:53/udp \ ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${BUILD_NUMBER} else docker service update ${SERVICE_NAME} \ --image ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${BUILD_NUMBER} if [ $? != 0 ]; then docker service rollback ${SERVICE_NAME}; fi fi rm -rf ${WORKSPACE}/${SWARM_GIT_NAME} ''' } } stage("Tagging") { steps { echo "\u001B[32m \u2600 Tagging \u001B[35m ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${BUILD_NUMBER} \u001B[32m to \u001B[35m ${DOCKER_REGISTRY}/iru/${DOCKER_IMAGE}:latest \u001B[0m" sh '''docker tag ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${BUILD_NUMBER} \ ${DOCKER_REGISTRY}/iru/${DOCKER_IMAGE}:latest docker push ${DOCKER_REGISTRY}/iru/${DOCKER_IMAGE}:latest ''' echo "\u001B[32m \u2600 Updating tag info in ${SWARM_GIT_NAME} repository \u001B[0m" withCredentials([sshUserPrivateKey(credentialsId: 'provision', keyFileVariable: 'GIT_SSH_KEY', passphraseVariable: '', usernameVariable: 'GIT_SSH_USERNAME')]) { sh '''GIT_SSH_COMMAND='ssh -i ${GIT_SSH_KEY} -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' \ git clone ${SWARM_GIT_URL} cd ${SWARM_GIT_NAME} echo -n ${BUILD_NUMBER} > tags/${DOCKER_IMAGE}.version git add -A git config --global user.email "${JENKINS_MAIL}" git config --global user.name "Jenkins" git commit -m 'Version update' GIT_SSH_COMMAND='ssh -i ${GIT_SSH_KEY} -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' \ git push origin master ''' } } } } post { always { echo "\u001B[32m \u2600 CleaningUp work directory \u001B[0m" deleteDir() } failure { mail charset: 'UTF-8', subject: "Jenkins build ERROR", mimeType: 'text/html', to: "${MAIL_TO}", body: "ATTENTION!!!
Jenkins job failed.\n\n
Project Name:
${env.JOB_NAME}
\nBuild Number:
${env.BUILD_NUMBER}
\nURL Build:
${RUN_DISPLAY_URL}" } } }