-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathJenkinsfile
139 lines (119 loc) · 3.91 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
#!groovy
notifyBuildDetails = ""
cdnHost = ""
version = ""
try {
notifyBuild('STARTED')
/* Building agent binary.
Node block used to separate agent and subos code.
*/
node("deb") {
String goenvDir = ".goenv"
deleteDir()
stage("Checkout source")
/* checkout agent repo */
notifyBuildDetails = "\nFailed on Stage - Checkout source"
checkout scm
stage("Prepare GOENV")
/* Creating GOENV path
Recreating GOENV path to catch possible issues with external libraries.
*/
notifyBuildDetails = "\nFailed on Stage - Prepare GOENV"
sh """
if test ! -d ${goenvDir}; then mkdir -p ${goenvDir}/src/github.com/subutai-io/; fi
ln -s ${workspace} ${workspace}/${goenvDir}/src/github.com/subutai-io/cdn
"""
stage("Build gorjun")
/* Build gorjun binary */
notifyBuildDetails = "\nFailed on Stage - Build gorjun"
version = sh (script: """
export GOPATH=${workspace}/${goenvDir}
export GOBIN=${workspace}/${goenvDir}/bin
export GIT_BRANCH=${env.BRANCH_NAME}
export PATH=\$PATH:/usr/local/go/bin
go get -u github.com/satori/go.uuid
go get
make | cut -d ' ' -f 7 | cut -d '=' -f 2 | tr -d '\"'
""", returnStdout: true).trim()
stage("Deploy Gorjun")
/* Build binary
** upload it to appropriate cdn server
*/
notifyBuildDetails = "\nFailed on Stage - Deploy Gorjun"
switch (env.BRANCH_NAME) {
case ~/master/: cdnHost = "eu0.mastercdn.subutai.io"; break;
case ~/sysnet/: cdnHost = "eu0.sysnetcdn.subutai.io"; break;
default: cdnHost = "eu0.devcdn.subutai.io"
}
if (env.BRANCH_NAME == 'sysnet') {
sh """
set +x
scp -P 8022 gorjun ubuntu@${cdnHost}:/tmp
ssh ubuntu@${cdnHost} -p8022 <<- EOF
set -e
sudo mv /tmp/gorjun /var/snap/subutai-sysnet/common/lxc/gorjun/opt/gorjun/bin/
sudo subutai attach gorjun "systemctl restart gorjun.service"
EOF"""
// check remote gorjun version
} else {
sh """
set +x
scp -P 8022 gorjun ubuntu@${cdnHost}:/tmp
ssh ubuntu@${cdnHost} -p8022 <<- EOF
set -e
sudo mv /tmp/gorjun /var/snap/subutai-dev/common/lxc/gorjun/opt/gorjun/bin/
sudo subutai attach gorjun systemctl restart gorjun
EOF"""
// check remote gorjun version
sh """
[ "${version}" == "\$(ssh ubuntu@${cdnHost} -p8022 sudo subutai attach gorjun curl -s -q http://127.0.0.1:8080/kurjun/rest/about)" ]
"""
}
}
} catch (e) {
currentBuild.result = "FAILED"
throw e
} finally {
// Success or failure, always send notifications
notifyBuild(currentBuild.result, notifyBuildDetails)
}
// https://jenkins.io/blog/2016/07/18/pipline-notifications/
def notifyBuild(String buildStatus = 'STARTED', String details = '') {
// build status of null means successful
buildStatus = buildStatus ?: 'SUCCESSFUL'
// Default values
def colorName = 'RED'
def colorCode = '#FF0000'
def subject = "${buildStatus}: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]'"
def summary = "${subject} (${env.BUILD_URL})"
// Override default values based on build status
if (buildStatus == 'STARTED') {
color = 'YELLOW'
colorCode = '#FFFF00'
} else if (buildStatus == 'SUCCESSFUL') {
color = 'GREEN'
colorCode = '#00FF00'
} else {
color = 'RED'
colorCode = '#FF0000'
summary = "${subject} (${env.BUILD_URL})${details}"
}
// Get token
def slackToken = getSlackToken('sysnet')
// Send notifications
slackSend (color: colorCode, message: summary, teamDomain: 'optdyn', token: "${slackToken}")
}
// get slack token from global jenkins credentials store
@NonCPS
def getSlackToken(String slackCredentialsId){
// id is ID of creadentials
def jenkins_creds = Jenkins.instance.getExtensionList('com.cloudbees.plugins.credentials.SystemCredentialsProvider')[0]
String found_slack_token = jenkins_creds.getStore().getDomains().findResult { domain ->
jenkins_creds.getCredentials(domain).findResult { credential ->
if(slackCredentialsId.equals(credential.id)) {
credential.getSecret()
}
}
}
return found_slack_token
}