-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
104 lines (85 loc) · 2.58 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
@Library("PipelineLibrary") _
import com.maxxton.pipeline.models.Project
import com.maxxton.pipeline.models.StageState
def loadEnvs(Closure closure) {
env.BITBUCKET = credentials("bitbucket")
env.BITBUCKET_SSH = "bitbucket_ssh"
env.BITBUCKET_URL = "https://stash.maxxton.com/rest"
// Google Kubernetes clusters
env.GCE_MAXXTON_CREDENTIALS_ID = "gcr:${GCE_MAXXTON_PROJECT_ID}"
env.GCE_MAXXTON_DOCKER_REGISTRY = "eu.gcr.io"
closure()
}
node("master") {
loadEnvs {
try {
stage("Init Build") {
//Stop previous builds
def info = getRepositoryInfo(env.CHANGE_URL)
stopPreviousBuilds(list([]))
}
} catch (e) {
bitbucket.setBuildFailed()
if (env.CHANGE_URL == null) {
notification.sendMail("mxts", "[email protected]")
}
throw e;
}
}
}
node("mcms-slave") {
timeout(15) {
loadEnvs {
try {
properties([
buildDiscarder(logRotator(numToKeepStr: '2'))
])
def info
def tag
stage("Init Workspace") {
// Initialize stuff
sshagent([env.BITBUCKET_SSH]) {
sh "chmod 644 ~/.ssh/config || true"
sh "chmod 644 ~/.ssh/config || true"
checkout scm
info = getRepositoryInfo(env.CHANGE_URL)
tag = env.BRANCH_NAME
}
}
stage("Build Docker image") {
bitbucket.notifyStage("Build Docker image") { stage ->
sh "docker build -t ${info.name}:${tag} ."
}
}
stage("Push Docker image") {
bitbucket.notifyStage("Push Docker image") { stage ->
def gceDockerRegistry = GCE_MAXXTON_DOCKER_REGISTRY
def gceCredentialsId = GCE_MAXXTON_CREDENTIALS_ID
def gceProjectId = GCE_MAXXTON_PROJECT_ID
// Push image to gcr.io
retry(3) {
try {
timeout(5) {
docker.withRegistry("https://${gceDockerRegistry}", gceCredentialsId) {
sh "docker tag ${info.name}:${tag} ${gceDockerRegistry}/${gceProjectId}/${info.name}:${tag}"
docker.image("${gceDockerRegistry}/${gceProjectId}/${info.name}:${tag}").push()
}
}
} catch (e) {
sleep 10
throw e;
}
}
}
}
bitbucket.setBuildSucceed()
} catch (e) {
bitbucket.setBuildFailed()
if (env.CHANGE_URL == null) {
notification.sendMail("mxts", "[email protected]")
}
throw e;
}
}
}
}