-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathJenkinsfile
179 lines (156 loc) · 6.03 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
#!/usr/bin/env groovy
@Field
public static final String PROJ_BASE = 'github.com/zanata/proxyhook'
// Import pipeline library for utility methods & classes:
// ansicolor(), Notifier, PullRequests, Strings
@Field
public static final String PIPELINE_LIBRARY_BRANCH = 'v0.3.0'
// GROOVY-3278:
// Using referenced String constant as value of Annotation causes compile error
@Library('[email protected]')
import org.zanata.jenkins.Notifier
import org.zanata.jenkins.PullRequests
import org.zanata.jenkins.ScmGit
import static org.zanata.jenkins.Reporting.codecov
import static org.zanata.jenkins.StackTraces.getStackTrace
import groovy.transform.Field
// The first milestone step starts tracking concurrent build order
milestone()
PullRequests.ensureJobDescription(env, manager, steps)
@Field
def pipelineLibraryScmGit
@Field
def mainScmGit
@Field
def notify
// initialiser must be run separately (bindings not available during compilation phase)
/* Only keep the 10 most recent builds. */
def projectProperties = [
[
$class: 'GithubProjectProperty',
projectUrlStr: "https://$PROJ_BASE"
],
[
$class: 'BuildDiscarderProperty',
strategy: [$class: 'LogRotator', numToKeepStr: '10']
],
]
properties(projectProperties)
// Decide whether the build should be tagged and deployed. If the result $tag
// is non-null, a corresponding git tag $tag has been created. If the build
// succeeds, we should push $tag and deploy the application. If the result
// is null, this is not the master branch, and we should not perform any
// release/deploy steps.
String makeTag() {
if (env.BRANCH_NAME == 'master') {
sh './gradlew setVersionFromBuild'
def ver = readProperties(file: 'gradle.properties')?.version
if (ver == null) return null
def tag = 'v' + ver
sh "git commit gradle.properties -m 'Update version for $ver' && git tag $tag"
return tag
} else {
return null
}
}
// use timestamps for Jenkins logs
timestamps {
// allocate a node for build+unit tests
node() {
echo "running on node ${env.NODE_NAME}"
pipelineLibraryScmGit = new ScmGit(env, steps, 'https://github.com/zanata/zanata-pipeline-library')
pipelineLibraryScmGit.init(PIPELINE_LIBRARY_BRANCH)
mainScmGit = new ScmGit(env, steps, "https://$PROJ_BASE")
mainScmGit.init(env.BRANCH_NAME)
notify = new Notifier(env, steps, currentBuild,
pipelineLibraryScmGit, mainScmGit, (env.GITHUB_COMMIT_CONTEXT) ?: 'Jenkinsfile',
)
// generate logs in colour
ansicolor {
try {
stage('Checkout') {
// notify methods send instant messages about the build progress
notify.started()
// Shallow Clone does not work with RHEL7, which uses git-1.8.3
// https://issues.jenkins-ci.org/browse/JENKINS-37229
checkout scm
// Clean the workspace
sh "git clean -fdx"
}
def tag = null
stage('Build') {
notify.startBuilding()
tag = makeTag()
// TODO run detekt
sh """./gradlew clean build shadowJar jacocoTestReport
"""
// archive build artifacts
archive "**/build/libs/*.jar"
// gather surefire results; mark build as unstable in case of failures
junit(testResults: '**/build/test-results/**/*.xml')
notify.testResults("UNIT", currentBuild.result)
if (isBuildResultSuccess()) {
// parse Jacoco test coverage
step([$class: 'JacocoPublisher'])
if (env.BRANCH_NAME == 'master') {
step([$class: 'MasterCoverageAction'])
} else if (env.BRANCH_NAME.startsWith('PR-')) {
step([$class: 'CompareCoverageAction'])
}
// send test coverage data to codecov.io
codecov(env, steps, mainScmGit)
}
}
stage('Deploy') {
if (tag && isBuildResultSuccess()) {
withCredentials(
[[$class : 'UsernamePasswordMultiBinding', credentialsId: 'zanata-jenkins',
usernameVariable: 'GIT_USERNAME', passwordVariable: 'GITHUB_OAUTH2_TOKEN']]) {
sh "git push https://$GIT_USERNAME:$GITHUB_OAUTH2_TOKEN@$PROJ_BASE $tag"
// When https://issues.jenkins-ci.org/browse/JENKINS-28335 is done, use GitPublisher instead
}
// TODO deploy binaries, docker images
/*
// TODO deploy client
if (env.PROXYHOOK_CLIENT_HOME && env.PROXYHOOK_SERVER) {
// deploy new version of client
sh "cp client/client*-fat.jar ${env.PROXYHOOK_CLIENT_HOME}/proxyhook-client-fat.jar"
sh "cp client/init.groovy.d/proxyhook_client.groovy $JENKINS_HOME/init.groovy.d/proxyhook_client.groovy"
// restart the client
sh "$JENKINS_HOME/init.groovy.d/proxyhook_client.groovy"
}
// TODO deploy server
sh "./gradlew :server:tarball -x clean -x shadowJar"
if (env.PROXYHOOK_NAMESPACE && env.PROXYHOOK_APP) {
// deploy new version of server
withCredentials([usernamePassword(
credentialsId: 'openshift',
usernameVariable: 'OPENSHIFT_LOGIN',
passwordVariable: 'OPENSHIFT_PASSWORD')]) {
docker.image('bigm/rhc').inside("-v /root/.openshift -v /private") {
sh """rhc setup --rhlogin ${env.OPENSHIFT_LOGIN}
--password ${env.OPENSHIFT_PASSWORD}"""
sh """rhc deploy --app ${env.PROXYHOOK_APP}
--namespace ${env.PROXYHOOK_NAMESPACE}
server/build/server-deploy*.tar.gz"""
}
}
}
}
*/
}
}
// Reduce workspace size
sh "git clean -fdx"
notify.successful()
} catch (e) {
notify.failed()
currentBuild.result = 'FAILURE'
throw e
}
}
}
}
private boolean isBuildResultSuccess() {
currentBuild.result in ['SUCCESS', null]
}