· 6 years ago · Mar 21, 2019, 11:18 AM
1#!/usr/bin/env groovy
2import groovy.json.JsonOutput
3import java.text.SimpleDateFormat
4
5https_proxy = 'http://gateway.zscaler.net:80'
6stack_name = "plateformsport"
7service_name = "api"
8registry_url = "registry-eu-local.subsidia.org"
9compose_version = 'v0.12.4'
10cli_version = 'v0.5.0'
11
12def notifySlack(text) {
13 def slackURL = 'https://hooks.slack.com/services/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'
14 def payload = JsonOutput.toJson([
15 text : text,
16 username: "Jenkins",
17 icon_url: "https://wiki.jenkins.io/download/attachments/2916393/headshot.png"
18 ])
19 sh("curl -X POST -x ${https_proxy} --data-urlencode \'payload=${payload}\' ${slackURL}")
20}
21
22pipeline {
23 agent {
24 node {
25 label 'DOCKER'
26 }
27 }
28
29 environment {
30 releaseVersion = ''
31 pullRequestId = "${env.CHANGE_ID}"
32 }
33
34 post {
35 failure {
36 notifySlack("<${env.RUN_DISPLAY_URL}|Job #${env.BUILD_NUMBER} - ${env.JOB_NAME}> failed. <${env.RUN_DISPLAY_URL}|Click> to see details")
37 }
38 success {
39 notifySlack("<${env.RUN_DISPLAY_URL}|Job #${env.BUILD_NUMBER} - ${env.JOB_NAME}> - `${env.JOB_NAME}` completed successfully. Tag: `${releaseVersion}`")
40 }
41 aborted {
42 notifySlack("<${env.RUN_DISPLAY_URL}|Job #${env.BUILD_NUMBER} - ${env.JOB_NAME}> canceled")
43 }
44 }
45
46 stages {
47 stage('Checkout') {
48 steps {
49 checkout scm
50 script {
51 sh('printenv')
52 sh('echo "Installing .jq..."')
53 sh("curl -L -x ${https_proxy} -o jq \"https://github.com/stedolan/jq/releases/download/jq-1.6/jq-linux64\"")
54 sh('chmod +x ./jq')
55 notifySlack("<${env.RUN_DISPLAY_URL}|Job #${env.BUILD_NUMBER} - ${env.JOB_NAME}> started")
56 releaseVersion = sh(returnStdout: true, script: './jq -r ".version" composer.json').trim()
57
58 if (pullRequestId != null && pullRequestId != "null") {
59 releaseVersion += '-PR' + pullRequestId
60 }
61 if (env.BRANCH_NAME.trim() != "master") {
62 releaseVersion += '-' + sh(returnStdout: true, script: 'git rev-parse --short HEAD').trim()
63 }
64
65 echo "Current version : ${releaseVersion}"
66 }
67 }
68 }
69
70 stage('build docker') {
71 steps {
72 script {
73 dockerApiImage = docker.build("hr/${stack_name}-${service_name}:${releaseVersion}", "--build-arg http_proxy=${https_proxy} --build-arg https_proxy=${https_proxy} --build-arg CURRENT_API_TAG=${releaseVersion} --build-arg CURRENT_BUILD_NUMBER=${env.BUILD_NUMBER} .")
74 dockerCronImage = docker.build("hr/${stack_name}-cron:${releaseVersion}", "--build-arg http_proxy=${https_proxy} --build-arg https_proxy=${https_proxy} docker/crontab")
75 }
76 }
77 }
78
79 stage('Sonar') {
80 steps {
81 withSonarQubeEnv('sonar.osiris.withoxylane.com') {
82 withEnv(["JAVA_HOME=${tool 'jdk8'}", "PATH=$PATH:${tool 'Scanner2.8'}/bin"]) {
83 sh("sonar-runner scan -Dsonar.projectVersion=${releaseVersion}")
84 }
85 }
86 }
87 }
88
89 stage('Publish to registry') {
90 when {
91 anyOf {
92 branch 'master'
93 branch 'develop'
94 changeRequest()
95 }
96 }
97 steps {
98 script {
99 docker.withRegistry("https://${registry_url}", 'docked_preprod') {
100 dockerApiImage.push "${releaseVersion}"
101 dockerApiImage.push 'latest'
102 dockerCronImage.push "${releaseVersion}"
103 dockerCronImage.push 'latest'
104 }
105 }
106 }
107 }
108
109 stage('Prepare Rancher deployment') {
110 when {
111 anyOf {
112 branch 'master'
113 branch 'develop'
114 changeRequest()
115 }
116 }
117 steps {
118 sh "wget http://nexus.osiris.withoxylane.com/service/local/repositories/utils/content/rancher/rancher-compose-linux-amd64-${compose_version}.tar.gz -O - | tar -zx"
119 sh "wget http://nexus.osiris.withoxylane.com/service/local/repositories/utils/content/rancher/rancher-linux-amd64-${cli_version}.tar.gz -O - | tar -zx"
120 sh "mv rancher-compose-${compose_version}/rancher-compose . && rm -rf rancher-compose-${compose_version}"
121 sh "mv rancher-${cli_version}/rancher . && rm -rf rancher-${cli_version}"
122 }
123 }
124
125 stage('Deploy') {
126 when {
127 anyOf {
128 branch 'master'
129 branch 'develop'
130 changeRequest()
131 }
132 }
133 steps {
134 script {
135 try {
136 // get the docker-compose and rancher-compose files
137 sh "./rancher --url http://internal-lb-rancherpp-back-1974756408.eu-west-1.elb.amazonaws.com \
138 --access-key 45FCDA866F1AC1C5D1FB \
139 --secret-key cbF25bHyMgUmJurbndxcFYy2ABi9L5SaQMKoKgTF \
140 export ${stack_name}"
141 sh "mv ${stack_name}/*-compose.yml ."
142 } catch (Exception err) {
143 echo "Stack not found, using local compose"
144 }
145
146 // format compose files with the new image tag
147 sh "sed -i \"s/${stack_name}-${service_name}:.*\$/${stack_name}-${service_name}:${releaseVersion}/g\" docker-compose.yml"
148 sh "sed -i \"s/${stack_name}-cron:.*\$/${stack_name}-cron:${releaseVersion}/g\" docker-compose.yml"
149
150 // deploy api
151 sh "./rancher-compose --project-name ${stack_name} \
152 --url http://internal-lb-rancherpp-back-1974756408.eu-west-1.elb.amazonaws.com \
153 --access-key 45FCDA866F1AC1C5D1FB \
154 --secret-key cbF25bHyMgUmJurbndxcFYy2ABi9L5SaQMKoKgTF \
155 --verbose up -d --force-upgrade --pull --confirm-upgrade ${service_name} cron"
156 }
157 }
158 }
159
160 stage('Add Tag to sources & create release') {
161 when {
162 anyOf {
163 branch 'master'
164 }
165 }
166 steps {
167 script {
168 withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'GitHubJenkinsAccessToken', usernameVariable: 'GITHUB_LOGIN', passwordVariable: 'GITHUB_TOKEN']]) {
169 def myApp = "${stack_name}-${service_name}"
170 def latestTag = sh(returnStdout: true, script: "git describe --tags `git rev-list --tags --max-count=1`").trim()
171 sh("git tag v${releaseVersion}")
172 sh("git push https://${GITHUB_LOGIN}:${GITHUB_TOKEN}@github.com/dktunited/${myApp}.git --tags")
173 def GIT_CHANGELOG = sh(returnStdout: true, script: "git log ${latestTag}...v${releaseVersion} --pretty=format:'<li> <a href=\"http://github.com/dktunited/${myApp}/commit/%H\">%h •</a> %s</li> ' --reverse")
174 def dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm")
175 def now = new Date()
176 withCredentials([string(credentialsId: 'DKT_PROXY_URL', variable: 'PROXY_URL')]) {
177 sh("docker run --rm --env HTTP_PROXY=${https_proxy} --env HTTPS_PROXY=${https_proxy} socialengine/github-release github-release release -u dktunited -r ${myApp} -s ${GITHUB_TOKEN} -t v${releaseVersion} -n 'Release v${releaseVersion}' -d 'Version deployed on the preproduction environment on ${dateFormat.format(now)}.<br/><br/>${GIT_CHANGELOG}<br/><br/> To deploy it on the production environment you can use the following version <b>${releaseVersion}</b>. Remember to change the release from <i>pre release</i> to official when you are pushing on production.' -p")
178 }
179 }
180 }
181 }
182 }
183 }
184}