· 6 years ago · Mar 21, 2019, 11:14 AM
1Skip to content
2
3Search or jump to…
4
5Pull requests
6Issues
7Marketplace
8Explore
9
10@Duffy59 Sign out
11It looks like you are opted in to receive dktunited email notifications, but you have an ineligible email address configured in your notification routing settings. In order to receive email notifications for this organization, you'll have to update your organization routing settings for dktunited to use an email from one of the following domains:
12
13decathlon.com
141
150 0 dktunited/plateformsport-api Private
16 Code Issues 4 Pull requests 2 Projects 0 Insights
17plateformsport-api/Jenkinsfile
18@SkYNewZ SkYNewZ Update Jenkinsfile for Github CI
19c4bdbae on 25 Jan
20@mcaboche
21@SkYNewZ
22185 lines (168 sloc) 8.32 KB
23
24#!/usr/bin/env groovy
25import groovy.json.JsonOutput
26import java.text.SimpleDateFormat
27
28https_proxy = 'http://gateway.zscaler.net:80'
29stack_name = "plateformsport"
30service_name = "api"
31registry_url = "registry-eu-local.subsidia.org"
32compose_version = 'v0.12.4'
33cli_version = 'v0.5.0'
34
35def notifySlack(text) {
36 def slackURL = 'https://hooks.slack.com/services/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'
37 def payload = JsonOutput.toJson([
38 text : text,
39 username: "Jenkins",
40 icon_url: "https://wiki.jenkins.io/download/attachments/2916393/headshot.png"
41 ])
42 sh("curl -X POST -x ${https_proxy} --data-urlencode \'payload=${payload}\' ${slackURL}")
43}
44
45pipeline {
46 agent {
47 node {
48 label 'DOCKER'
49 }
50 }
51
52 environment {
53 releaseVersion = ''
54 pullRequestId = "${env.CHANGE_ID}"
55 }
56
57 post {
58 failure {
59 notifySlack("<${env.RUN_DISPLAY_URL}|Job #${env.BUILD_NUMBER} - ${env.JOB_NAME}> failed. <${env.RUN_DISPLAY_URL}|Click> to see details")
60 }
61 success {
62 notifySlack("<${env.RUN_DISPLAY_URL}|Job #${env.BUILD_NUMBER} - ${env.JOB_NAME}> - `${env.JOB_NAME}` completed successfully. Tag: `${releaseVersion}`")
63 }
64 aborted {
65 notifySlack("<${env.RUN_DISPLAY_URL}|Job #${env.BUILD_NUMBER} - ${env.JOB_NAME}> canceled")
66 }
67 }
68
69 stages {
70 stage('Checkout') {
71 steps {
72 checkout scm
73 script {
74 sh('printenv')
75 sh('echo "Installing .jq..."')
76 sh("curl -L -x ${https_proxy} -o jq \"https://github.com/stedolan/jq/releases/download/jq-1.6/jq-linux64\"")
77 sh('chmod +x ./jq')
78 notifySlack("<${env.RUN_DISPLAY_URL}|Job #${env.BUILD_NUMBER} - ${env.JOB_NAME}> started")
79 releaseVersion = sh(returnStdout: true, script: './jq -r ".version" composer.json').trim()
80
81 if (pullRequestId != null && pullRequestId != "null") {
82 releaseVersion += '-PR' + pullRequestId
83 }
84 if (env.BRANCH_NAME.trim() != "master") {
85 releaseVersion += '-' + sh(returnStdout: true, script: 'git rev-parse --short HEAD').trim()
86 }
87
88 echo "Current version : ${releaseVersion}"
89 }
90 }
91 }
92
93 stage('build docker') {
94 steps {
95 script {
96 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} .")
97 dockerCronImage = docker.build("hr/${stack_name}-cron:${releaseVersion}", "--build-arg http_proxy=${https_proxy} --build-arg https_proxy=${https_proxy} docker/crontab")
98 }
99 }
100 }
101
102 stage('Sonar') {
103 steps {
104 withSonarQubeEnv('sonar.osiris.withoxylane.com') {
105 withEnv(["JAVA_HOME=${tool 'jdk8'}", "PATH=$PATH:${tool 'Scanner2.8'}/bin"]) {
106 sh("sonar-runner scan -Dsonar.projectVersion=${releaseVersion}")
107 }
108 }
109 }
110 }
111
112 stage('Publish to registry') {
113 when {
114 anyOf {
115 branch 'master'
116 branch 'develop'
117 changeRequest()
118 }
119 }
120 steps {
121 script {
122 docker.withRegistry("https://${registry_url}", 'docked_preprod') {
123 dockerApiImage.push "${releaseVersion}"
124 dockerApiImage.push 'latest'
125 dockerCronImage.push "${releaseVersion}"
126 dockerCronImage.push 'latest'
127 }
128 }
129 }
130 }
131
132 stage('Prepare Rancher deployment') {
133 when {
134 anyOf {
135 branch 'master'
136 branch 'develop'
137 changeRequest()
138 }
139 }
140 steps {
141 sh "wget http://nexus.osiris.withoxylane.com/service/local/repositories/utils/content/rancher/rancher-compose-linux-amd64-${compose_version}.tar.gz -O - | tar -zx"
142 sh "wget http://nexus.osiris.withoxylane.com/service/local/repositories/utils/content/rancher/rancher-linux-amd64-${cli_version}.tar.gz -O - | tar -zx"
143 sh "mv rancher-compose-${compose_version}/rancher-compose . && rm -rf rancher-compose-${compose_version}"
144 sh "mv rancher-${cli_version}/rancher . && rm -rf rancher-${cli_version}"
145 }
146 }
147
148 stage('Deploy') {
149 when {
150 anyOf {
151 branch 'master'
152 branch 'develop'
153 changeRequest()
154 }
155 }
156 steps {
157 script {
158 try {
159 // get the docker-compose and rancher-compose files
160 sh "./rancher --url http://internal-lb-rancherpp-back-1974756408.eu-west-1.elb.amazonaws.com \
161 --access-key 45FCDA866F1AC1C5D1FB \
162 --secret-key cbF25bHyMgUmJurbndxcFYy2ABi9L5SaQMKoKgTF \
163 export ${stack_name}"
164 sh "mv ${stack_name}/*-compose.yml ."
165 } catch (Exception err) {
166 echo "Stack not found, using local compose"
167 }
168
169 // format compose files with the new image tag
170 sh "sed -i \"s/${stack_name}-${service_name}:.*\$/${stack_name}-${service_name}:${releaseVersion}/g\" docker-compose.yml"
171 sh "sed -i \"s/${stack_name}-cron:.*\$/${stack_name}-cron:${releaseVersion}/g\" docker-compose.yml"
172
173 // deploy api
174 sh "./rancher-compose --project-name ${stack_name} \
175 --url http://internal-lb-rancherpp-back-1974756408.eu-west-1.elb.amazonaws.com \
176 --access-key 45FCDA866F1AC1C5D1FB \
177 --secret-key cbF25bHyMgUmJurbndxcFYy2ABi9L5SaQMKoKgTF \
178 --verbose up -d --force-upgrade --pull --confirm-upgrade ${service_name} cron"
179 }
180 }
181 }
182
183 stage('Add Tag to sources & create release') {
184 when {
185 anyOf {
186 branch 'master'
187 }
188 }
189 steps {
190 script {
191 withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'GitHubJenkinsAccessToken', usernameVariable: 'GITHUB_LOGIN', passwordVariable: 'GITHUB_TOKEN']]) {
192 def myApp = "${stack_name}-${service_name}"
193 def latestTag = sh(returnStdout: true, script: "git describe --tags `git rev-list --tags --max-count=1`").trim()
194 sh("git tag v${releaseVersion}")
195 sh("git push https://${GITHUB_LOGIN}:${GITHUB_TOKEN}@github.com/dktunited/${myApp}.git --tags")
196 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")
197 def dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm")
198 def now = new Date()
199 withCredentials([string(credentialsId: 'DKT_PROXY_URL', variable: 'PROXY_URL')]) {
200 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")
201 }
202 }
203 }
204 }
205 }
206 }
207}
208© 2019 GitHub, Inc.
209Terms
210Privacy
211Security
212Status
213Help
214Contact GitHub
215Pricing
216API
217Training
218Blog
219About