-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathJenkinsfile
125 lines (105 loc) · 3.53 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
import org.jenkinsci.plugins.pipeline.modeldefinition.Utils
import java.text.SimpleDateFormat
DOCKER_IMAGE_NAME = ''
DOCKER_IMAGE = ''
DOCKER_ARGS = '--network=services_default'
DOCKER_REGISTRY = 'registry.n-os.org:5000'
properties([
disableConcurrentBuilds(),
parameters([
booleanParam(name: 'SKIP_TESTS', defaultValue: false, description: 'Do you want to run the build with tests?')
])
])
node {
try {
pipeline()
}
catch(e) {
setBuildStatus(e.toString().take(140), 'FAILURE')
throw e
}
finally {
cleanup()
}
}
/*
******************************************************************
standard functions
these functions below implement the standard docker image pipeline
******************************************************************
*/
def pipeline() {
stage('checkout git') {
checkout scm
setBuildStatus('In progress...', 'PENDING')
}
// https://docs.cloudbees.com/docs/admin-resources/latest/plugins/docker-workflow
stage('build image') {
DOCKER_IMAGE_NAME = "${DOCKER_REGISTRY}/${getDockerImage()}:${getDockerTag()}"
DOCKER_IMAGE = docker.build(DOCKER_IMAGE_NAME, "--no-cache ${DOCKER_ARGS} .")
}
stage('run tests') {
if (fileExists('./test/run.sh') && !params.SKIP_TESTS) {
DOCKER_IMAGE.inside("${DOCKER_ARGS} --entrypoint=") {
sh 'bash /usr/build/test/run.sh'
}
}
else {
Utils.markStageSkippedForConditional('run tests')
}
}
stage('push image') {
if (BRANCH_NAME == 'master') {
DOCKER_IMAGE.push()
}
else {
Utils.markStageSkippedForConditional('push image')
}
}
stage('delete image') {
if (BRANCH_NAME == 'master') {
Utils.markStageSkippedForConditional('delete image')
}
else {
deleteDockerImage(DOCKER_IMAGE_NAME)
}
setBuildStatus('Success', 'SUCCESS')
}
}
void deleteDockerImage(image) {
sh(script: "docker rmi -f ${image}")
}
void cleanup() {
stage('schedule cleanup') {
build job: '/maintenance/starter', wait: false
}
}
String getDockerImage() {
return sh(script: "echo '${JOB_NAME}' | awk -F/ '{print \$(NF-1)}' | sed 's%docker-%%'", returnStdout: true).trim()
}
String getDockerTag() {
def shortHash = sh(script: 'git rev-parse --short HEAD', returnStdout: true).trim()
def date = new Date()
def sdf = new SimpleDateFormat("yyyyMMddHHmmss")
// semver in TAG_ID file or reference to ARG in Dockerfile
if (!fileExists('./TAG_ID')) {
return "${sdf.format(date)}.${shortHash}.b${BUILD_ID}"
}
def tagId = sh(script: 'cat ./TAG_ID', returnStdout: true).trim()
if (tagId ==~ /^[A-Z_]+$/) {
return sh(script: "awk -F= '/ARG ${tagId}=/{print \$2}' Dockerfile", returnStdout: true).trim()
}
else {
return tagId
}
}
void setBuildStatus(message, state) {
def repoUrl = sh(script: 'git config --get remote.origin.url', returnStdout: true).trim()
step([
$class: "GitHubCommitStatusSetter",
reposSource: [$class: "ManuallyEnteredRepositorySource", url: repoUrl],
contextSource: [$class: "ManuallyEnteredCommitContextSource", context: "ci/jenkins/build-status"],
errorHandlers: [[$class: "ChangingBuildStatusErrorHandler", result: "UNSTABLE"]],
statusResultSource: [ $class: "ConditionalStatusResultSource", results: [[$class: "AnyBuildResult", message: message, state: state]] ]
]);
}