-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathJenkinsfile
223 lines (207 loc) · 9.15 KB
/
Jenkinsfile
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
import org.jenkinsci.plugins.pipeline.modeldefinition.Utils
import org.jenkinsci.plugins.workflow.steps.FlowInterruptedException
@Library('jenkins-shared-libraries') _
def DEPLOY = params.getOrDefault("DEPLOY", false)
// tag as SNAPSHOT if not `master` or is `pr`
def SNAPSHOT = DEPLOY && env.BRANCH_NAME != 'master';
def TIMESTAMP = (new Date()).format("yyyyddMMHHmmss")
def APPROVED = false
// This looks like shit because of
// - https://issues.jenkins-ci.org/browse/JENKINS-35988
// - https://issues.jenkins-ci.org/browse/JENKINS-36195
pipeline {
agent none
parameters {
booleanParam defaultValue: false, description: 'Deploy tagged images to docker hub.', name: 'DEPLOY'
}
options {
buildDiscarder logRotator(artifactDaysToKeepStr: '', artifactNumToKeepStr: '50', daysToKeepStr: '', numToKeepStr: '1000')
timeout(time: 2, unit: 'HOURS')
disableResume()
durabilityHint 'PERFORMANCE_OPTIMIZED'
disableConcurrentBuilds()
skipStagesAfterUnstable()
}
environment {
TIMESTAMP = "$TIMESTAMP"
}
stages {
stage('Approve') {
steps {
script {
try
{
// don't allow skipping for master branch.
if (BRANCH_NAME.equals('master'))
{
APPROVED = true
}
else
{
timeout(time: 30, unit: "MINUTES") {
DEPLOY = input(
message: 'Approve this build? (please notify us in the #community channel to approve)',
submitter: 'administrators,strongbox-core,strongbox-pro,strongbox-oss',
parameters: [
booleanParam(defaultValue: false, description: 'Should we also deploy?', name: 'DEPLOY')
]
)
// retardness - if you "abort" the input, it will throw an exception,
// however if you "approve" it returns "null"!? This "fixes" it.
APPROVED = true
SNAPSHOT = true
}
}
}
catch (FlowInterruptedException e)
{
println "Skipping build"
APPROVED = false
SNAPSHOT = false
currentBuild.result = "NOT_BUILT"
}
}
}
}
stage('Build & Publish') {
when {
expression { APPROVED == true }
}
steps {
script {
def buildStages = [:]
buildStages.put("alpine", distributionBuildStages("alpine", BRANCH_NAME, DEPLOY, SNAPSHOT))
buildStages.put("debian", distributionBuildStages("debian", BRANCH_NAME, DEPLOY, SNAPSHOT))
buildStages.put("centos", distributionBuildStages("centos", BRANCH_NAME, DEPLOY, SNAPSHOT))
buildStages.put("mkdocs", distributionBuildStages("mkdocs", BRANCH_NAME, DEPLOY, SNAPSHOT, false))
buildStages.put("opensuse", distributionBuildStages("opensuse", BRANCH_NAME, DEPLOY, SNAPSHOT))
buildStages.put("ubuntu", distributionBuildStages("ubuntu", BRANCH_NAME, DEPLOY, SNAPSHOT))
parallel buildStages
}
}
}
}
}
def distributionBuildStages(DISTRIBUTION, BRANCH_NAME, DEPLOY, SNAPSHOT, BUILD_JDKS = true) {
return {
node('alpine-docker')
{
def IMAGES = []
stage(DISTRIBUTION) {
try {
stage('node') {
container("docker") {
nodeInfo("docker")
checkout scm
}
}
stage('base') {
container("docker") {
script {
def built = processDockerfiles(findDockerfiles((String) "./images/$DISTRIBUTION/Dockerfile.$DISTRIBUTION*"), SNAPSHOT);
IMAGES = IMAGES + built
}
}
}
if(BUILD_JDKS)
{
stage('jdk8') {
container("docker") {
script {
def built = processDockerfiles(findDockerfiles((String) "./images/$DISTRIBUTION/jdk8"), SNAPSHOT);
IMAGES = IMAGES + built
}
}
}
stage('jdk11') {
container("docker") {
script {
def built = processDockerfiles(findDockerfiles((String) "./images/$DISTRIBUTION/jdk11"), SNAPSHOT);
IMAGES = IMAGES + built
}
}
}
stage('jdk17') {
container("docker") {
script {
def built = processDockerfiles(findDockerfiles((String) "./images/$DISTRIBUTION/jdk17"), SNAPSHOT);
IMAGES = IMAGES + built
}
}
}
}
stage('publish') {
if(BRANCH_NAME != "master" && DEPLOY != true) {
Utils.markStageSkippedForConditional(STAGE_NAME)
return;
}
container("docker") {
script {
echo "Images to push: " + IMAGES.toString()
withDockerRegistry([credentialsId: '5b811a23-b4cf-4f4c-b944-2ef6a88e80a3', url: "https://index.docker.io/v1/"]) {
def attempt = 0
retry(20) {
// wait for a moment, might be a temporary network issue?
if(attempt > 0) {
sleep 30
}
attempt++;
IMAGES.each {
sh label: "Publishing ${it}...",
script: "docker push ${it}"
}
}
}
}
}
}
} finally {
archiveArtifacts '**/*.build.log'
}
}
}
}
}
def findDockerfiles(path, maxDepth = "")
{
def findArgs = maxDepth ? " -maxdepth $maxDepth " : ""
// !!!!!!!! DO NOT MESS WITH THE QUOTES OR YOU WILL REGRET IT !!!!!!!!
// https://gist.github.com/Faheetah/e11bd0315c34ed32e681616e41279ef4
return sh(script: """find $path $findArgs -type f -name "*Dockerfile*" ! -name "*build.log" | sort | xargs""", label: "Searching for Dockerfiles", returnStdout: true)
}
def processDockerfiles(files, snapshot)
{
def BUILD_ARGS = snapshot ? " --snapshot " : ""
def images = [];
files.split(" ").each {
def match = (it =~ /(.*)\\/Dockerfile\.(\w+)(\.(.+))?/)
if (match.find())
{
def attempt = 0
retry(20) {
// wait for a moment, might be a temporary network issue?
if(attempt > 0) {
sleep 30
}
attempt++;
// Do not use `docker.build` because it fails with `"docker build" requires exactly 1 argument.`.
// docker.build(IMAGE, '-f ${it} --no-cache .')
def IMAGE_TAG = sh(label: "Getting image tag", script: "/bin/bash ./build.sh ${BUILD_ARGS} --get-image $it", returnStdout: true)
sh label: "DNS",
script: "cat /etc/resolv.conf"
sh label: "Patching build",
script: "sed -i '/USER root/a RUN set -ex; cat /etc/resolv.conf; echo nameserver 10.43.0.10 > /etc/resolv.conf;' $it"
sh label: "Patched version:",
script: "cat $it"
sh label: "Building $IMAGE_TAG",
script: "/bin/bash ./build.sh --no-cache ${BUILD_ARGS} $it"
images.add(IMAGE_TAG)
}
}
else
{
println "Something went wrong and we could not properly parse ${it} - skipping"
}
}
return images
}