-
Notifications
You must be signed in to change notification settings - Fork 2
/
Jenkinsfile
executable file
·162 lines (135 loc) · 4.84 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
def notify(status){
emailext (
body: '$DEFAULT_CONTENT',
recipientProviders: [
[$class: 'CulpritsRecipientProvider'],
[$class: 'DevelopersRecipientProvider'],
[$class: 'RequesterRecipientProvider']
],
replyTo: '$DEFAULT_REPLYTO',
subject: '$DEFAULT_SUBJECT',
to: '$DEFAULT_RECIPIENTS'
)
}
@NonCPS
def killall_jobs() {
def jobname = env.JOB_NAME
def buildnum = env.BUILD_NUMBER.toInteger()
def killnums = ""
def job = Jenkins.instance.getItemByFullName(jobname)
def fixed_job_name = env.JOB_NAME.replace('%2F','/')
for (build in job.builds) {
if (!build.isBuilding()) { continue; }
if (buildnum == build.getNumber().toInteger()) { continue; println "equals" }
if (buildnum < build.getNumber().toInteger()) { continue; println "newer" }
echo "Kill task = ${build}"
killnums += "#" + build.getNumber().toInteger() + ", "
build.doStop();
}
if (killnums != "") {
slackSend color: "danger", channel: "#jenkins", message: "Killing task(s) ${fixed_job_name} ${killnums} in favor of #${buildnum}, ignore following failed builds for ${killnums}"
}
echo "Done killing"
}
def buildStep(DOCKER_ROOT, DOCKERIMAGE, DOCKERTAG, DOCKERFILE, BUILD_NEXT, BUILD_PARAM) {
def fixed_job_name = env.JOB_NAME.replace('%2F','/')
try {
checkout scm;
def buildenv = '';
def tag = '';
if (env.BRANCH_NAME.equals('master')) {
buildenv = 'production';
tag = "${DOCKERTAG}";
} else if (env.BRANCH_NAME.equals('dev')) {
buildenv = 'development';
tag = "${DOCKERTAG}-dev";
} else {
throw new Exception("Invalid branch, stopping build!");
}
docker.withRegistry("https://index.docker.io/v1/", "dockerhub") {
def customImage
stage("Building ${DOCKERIMAGE}:${tag}...") {
customImage = docker.build("${DOCKER_ROOT}/${DOCKERIMAGE}:${tag}", "--build-arg BUILDENV=${buildenv} --network=host --pull -f ${DOCKERFILE} .");
}
stage("Pushing to docker hub registry...") {
customImage.push();
}
}
} catch(err) {
slackSend color: "danger", channel: "#jenkins", message: "Build Failed: ${fixed_job_name} #${env.BUILD_NUMBER} Target: ${DOCKER_ROOT}/${DOCKERIMAGE}:${tag} (<${env.BUILD_URL}|Open>)"
currentBuild.result = 'FAILURE'
notify("Build Failed: ${fixed_job_name} #${env.BUILD_NUMBER} Target: ${DOCKER_ROOT}/${DOCKERIMAGE}:${tag}")
throw err
}
}
def buildManifest(DOCKER_ROOT, DOCKERIMAGE, DOCKERTAG, DOCKERFILE, PLATFORMS, BUILD_NEXT, BUILD_PARAM) {
def fixed_job_name = env.JOB_NAME.replace('%2F','/')
try {
checkout scm;
def buildenv = '';
def tag = '';
if (env.BRANCH_NAME.equals('master')) {
buildenv = 'production';
tag = "${DOCKERTAG}";
} else if (env.BRANCH_NAME.equals('dev')) {
buildenv = 'development';
tag = "${DOCKERTAG}-dev";
} else {
throw new Exception("Invalid branch, stopping build!");
}
docker.withRegistry("https://index.docker.io/v1/", "dockerhub") {
stage("Building ${DOCKERIMAGE}:${tag} manifest...") {
sh('docker version');
def platformsString = "";
PLATFORMS.each { p ->
sh("docker pull ${DOCKER_ROOT}/${DOCKERIMAGE}:${tag}_${p}");
platformsString = "${platformsString} ${DOCKER_ROOT}/${DOCKERIMAGE}:${tag}_${p}"
}
sh("docker manifest create ${DOCKER_ROOT}/${DOCKERIMAGE}:${tag} ${platformsString}");
sh("docker manifest push ${DOCKER_ROOT}/${DOCKERIMAGE}:${tag}");
}
}
def branches = [:]
BUILD_NEXT.each { v ->
branches["Build ${v}"] = {
build(job: "${v}/${env.BRANCH_NAME}", wait: true, parameters: [string(name: 'BUILD_IMAGE', value: String.valueOf(BUILD_PARAM))]);
}
}
parallel branches;
} catch(err) {
slackSend color: "danger", channel: "#jenkins", message: "Build Failed: ${fixed_job_name} #${env.BUILD_NUMBER} Target: ${DOCKER_ROOT}/${DOCKERIMAGE}:${tag} (<${env.BUILD_URL}|Open>)"
currentBuild.result = 'FAILURE'
notify("Build Failed: ${fixed_job_name} #${env.BUILD_NUMBER} Target: ${DOCKER_ROOT}/${DOCKERIMAGE}:${tag}")
throw err
}
}
node('master') {
killall_jobs();
def fixed_job_name = env.JOB_NAME.replace('%2F','/');
slackSend color: "good", channel: "#jenkins", message: "Build Started: ${fixed_job_name} #${env.BUILD_NUMBER} (<${env.BUILD_URL}|Open>)";
checkout scm;
def branches = [:]
def project = readJSON file: "JenkinsEnv.json";
project.builds.each { v ->
branches["Build ${v.DockerRoot}/${v.DockerImage}:${v.DockerTag}"] = {
def platforms = [:];
v.Platforms.each { p ->
platforms["Build ${v.DockerRoot}/${v.DockerImage}:${v.DockerTag}_${p}"] = {
stage("Build ${p} version") {
node(p) {
buildStep(v.DockerRoot, v.DockerImage, "${v.DockerTag}_${p}", v.Dockerfile, [], v.BuildParam);
}
}
}
};
parallel platforms;
stage('Build multi-arch manifest') {
node() {
buildManifest(v.DockerRoot, v.DockerImage, v.DockerTag, v.Dockerfile, v.Platforms, v.BuildIfSuccessful, v.BuildParam);
}
}
}
}
sh "rm -rf ./*"
parallel branches;
}