forked from macg33zr/pipelineUnit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
110 lines (85 loc) · 2.81 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
/**
* Helper to validate a pipeline.
*
* @param pipelineFile - Pipeline file to validate
* @return An error count
*/
int validatePipeline(String pipelineFile) {
Boolean valid = validateDeclarativePipeline(pipelineFile)
if(!valid) {
echo "The file ${pipelineFile} is not a valid declarative pipeline"
return 1
}
return 0
}
/**
* A CI pipeline for Jenkins pipeline jobs. It will validate the jobs and run the unit tests with Gradle
*/
pipeline {
agent any
parameters {
booleanParam(name: 'VALIDATE', defaultValue: true, description: 'Whether to run validation stage')
string(name: 'GRADLE_TASKS_OPTIONS', defaultValue: 'clean build test -i', description: 'Tasks and options for the gradle command')
}
options {
buildDiscarder(logRotator(numToKeepStr: '10'))
timestamps()
}
triggers {
pollSCM('*/5 * * * *')
}
stages {
stage('Checkout') {
steps {
deleteDir()
checkout scm
}
}
stage('validate') {
when { expression { return params.VALIDATE } }
steps {
script {
int validationErrors = 0
// Validate the example jobs. This will only work for declarative
validationErrors += validatePipeline('exampleJobs/parallel/Jenkinsfile')
// Validate this job
validationErrors += validatePipeline('Jenkinsfile')
// Fail here if any not valid - need to fix this first
if(validationErrors > 0) {
error("One or more of the pipeline files are not valid. Validation errors: ${validationErrors}")
}
}
}
}
stage('build') {
steps {
withEnv(["GRADLE_HOME=${tool name: 'GRADLE_3', type: 'hudson.plugins.gradle.GradleInstallation'}"]) {
withEnv(["PATH=${env.PATH}:${env.GRADLE_HOME}/bin"]) {
// Checking the env
echo "GRADLE_HOME=${env.GRADLE_HOME}"
echo "PATH=${env.PATH}"
sh "gradle ${params.GRADLE_TASKS_OPTIONS}"
}
}
}
}
}
post {
always {
echo 'pipeline unit tests completed - recording JUnit results'
junit 'build/test-results/**/*.xml'
}
success {
echo 'pipeline unit tests PASSED'
}
failure {
echo 'pipeline unit tests FAILED'
}
changed {
echo 'pipeline unit tests results have CHANGED'
}
unstable {
echo 'pipeline unit tests have gone UNSTABLE'
}
}
}