forked from subutai-io/agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
186 lines (156 loc) · 5.76 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
#!groovy
notifyBuildDetails = ""
agentCommitId = ""
agentVersion = ""
try {
notifyBuild('STARTED')
/* Building agent binary.
Node block used to separate agent and subos code.
*/
node() {
String goenvDir = ".goenv"
deleteDir()
stage("Checkout source")
/* checkout agent repo */
notifyBuildDetails = "\nFailed on Stage - Checkout source"
checkout scm
agentCommitId = sh (script: "git rev-parse HEAD", returnStdout: true)
agentVersion = sh (script: """
set +x
version=\$(git describe --abbrev=0 --tags | tr -d '\n')
[[ "\$version" =~ (.*)([0-9]+)([^0-9]*)\$ ]] && version="\${BASH_REMATCH[1]}\$((\${BASH_REMATCH[2]} + 1))\${BASH_REMATCH[3]}";
echo \$version | tr -d '\n'
""", returnStdout: true).trim()
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/agent
"""
stage("Build Agent")
/* Build subutai binary */
notifyBuildDetails = "\nFailed on Stage - Build Agent"
sh """
export GOPATH=${workspace}/${goenvDir}
export GOBIN=${workspace}/${goenvDir}/bin
export GIT_BRANCH=${env.BRANCH_NAME}
make
"""
/* stash subutai binary and agent config file to use it in next node() */
stash includes: 'subutai', name: 'subutai'
}
node() {
/* Checkout subos repo and push new subutai binary */
deleteDir()
stage("Push new subutai binary to subos repo")
/* Get subutai binary from stage and push it to same branch of subos repo
*/
notifyBuildDetails = "\nFailed on Stage - Push new subutai binary to subos repo"
String subosRepoName = "github.com/subutai-io/subos.git"
git branch: "${env.BRANCH_NAME}", changelog: false, credentialsId: 'hub-optdyn-github-auth', poll: false, url: "https://${subosRepoName}"
/* replace subutai binary */
dir("subutai/bin") {
unstash 'subutai'
}
sh """
sed 's/branch =.*/branch = ${env.BRANCH_NAME}/g' -i subutai/etc/agent.gcfg
sed 's/version =.*/version = ${agentVersion}/g' -i subutai/etc/agent.gcfg
"""
def gitStatusSubos = sh(script: 'git status --porcelain', returnStdout: true)
if (gitStatusSubos != '') {
withCredentials([[$class: 'UsernamePasswordMultiBinding',
credentialsId: 'hub-optdyn-github-auth',
passwordVariable: 'GIT_PASSWORD',
usernameVariable: 'GIT_USER']]) {
sh """
git config user.email [email protected]
git config user.name 'Jenkins Admin'
git commit subutai/bin/subutai subutai/etc/agent.gcfg -m 'Push subutai version from subutai-io/agent@${agentCommitId}'
git push https://${env.GIT_USER}:'${env.GIT_PASSWORD}'@${subosRepoName} ${env.BRANCH_NAME}
"""
}
}
}
node("snapcraft") {
/*
** Trigger snapcraft build (subutai-io/snap)
** - Check version in snapcraft.yaml, and update if need
** - If version is not changed, trigger build
*/
// now snapcraft builder working only for dev branch
if (env.BRANCH_NAME == 'dev') {
stage("Update version on snap repo")
deleteDir()
String snapRepoName = "github.com/subutai-io/snap.git"
git branch: "${env.BRANCH_NAME}", changelog: false, credentialsId: 'hub-optdyn-github-auth', poll: false, url: "https://${snapRepoName}"
sh """
sed 's/version:.*/version: \"${agentVersion}-(BRANCH)\"/g' -i snapcraft.yaml.templ
"""
def gitStatusSnap = sh(script: 'git status --porcelain', returnStdout: true)
if (gitStatusSnap != '') {
withCredentials([[$class: 'UsernamePasswordMultiBinding',
credentialsId: 'hub-optdyn-github-auth',
passwordVariable: 'GIT_PASSWORD',
usernameVariable: 'GIT_USER']]) {
sh """
git config user.email [email protected]
git config user.name 'Jenkins Admin'
git commit snapcraft.yaml.templ -m 'Push subutai version from subutai-io/agent@${agentCommitId}'
git push https://${env.GIT_USER}:'${env.GIT_PASSWORD}'@${snapRepoName} ${env.BRANCH_NAME}
"""
}
} else {
build job: 'snap.subutai-io.pipeline/dev/', propagate: false, wait: false
}
}
}
} 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-bots-slack-token')
// Send notifications
slackSend (color: colorCode, message: summary, teamDomain: 'subutai-io', 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
}