forked from Wind-River/ci-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile
272 lines (248 loc) · 10.7 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
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
263
264
265
266
267
268
269
270
271
272
#!/usr/bin/env groovy
// -*- mode: groovy; tab-width: 2; groovy-indent-offset: 2 -*-
// Copyright (c) 2017 Wind River Systems Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
node('docker') {
// function for adding array of env vars to string
def add_env = {
String command, String[] env_args ->
for ( arg in env_args ) {
if ( arg.contains('EMAIL=') ) {
arg = arg.replace(' ', ',')
}
command = command + " -e ${arg} "
}
return command
}
// function to run docker command after ensuring the image is up to date
def docker_run = {
String params, String image, String cmd ->
imageId = sh(returnStdout: true, script: "docker image inspect --format='{{json .Id}}' ${image} | tr -d '\"'").trim()
if ( imageId.startsWith('sha256') ) {
ret = sh(returnStatus: true, script: "docker pull ${image}")
if ( ret != 0 ) {
echo "Docker pull failed. Using local version of image: ${imageId}"
} else {
echo "Docker image ${image} is up to date"
}
} else {
def counter = 10
while ( counter > 0 ) {
ret = sh(returnStatus: true, script: "docker pull ${image}")
if ( ret == 0 ) {
break
}
echo "Docker pull of ${image} failed. Waiting 60 seconds"
sleep 60
counter = counter - 1
}
if ( counter == 0 ) {
error "Unable to pull ${image}. Please check image name and registry availablity. Cannot continue."
}
}
echo "Using image ${image} with ID: ${imageId}"
sh "docker run ${params} ${image} ${cmd}"
}
// Node name is from docker swarm is hostname + dash + random string. Remove random part of recover hostname
def hostname = "${NODE_NAME}"
hostname = hostname[0..-10]
def common_docker_params = "--rm --name build-${BUILD_ID} --hostname ${hostname} -i --tmpfs /tmp --tmpfs /var/tmp -v /etc/localtime:/etc/localtime:ro -u 1000 -v ci_jenkins_agent:/home/jenkins --ulimit nofile=1024:1024 "
common_env_args = ["LANG=en_US.UTF-8", "BUILD_ID=${BUILD_ID}", "WORKSPACE=${WORKSPACE}", "JENKINS_URL=${JENKINS_URL}", "BUILD_GROUP_ID=" + params.BUILD_GROUP_ID, "NODE_NAME=${NODE_NAME}" ]
common_docker_params = add_env( common_docker_params, common_env_args )
def BUILD_DIR="${WORKSPACE}/builds/builds-${BUILD_ID}"
// set the build display name
currentBuild.displayName = "#${BUILD_NUMBER}-${NAME}"
stage('Docker Run Check') {
dir('ci-scripts') {
git(url:params.CI_REPO, branch:params.CI_BRANCH)
}
sh "${WORKSPACE}/ci-scripts/docker_run_check.sh"
}
stage('Cache Sources') {
dir('ci-scripts') {
git(url:params.CI_REPO, branch:params.CI_BRANCH)
}
def env_args = ["BASE=${WORKSPACE}/..", "REMOTE=${REMOTE}"]
def docker_params = add_env( common_docker_params, env_args )
if (params.GIT_CREDENTIAL == "enable") {
withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId:"${GIT_CREDENTIAL_ID}", usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD']]) {
writeFile (file: "credentials.txt", text: "${USERNAME}:${PASSWORD}")
}
}
String cmd="${WORKSPACE}/ci-scripts/wrlinux_update.sh ${BRANCH}"
docker_run("${docker_params}", "${REGISTRY}/ubuntu1804_64", "${cmd}")
// cleanup credentials
if (params.GIT_CREDENTIAL == "enable") {
sh "rm -f credentials.txt url_credentials.txt"
}
}
stage('Layerindex Setup') {
// if devbuilds are enabled, start build in same network as layerindex
if (params.DEVBUILD_ARGS != "") {
dir('ci-scripts') {
git(url:params.CI_REPO, branch:params.CI_BRANCH)
}
devbuild = readYaml text: params.DEVBUILD_ARGS
devbuild_env = ["LAYERINDEX_SOURCE=${LAYERINDEX_SOURCE}"]
devbuild_env = devbuild_env + ["BITBAKE_REPO_URL=${BITBAKE_REPO_URL}"]
withEnv(devbuild_env) {
dir('ci-scripts/layerindex') {
try {
sh "./layerindex_start.sh --type=${LAYERINDEX_TYPE}"
for ( repo in devbuild.repos ) {
for ( layer in repo.layers ) {
layer_args = [
"DEVBUILD_LAYER_NAME=${layer}",
"DEVBUILD_LAYER_VCS_URL=${repo.repo}",
"DEVBUILD_LAYER_ACTUAL_BRANCH=${repo.branch}"]
if (!repo.repo.endsWith(layer)) {
layer_args = layer_args + ["DEVBUILD_LAYER_VCS_SUBDIR=${layer}"]
}
withEnv(layer_args) {
sh "./layerindex_layer_update.sh"
}
}
}
sh "./layerindex_export.sh --branch=${BRANCH}"
sh "mkdir -p ${BUILD_DIR}"
sh "mv -f layerindex.json ${BUILD_DIR}"
} finally {
sh "./layerindex_stop.sh"
}
}
}
}
else {
println("Not starting local LayerIndex")
}
}
try {
stage('Build') {
dir('ci-scripts') {
git(url:params.CI_REPO, branch:params.CI_BRANCH)
}
def docker_params = common_docker_params
def env_args = ["MESOS_TASK_ID=${BUILD_ID}", "BASE=${WORKSPACE}", "REMOTE=${REMOTE}"]
env_args = env_args + ["NAME=${NAME}", "BRANCH=${BRANCH}"]
env_args = env_args + ["SETUP_ARGS=\'${SETUP_ARGS}\'"]
env_args = env_args + ["PREBUILD_CMD=\'${PREBUILD_CMD}\'", "PREBUILD_CMD_FOR_TEST=\'${PREBUILD_CMD_FOR_TEST}\'"]
env_args = env_args + ["TEST=${TEST}", "TEST_CONFIGS_FILE=${TEST_CONFIGS_FILE}"]
env_args = env_args + ["BUILD_CMD=\'${BUILD_CMD}\'", "TOASTER=${TOASTER}"]
env_args = env_args + ["BUILD_CMD_FOR_TEST=\'${BUILD_CMD_FOR_TEST}\'"]
if (params.MACHINE != "") {
env_args = env_args + ["MACHINE=\'${MACHINE}\'"]
}
if (params.DISTRO != "") {
env_args = env_args + ["DISTRO=\'${DISTRO}\'"]
}
String image = "${REGISTRY}/${IMAGE}"
if ( params.IMAGE.contains('/') ) {
image = params.IMAGE
}
// Capture login params without toaster args
def login_params = docker_params
login_params = login_params + " --entrypoint /bin/bash -w=${BUILD_DIR} "
login_params = add_env( login_params, env_args)
sh "mkdir -p ${BUILD_DIR}"
writeFile file: "${BUILD_DIR}/build_login.sh", text: "docker run -it --init ${login_params} ${image}"
if (params.TOASTER == "enable") {
docker_params = docker_params + ' --expose=8800 -P '
env_args = env_args + ["SERVICE_NAME=toaster", "SERVICE_CHECK_HTTP=/health"]
}
docker_params = add_env( docker_params, env_args )
def cmd="${WORKSPACE}/ci-scripts/jenkins_build.sh"
if (params.LOCALCONF != "") {
writeFile file: "${BUILD_DIR}/local.conf", text: params.LOCALCONF
}
try {
docker_run("${docker_params}", "${image}", "${cmd}")
} catch (err) {
def err_message = err.getMessage()
if (err_message == "script returned exit code 2") {
echo "Build stage succeeded but with errors!"
currentBuild.result = 'UNSTABLE'
}
else {
echo "Build stage failed!"
currentBuild.result = 'FAILURE'
throw err
}
}
}
} finally {
stage('Post Process') {
dir('ci-scripts') {
git(url:params.CI_REPO, branch:params.CI_BRANCH)
}
def docker_params = common_docker_params
if (! params.POSTPROCESS_ARGS.contains('SYNC_SERVER')) {
docker_params = docker_params + " --network=rsync_net"
}
def env_args = ["NAME=${NAME}"]
env_args = env_args + ["POST_SUCCESS=${POST_SUCCESS}", "POST_FAIL=${POST_FAIL}", "TEST=" + params.TEST]
env_args = env_args + params.POSTPROCESS_ARGS.tokenize(',')
docker_params = add_env( docker_params, env_args )
def cmd="${WORKSPACE}/ci-scripts/build_postprocess.sh"
docker_run("${docker_params}", "${REGISTRY}/${POSTPROCESS_IMAGE}", "${cmd}")
}
}
try {
stage('Test') {
if (params.TEST != 'disable' && params.TEST != '') {
dir('ci-scripts') {
git(url:params.CI_REPO, branch:params.CI_BRANCH)
}
def docker_params = common_docker_params
def env_args = ["NAME=${NAME}"]
env_args = env_args + ["TEST=" + params.TEST, "RUNTIME_TEST_CMD=\'${RUNTIME_TEST_CMD}\'"]
env_args = env_args + params.TEST_ARGS.tokenize(',')
env_args = env_args + params.POSTPROCESS_ARGS.tokenize(',')
docker_params = add_env( docker_params, env_args )
def login_params = docker_params
login_params = login_params + " --entrypoint /bin/bash -w=${BUILD_DIR} "
login_params = add_env( login_params, env_args)
sh "mkdir -p ${BUILD_DIR}"
writeFile file: "${BUILD_DIR}/test_login.sh", text: "docker run -it --init ${login_params} ${REGISTRY}/${TEST_IMAGE}"
def cmd="${WORKSPACE}/ci-scripts/${RUNTIME_TEST_CMD}"
docker_run("${docker_params}", "${REGISTRY}/${TEST_IMAGE}", "${cmd}")
} else {
println("Test is disabled, ignore 'Test' stage.")
}
}
} finally {
stage('Post Test') {
if (params.TEST != 'disable' && params.TEST != '') {
dir('ci-scripts') {
git(url:params.CI_REPO, branch:params.CI_BRANCH)
}
def docker_params = common_docker_params
def env_args = ["NAME=${NAME}"]
env_args = env_args + ["POST_TEST_SUCCESS=${POST_TEST_SUCCESS}", "POST_TEST_FAIL=${POST_TEST_FAIL}"]
env_args = env_args + params.TEST_ARGS.tokenize(',')
env_args = env_args + params.POSTPROCESS_ARGS.tokenize(',')
docker_params = add_env( docker_params, env_args )
def cmd="${WORKSPACE}/ci-scripts/test_postprocess.sh"
docker_run("${docker_params}", "${REGISTRY}/${POST_TEST_IMAGE}", "${cmd}")
} else {
println("Test is disabled, ignore 'Post Test' stage.")
}
}
}
}