-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathJenkinsfile
165 lines (150 loc) · 6.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
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
// Variables we want to define with a script{} block should be declared outside the pipeline
def startdate = "UNKNOWN"
pipeline {
// Default agent label to run all stages which don't override setting
agent { label "linux" }
parameters {
string(defaultValue: '', description: 'Tag Version, e.g. v1.2.2', name: 'tagVersion')
booleanParam(defaultValue: false, description: 'Make a release?', name: 'makeRelease')
}
options {
// Tell Git about the build stages we're about to run
// gitLabConnection('TODO')
// gitlabBuilds(builds: ['Prep', 'Build'])
// Add timestamps to console log
timestamps()
}
// Default tools to install on agent
tools {
jdk 'jdk8'
gradle 'gradle4'
}
stages {
stage('Prep') {
steps {
gitlabCommitStatus(name: 'Prep') {
// Output parameters
echo "Create Release: ${env.makeRelease}"
echo "Tag Version: ${env.tagVersion}"
// Some git state survives between builds, even tags that weren't pushed. This gives us a clean slate to work from.
script {
if ( params.makeRelease.equals(true) ) {
echo "Cleaning git cache for release."
gitClean()
}
}
// Check out revision that was used to fetch the Jenkinsfile running the pipeline
checkout scm
// Run groovy script, we can access the full Groovy sandbox and approved classes this way
script {
startdate = new Date().format("yyyyMMddHHmm")
}
}
}
}
stage('Build') {
steps {
// Run build on platforms in parallel
parallel (
// Run on default agent
"linux": {
//withCredentials([usernamePassword(credentialsId: 'TODO', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) {
gitlabCommitStatus(name: 'Build') {
sh "gradle clean -PmakeRelease=${env.makeRelease} -Partifactory_user=${USERNAME} -Partifactory_password=${PASSWORD}"
sh "gradle -i -PmakeRelease=${env.makeRelease} -Partifactory_user=${USERNAME} -Partifactory_password=${PASSWORD}"
}
//}
}
)
}
}
stage('Release') {
steps {
script {
if (env.BRANCH_NAME.equals("master")) {
if ( params.makeRelease.equals(true) ) {
gitlabCommitStatus(name: 'Deliver') {
if (params.tagVersion.equals("") ){
error("A valid tagVersion must be specified. Invalid tagVersion: '${env.tagVersion}'")
} else {
echo "Tagging and pushing to Git"
sh("git tag -a ${env.tagVersion} -m 'version ${env.tagVersion}'")
// TODO FIXME
//sshagent(['TODO']) {
// sh("git push --tags")
//}
}
}
}
}
}
}
}
stage('Deploy') {
steps {
script {
if (env.BRANCH_NAME.equals("master")) {
gitlabCommitStatus(name: 'Deliver') {
// TODO Fix credentials
//withCredentials([usernamePassword(credentialsId: 'TODO', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) {
sh "gradle publish -Pdo_publishing=true -Partifactory_user=${USERNAME} -Partifactory_password=${PASSWORD} -PmakeRelease=${env.makeRelease}"
//}
}
} else{
gitlabCommitStatus(name: 'Deliver') {
echo "Skipping publish on branch"
}
}
}
}
}
}
// A post-pipeline "stage" to run various cleanup
post {
failure {
updateGitlabCommitStatus name: 'Build', state: 'failed'
}
success {
updateGitlabCommitStatus name: 'Build', state: 'success'
}
always {
// Check if we skipped the real deliver stage to close out the build in GitLab
script {
// Generated these from the Pipeline Syntax page in Jenkins.
jacoco classPattern: '**/build/classes', exclusionPattern: '**/edu/**', execPattern: '**/build/jacoco/*.exec'
findbugs canComputeNew: false, defaultEncoding: '', excludePattern: '', healthy: '', includePattern: '', pattern: '**/findbugs/main.xml', unHealthy: ''
pmd canComputeNew: false, defaultEncoding: '', healthy: '', pattern: '**/pmd/*.xml', unHealthy: ''
junit '**/build/test-results/test/*.xml'
deleteDir() /* clean up workspace */
}
}
}
}
/**
* Clean a Git project workspace.
* Uses 'git clean' if there is a repository found.
* Uses Pipeline 'deleteDir()' function if no .git directory is found.
*/
def gitClean() {
timeout(time: 60, unit: 'SECONDS') {
if (fileExists('.git')) {
echo "Found Git repository: using Git to clean the tree."
// The sequence of reset --hard and clean -fdx first
// in the root and then using submodule foreach
// is based on how the Jenkins Git SCM clean before checkout
// feature works.
sh 'git reset --hard'
// Note: -e is necessary to exclude the temp directory
// .jenkins-XXXXX in the workspace where Pipeline puts the
// batch file for the 'bat' command.
sh 'git clean -ffdx -e ".jenkins-*/"'
sh 'git submodule foreach --recursive git reset --hard'
sh 'git submodule foreach --recursive git clean -ffdx'
}
else
{
echo "No Git repository found: using deleteDir() to wipe clean"
deleteDir()
}
}
}