forked from jetstack/kube-lego
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile
103 lines (86 loc) · 3.24 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
// vim: et:ts=4:sw=4:ft=groovy
def jenkinsSlack(type){
def jobInfo = "\n » ${env.JOB_NAME} ${env.BUILD_NUMBER} (<${env.BUILD_URL}|job>) (<${env.BUILD_URL}/console|console>)"
if (type == 'start'){
slackSend color: 'blue', message: "build started${jobInfo}"
}
if (type == 'finish'){
def buildColor = currentBuild.result == null? "good": "warning"
def buildStatus = currentBuild.result == null? "SUCCESS": currentBuild.result
slackSend color: buildColor, message: "build finished - ${buildStatus}${jobInfo}"
}
}
def gitTags(commit) {
sh("git tag --contains ${commit} > GIT_TAGS")
def gitTags = readFile('GIT_TAGS').trim()
sh('rm -f GIT_TAGS')
if (gitTags == '') {
return []
}
return gitTags.tokenize('\n')
}
def gitCommit() {
sh('git rev-parse HEAD > GIT_COMMIT')
def gitCommit = readFile('GIT_COMMIT').trim()
sh('rm -f GIT_COMMIT')
return gitCommit
}
def gitMasterBranchCommit() {
sh('git rev-parse origin/master > GIT_MASTER_COMMIT')
def gitCommit = readFile('GIT_MASTER_COMMIT').trim()
sh('rm -f GIT_MASTER_COMMIT')
return gitCommit
}
def onMasterBranch(){
return gitCommit() == gitMasterBranchCommit()
}
def imageTags(){
def gitTags = gitTags(gitCommit())
if (gitTags == []) {
return ["canary"]
} else {
return gitTags + ["latest"]
}
}
node('docker'){
catchError {
def imageName = 'jetstack/kube-lego'
def imageTag = 'jenkins-build'
jenkinsSlack('start')
stage 'Checkout source code'
checkout scm
stage 'Test kube-lego'
sh "make docker_test"
step([$class: 'JUnitResultArchiver', testResults: '_test/test*.xml'])
stage 'Build kube-lego'
sh "make docker_build"
stage 'Build docker image'
sh "docker build --build-arg VCS_REF=${gitCommit().take(8)} -t ${imageName}:${imageTag} ."
if (onMasterBranch()) {
stage 'Push docker image'
withCredentials([[$class: 'FileBinding', credentialsId: '31a54b99-cab6-4a1a-9bd7-4de5e85ca0e6', variable: 'DOCKER_CONFIG_FILE']]) {
try {
// prepare docker auth
sh 'mkdir -p _temp_dockercfg'
sh 'ln -sf \$DOCKER_CONFIG_FILE _temp_dockercfg/config.json'
// get tags to push
def imageTags = imageTags()
echo "tags to push '${imageTags}'"
def desc = []
for (i = 0; i < imageTags.size(); i++) {
def repoNameTag = "${imageName}:${imageTags[i]}"
echo "Push and tag ${repoNameTag}"
sh "docker tag ${imageName}:${imageTag} ${repoNameTag}"
sh "docker --config=_temp_dockercfg push ${repoNameTag}"
desc << "${repoNameTag}"
}
currentBuild.description = desc.join("\n") + "\ngit_commit=${gitCommit().take(8)}"
} finally {
sh 'rm -rf _temp_dockercfg'
}
}
}
}
jenkinsSlack('finish')
step([$class: 'Mailer', recipients: 'christian@jetstack.io', notifyEveryUnstableBuild: true])
}