-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathJenkinsfile
164 lines (162 loc) · 4.92 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
def sendEmailNotification(subj, recepients) {
emailext body: "${BUILD_URL}",
recipientProviders: [
[$class: 'CulpritsRecipientProvider'],
[$class: 'DevelopersRecipientProvider'],
[$class: 'RequesterRecipientProvider']
],
subject: subj,
to: "${recepients}"
}
def printTopic(topic) {
println("[*] ${topic} ".padRight(80, '-'))
}
node {
//
def pullRequest = false
def commitSha = ''
def buildBranch = ''
def pullId = ''
def lastCommitAuthorEmail = ''
def repo = ''
def org = ''
//
def delivery = true;
if (env.BUILD_IMAGES) {
delivery = BUILD_IMAGES.toString().toBoolean();
}
def deploy = true;
if (env.CLUSTER) {
deploy = CLUSTER?.trim();
}
//
stage('Clone sources') {
//
def scmVars = checkout scm
printTopic('Job input parameters');
println(params)
printTopic('SCM variables')
println(scmVars)
//
commitSha = scmVars.GIT_COMMIT
buildBranch = scmVars.GIT_BRANCH
if (buildBranch.contains('PR-')) {
pullRequest = true
pullId = CHANGE_ID
} else if (params.containsKey('sha1')){
pullRequest = true
pullId = ghprbPullId
} else {
}
//
sh 'envsubst < .env.template > .env'
sh 'export $(cat ./.env | grep -v ^# | xargs) && envsubst < sonar-project.properties.template > sonar-project.properties'
//
printTopic('Config files')
sh 'cat ./.env';
sh 'cat ./sonar-project.properties';
//
printTopic('Build info')
echo "[PR:${pullRequest}] [BRANCH:${scmVars.GIT_BRANCH}] [COMMIT: ${scmVars.GIT_COMMIT}]"
//
printTopic('Environment variables')
echo sh(returnStdout: true, script: 'env')
//
repo = sh(returnStdout: true, script:'''git config --get remote.origin.url | rev | awk -F'[./:]' '{print $2}' | rev''').trim()
org = sh(returnStdout: true, script:'''git config --get remote.origin.url | rev | awk -F'[./:]' '{print $3}' | rev''').trim()
//
printTopic('Repo parameters')
echo "[org:${org}] [repo:${repo}]"
//
lastCommitAuthorEmail = sh(returnStdout: true, script:'''git log --format="%ae" HEAD^!''').trim()
if (!pullRequest){
lastCommitAuthorEmail = sh(returnStdout: true, script:'''git log -2 --format="%ae" | paste -s -d ",\n"''').trim()
}
printTopic('Author(s)')
echo "[lastCommitAuthorEmail:${lastCommitAuthorEmail}]"
}
try {
stage('Prereq') {
sh "./prereq.sh"
}
stage('Build') {
sh "./build.sh"
}
stage('Unit test') {
sh "./test.sh"
}
//
stage('SonarQube analysis') {
/*/
def scannerHome = tool 'SonarQube Scanner';
withSonarQubeEnv('SonarQube') {
if (pullRequest){
sh "${scannerHome}/bin/sonar-scanner -Dsonar.analysis.mode=preview -Dsonar.github.pullRequest=${ghprbPullId} -Dsonar.github.repository=${org}/${repo} -Dsonar.github.oauth=${GITHUB_ACCESS_TOKEN} -Dsonar.login=${SONARQUBE_ACCESS_TOKEN}"
} else {
sh "${scannerHome}/bin/sonar-scanner"
// check SonarQube Quality Gates
//// Pipeline Utility Steps
def props = readProperties file: '.scannerwork/report-task.txt'
echo "properties=${props}"
def sonarServerUrl=props['serverUrl']
def ceTaskUrl= props['ceTaskUrl']
def ceTask
//// HTTP Request Plugin
timeout(time: 1, unit: 'MINUTES') {
waitUntil {
def response = httpRequest "${ceTaskUrl}"
println('Status: '+response.status)
println('Response: '+response.content)
ceTask = readJSON text: response.content
return (response.status == 200) && ("SUCCESS".equals(ceTask['task']['status']))
}
}
//
def qgResponse = httpRequest sonarServerUrl + "/api/qualitygates/project_status?analysisId=" + ceTask['task']['analysisId']
def qualitygate = readJSON text: qgResponse.content
echo qualitygate.toString()
if ("ERROR".equals(qualitygate["projectStatus"]["status"])) {
currentBuild.description = "Quality Gate failure"
sendEmailNotification(currentBuild.description, lastCommitAuthorEmail)
currentBuild.result = 'UNSTABLE'
//error currentBuild.description
}
}
}
/*/
}
//
stage('Delivery') {
if (pullRequest){
} else {
// archiveArtifacts artifacts: 'package/target/*.zip'
}
}
//
stage('Deploy') {
if (pullRequest){
} else {
}
}
//
stage('Postprocess') {
/*/
if (!pullRequest){
try {
if (POSTPROCESS_JOB) {
build job: "${POSTPROCESS_JOB}", parameters: [
[$class: 'StringParameterValue', name: 'BUILD_VERSION', value: "${version}"]
]
}
} catch(e){}
}
/*/
}
//
} catch (e) {
//
//sendEmailNotification('BUILD FAILED', lastCommitAuthorEmail)
throw e
//
}
}