-
-
Notifications
You must be signed in to change notification settings - Fork 138
/
buildDockerAndPublishImage.groovy
262 lines (243 loc) · 13.3 KB
/
buildDockerAndPublishImage.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
import io.jenkins.infra.InfraConfig
import java.text.SimpleDateFormat
import java.util.Date
import java.text.DateFormat
def call(String imageName, Map userConfig=[:]) {
def defaultConfig = [
agentLabels: 'docker || linux-amd64-docker', // String expression for the labels the agent must match
automaticSemanticVersioning: false, // Do not automagically increase semantic version by default
includeImageNameInTag: false, // Set to true for multiple semversioned images built in parallel, will include the image name in tag to avoid conflict
dockerfile: 'Dockerfile', // Obvious default
platform: 'linux/amd64', // Intel/AMD 64 Bits, following Docker platform identifiers
nextVersionCommand: 'jx-release-version', // Commmand line used to retrieve the next version
gitCredentials: 'github-app-infra', // Credential ID for tagging and creating release
imageDir: '.', // Relative path to the context directory for the Docker build
]
// Merging the 2 maps - https://blog.mrhaki.com/2010/04/groovy-goodness-adding-maps-to-map_21.html
final Map finalConfig = defaultConfig << userConfig
// Retrieve Library's Static File Resources
final String makefileContent = libraryResource 'io/jenkins/infra/docker/Makefile'
final boolean semVerEnabledOnPrimaryBranch = finalConfig.automaticSemanticVersioning && env.BRANCH_IS_PRIMARY
// Only run 1 build at a time on primary branch to ensure builds won't use the same tag when semantic versionning is activated
if (env.BRANCH_IS_PRIMARY) {
properties([disableConcurrentBuilds()])
}
final Date now = new Date()
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX")
final String buildDate = dateFormat.format(now)
// Warn about potential Linux/Windows contradictions between platform & agentLabels, and set the Windows config suffix for CST files
String cstConfigSuffix = ''
if (finalConfig.agentLabels.contains('windows') || finalConfig.platform.contains('windows')) {
if (finalConfig.agentLabels.contains('windows') && !finalConfig.platform.contains('windows')) {
echo "WARNING: A 'windows' agent is requested, but the 'platform' is set to '${finalConfig.platform}'."
}
if (!finalConfig.agentLabels.contains('windows') && finalConfig.platform.contains('windows')) {
echo "WARNING: The 'platform' is set to '${finalConfig.platform}', but there isn't any 'windows' agent requested."
}
cstConfigSuffix = '-windows'
}
String operatingSystem = finalConfig.platform.split('/')[0]
node(finalConfig.agentLabels) {
withEnv([
"BUILD_DATE=${buildDate}",
"IMAGE_NAME=${imageName}",
"IMAGE_DIR=${finalConfig.imageDir}",
"IMAGE_DOCKERFILE=${finalConfig.dockerfile}",
"IMAGE_PLATFORM=${finalConfig.platform}",
]) {
infra.withDockerPullCredentials{
String nextVersion = ''
stage("Prepare ${imageName}") {
checkout scm
// The makefile to use must come from the pipeline to avoid a nasty user trying to exfiltrate data from the build
// Even though we have mitigation through the multibranch job config allowing to build PRs only from the repository contributors
writeFile file: 'Makefile', text: makefileContent
} // stage
// Automatic tagging on principal branch is not enabled by default, show potential next version in PR anyway
if (finalConfig.automaticSemanticVersioning) {
stage("Get Next Version of ${imageName}") {
String imageInTag = '-' + imageName.replace('-','').replace(':','').toLowerCase()
if (isUnix()) {
sh 'git fetch --all --tags' // Ensure that all the tags are retrieved (uncoupling from job configuration, wether tags are fetched or not)
if (!finalConfig.includeImageNameInTag) {
nextVersion = sh(script: finalConfig.nextVersionCommand, returnStdout: true).trim()
} else {
echo "Including the image name '${imageName}' in the next version"
// Retrieving the semver part from the last tag including the image name
String currentTagScript = 'git tag --list \"*' + imageInTag + '\" --sort=-v:refname | head -1'
String currentSemVerVersion = sh(script: currentTagScript, returnStdout: true).trim()
echo "Current semver version is '${currentSemVerVersion}'"
// Set a default value if there isn't any tag for the current image yet (https://groovy-lang.org/operators.html#_elvis_operator)
currentSemVerVersion = currentSemVerVersion ?: '0.0.0-' + imageInTag
String nextVersionScript = finalConfig.nextVersionCommand + ' -debug --previous-version=' + currentSemVerVersion
String nextVersionSemVerPart = sh(script: nextVersionScript, returnStdout: true).trim()
echo "Next semver version part is '${nextVersionSemVerPart}'"
nextVersion = nextVersionSemVerPart + imageInTag
}
} else {
powershell 'git fetch --all --tags' // Ensure that all the tags are retrieved (uncoupling from job configuration, wether tags are fetched or not)
if (!finalConfig.includeImageNameInTag) {
nextVersion = powershell(script: finalConfig.nextVersionCommand, returnStdout: true).trim()
} else {
echo "Including the image name '${imageName}' in the next version"
// Retrieving the semver part from the last tag including the image name
String currentTagScript = 'git tag --list \"*' + imageInTag + '\" --sort=-v:refname | head -1'
String currentSemVerVersion = powershell(script: currentTagScript, returnStdout: true).trim()
echo "Current semver version is '${currentSemVerVersion}'"
// Set a default value if there isn't any tag for the current image yet (https://groovy-lang.org/operators.html#_elvis_operator)
currentSemVerVersion = currentSemVerVersion ?: '0.0.0-' + imageInTag
String nextVersionScript = finalConfig.nextVersionCommand + ' -debug --previous-version=' + currentSemVerVersion
String nextVersionSemVerPart = powershell(script: nextVersionScript, returnStdout: true).trim()
echo "Next semver version part is '${nextVersionSemVerPart}'"
nextVersion = nextVersionSemVerPart + imageInTag
}
}
echo "Next Release Version = ${nextVersion}"
} // stage
} // if
stage("Lint ${imageName}") {
// Define the image name as prefix to support multi images per pipeline
String hadolintReportId = "${imageName.replaceAll(':','-')}-hadolint-${now.getTime()}"
String hadoLintReportFile = "${hadolintReportId}.json"
withEnv(["HADOLINT_REPORT=${env.WORKSPACE}/${hadoLintReportFile}"]) {
try {
if (isUnix()) {
sh 'make lint'
} else {
powershell 'make lint'
}
} finally {
recordIssues(
enabledForFailure: true,
aggregatingResults: false,
tool: hadoLint(id: hadolintReportId, pattern: hadoLintReportFile)
)
}
}
} // stage
stage("Build ${imageName}") {
if (isUnix()) {
sh 'make build'
} else {
powershell 'make build'
}
} //stage
// There can be 2 kind of tests: per image and per repository
// Assuming Windows versions of cst configuration files finishing by "-windows" (e.g. "common-cst-windows.yml")
[
'Image Test Harness': "${finalConfig.imageDir}/cst${cstConfigSuffix}.yml",
'Common Test Harness': "${env.WORKSPACE}/common-cst${cstConfigSuffix}.yml"
].each { testName, testHarness ->
if (fileExists(testHarness)) {
stage("Test ${testName} for ${imageName}") {
withEnv(["TEST_HARNESS=${testHarness}"]) {
if (isUnix()) {
sh 'make test'
} else {
powershell 'make test'
}
} // withEnv
} //stage
} else {
echo "Skipping test ${testName} for ${imageName} as ${testHarness} does not exist"
} // if else
} // each
// Automatic tagging on principal branch is not enabled by default
if (semVerEnabledOnPrimaryBranch) {
stage("Semantic Release of ${imageName}") {
echo "Configuring credential.helper"
// The credential.helper will execute everything after the '!', here echoing the username, the password and an empty line to be passed to git as credentials when git needs it.
if (isUnix()) {
sh 'git config --local credential.helper "!set -u; echo username=\\$GIT_USERNAME && echo password=\\$GIT_PASSWORD && echo"'
} else {
// Using 'bat' here instead of 'powershell' to avoid variable interpolation problem with $
bat 'git config --local credential.helper "!sh.exe -c \'set -u; echo username=$GIT_USERNAME && echo password=$GIT_PASSWORD && echo"\''
}
withCredentials([
usernamePassword(credentialsId: "${finalConfig.gitCredentials}", passwordVariable: 'GIT_PASSWORD', usernameVariable: 'GIT_USERNAME')
]) {
withEnv(["NEXT_VERSION=${nextVersion}"]) {
echo "Tagging and pushing the new version: ${nextVersion}"
if (isUnix()) {
sh '''
git config user.name "${GIT_USERNAME}"
git config user.email "jenkins-infra@googlegroups.com"
git tag -a "${NEXT_VERSION}" -m "${IMAGE_NAME}"
git push origin --tags
'''
} else {
powershell '''
git config user.email "jenkins-infra@googlegroups.com"
git config user.password $env:GIT_PASSWORD
git tag -a "$env:NEXT_VERSION" -m "$env:IMAGE_NAME"
git push origin --tags
'''
}
} // withEnv
} // withCredentials
} // stage
} // if
}// withDockerPullCredentials
infra.withDockerPushCredentials{
if (env.TAG_NAME || env.BRANCH_IS_PRIMARY) {
stage("Deploy ${imageName}") {
final InfraConfig infraConfig = new InfraConfig(env)
String imageDeployName = infraConfig.dockerRegistry + '/' + imageName
if (env.TAG_NAME) {
// User could specify a tag in the image name. In that case the git tag is appended. Otherwise the docker tag is set to the git tag.
if (imageDeployName.contains(':')) {
imageDeployName += "-${env.TAG_NAME}"
} else {
imageDeployName += ":${env.TAG_NAME}"
}
}
withEnv(["IMAGE_DEPLOY_NAME=${imageDeployName}"]) {
// Please note that "make deploy" uses the environment variable "IMAGE_DEPLOY_NAME"
if (isUnix()) {
sh 'make deploy'
} else {
powershell 'make deploy'
}
} // withEnv
} //stage
} // if
} // withDockerPushCredentials
if (env.TAG_NAME && finalConfig.automaticSemanticVersioning) {
stage('GitHub Release') {
withCredentials([
usernamePassword(credentialsId: "${finalConfig.gitCredentials}", passwordVariable: 'GITHUB_TOKEN', usernameVariable: 'GITHUB_USERNAME')
]) {
String origin = ''
if (isUnix()) {
origin = sh(returnStdout: true, script: 'git remote get-url origin').trim() - '.git'
} else {
origin = powershell(returnStdout: true, script: 'git remote get-url origin').trim() - '.git'
}
final String org = origin.split('/')[3]
final String repository = origin.split('/')[4]
final String ghReleasesApiUri = "/repos/${org}/${repository}/releases"
withEnv(["GH_RELEASES_API_URI=${ghReleasesApiUri}"]) {
int releaseId = 0
if (isUnix()) {
releaseId = sh(returnStdout: true, script: 'gh api ${GH_RELEASES_API_URI} | jq -e -r \'[ .[] | select(.draft == true and .name == "next").id] | max\' || echo 0').trim()
} else {
releaseId = powershell(returnStdout: true, script: 'gh api $env:GH_RELEASES_API_URI | jq -e -r \'[ .[] | select(.draft == true and .name == "next").id] | max\' || echo 0').trim()
}
if (releaseId > 0) {
withEnv(["GH_NEXT_RELEASE_URI=${ghReleasesApiUri}/${releaseId}"]) {
if (isUnix()) {
sh 'gh api -X PATCH -F draft=false -F name="${TAG_NAME}" -F tag_name="${TAG_NAME}" "${GH_NEXT_RELEASE_URI}"'
} else {
powershell 'gh api -X PATCH -F draft=false -F name="$env:TAG_NAME" -F tag_name="$env:TAG_NAME" "$env:GH_NEXT_RELEASE_URI"'
}
} // withEnv
} else {
echo "No draft release found for ${org}/${repository}"
} // if
} // withEnv
} // withCredentials
} // stage
} // if
} // withEnv
} // node
} // call