forked from politie/pipeline-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsPipelineSteps.groovy
202 lines (163 loc) · 7.21 KB
/
JenkinsPipelineSteps.groovy
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package politie.jenkins
import politie.jenkins.Constants
import groovy.json.JsonSlurper
// Constructor, called from PipelineBuilder.initialize().
void initialize() {
echo 'Initializing PipelineSteps.'
}
void cleanWorkspace() {
sh "echo 'Cleaning workspace'"
deleteDir()
}
void checkout(String project, String branch) {
checkout changelog: true, poll: true, scm: [
$class : 'GitSCM',
branches : [[name: '*/' + branch]],
browser : [$class: 'GitLab', repoUrl: Constants.GITLAB_WEB_BASE_URL + project , version: Constants.GITLAB_VERSION ],
userRemoteConfigs: [[credentialsId: Constants.GITLAB_CREDENTIALS_ID, url: Constants.GITLAB_CHECKOUT_BASE_URL + project + '.git' ]]
]
}
String determineVersion() {
return Constants.MAJOR_VERSION_NUMBER + '.' + env.BUILD_NUMBER;
}
String getBranch() {
String branchName;
// When Gitlab triggers the build, we can read the source branch from gitlab.
if (env.gitlabSourceBranch != null) {
branchName = env.gitlabSourceBranch
echo 'Gitlab source branch: ' + branchName
} else {
sh "git show-ref | grep `git rev-parse HEAD` | grep remotes | grep -v HEAD | sed -e 's/.*remotes.origin.//' > branch.txt"
branchName = readFile('branch.txt').trim()
}
echo 'Building branch \'' + branchName + '\'.'
return branchName;
}
String getCommitId() {
String commitId = sh(returnStdout: true, script: 'git rev-parse HEAD').trim()
return commitId;
}
void setBuildName(String version, String branch) {
currentBuild.displayName = version
if (branch != 'master') {
currentBuild.displayName = "# ${env.BUILD_NUMBER} - branch: ${branch} "
}
}
void setBuildProperties() {
// disabled because this doesn't work (yet with gitlab triggers)
// properties([pipelineTriggers([]),
// buildDiscarder(logRotator(artifactDaysToKeepStr: '', artifactNumToKeepStr: '', daysToKeepStr: '', numToKeepStr: '8')),
// [$class: 'GitLabConnectionProperty', gitLabConnection: '']
// ])
}
void updateApplicationVersionMaven(String version) {
sh "echo \'\ninfo.build.version=\'$version >> \$(find . -name application.properties | grep -E -v 'target|src/test') || true"
sh "mvn -B versions:set -DnewVersion=$version"
}
void runMavenBuild(boolean enableJacoco) {
// Apache Maven related side notes:
// --batch-mode : recommended in CI to inform maven to not run in interactive mode (less logs)
// -V : strongly recommended in CI, will display the JDK and Maven versions in use.
// Very useful to be quickly sure the selected versions were the ones you think.
// -U : force maven to update snapshots each time (default : once an hour, makes no sense in CI).
// -Dsurefire.useFile=false : useful in CI. Displays test errors in the logs directly (instead of
// having to crawl the workspace files to see the cause).
if (enableJacoco) {
sh '''
export CEPH_CONF=$PWD/src/test/resources/ceph.conf
mvn -B -V -U -e clean org.jacoco:jacoco-maven-plugin:prepare-agent verify -Dsurefire.useFile=false
'''
} else {
sh '''
export CEPH_CONF=$PWD/src/test/resources/ceph.conf
mvn -B -V -U -e clean verify -Dsurefire.useFile=false
'''
}
archiveTestResults()
}
void archiveTestResults() {
step([$class: 'JUnitResultArchiver', testResults: '**/target/**/TEST*.xml', allowEmptyResults: true])
}
void runSonarAnalysis() {
//println 'Sonar analysis temporarily disabled';
println 'Running Sonar analysis';
sh "mvn -B -V -U -e org.sonarsource.scanner.maven:sonar-maven-plugin:3.2:sonar -Dsonar.host.url='${Constants.SONAR_URL}'"
}
void deployToNexus() {
println 'Deploying to Nexus';
sh "mvn -B -V -U -e clean source:jar javadoc:jar deploy -DskipTests"
}
void deployApplicationWithConfigToDevEnv(String serviceName, String version, String applicationType) {
build job: 'deploy-application-with-config', parameters: [
[$class: 'StringParameterValue', name: 'APPLICATION', value: serviceName],
[$class: 'StringParameterValue', name: 'VERSION', value: version],
[$class: 'StringParameterValue', name: 'APPLICATION_TYPE', value: applicationType]
], wait: true
}
void deploySparkJobToDevEnv(String serviceName, String version) {
build job: 'deploy-spark-job', parameters: [
[$class: 'StringParameterValue', name: 'APPLICATION', value: serviceName],
[$class: 'StringParameterValue', name: 'VERSION', value: version]
], wait: true
}
def getUrlForEnvironment(String application, String tenant, String environment) {
return "https://${application}.${environment}.${tenant}" + Constants.ENVIRONMENT_DOMAIN_NAME
}
void checkDeployedVersion(String application, String version, String tenant) {
def url = getUrlForEnvironment(application, tenant, 'dev') + '/info'
sh """
sleep 5s
echo "Checking health of ${application} version ${version} at ${url}."
output=\$( curl --insecure ${url} 2>/dev/null || true )
echo "Healthcheck output: \${output}"
versionCheck=\$( echo \$output | grep ${version} || true )
if [ -z "\$versionCheck" ]
then
echo "Version ${version} NOT found, deployment failed?"
else
echo "Version ${version} found, deployment succeeded."
fi
"""
}
@NonCPS
def parseJsonText(String jsonText) {
final slurper = new JsonSlurper()
return slurper.parseText(jsonText)
}
@NonCPS
def gitlab_getProjectDetails(token, projectName) {
// Fetch project by project name
def response = httpRequest customHeaders: [[name: 'PRIVATE-TOKEN', value: token ]],
url: Constants.GITLAB_API_BASE_URL + '/projects/development%2F' + projectName
return response
}
@NonCPS
def gitlab_performTag(token, projectId, commitHash, tagName) {
// Create POST body
def postBody = """
{
"id": "$projectId",
"tag_name": "$tagName",
"ref": "$commitHash"
}
"""
def tagUrl = Constants.GITLAB_API_BASE_URL + "/projects/${projectId}/repository/tags"
// Perform tag
def response = httpRequest contentType: 'APPLICATION_JSON', httpMode: 'POST', requestBody: postBody,
customHeaders: [[name: 'PRIVATE-TOKEN', value: token ]], url: tagUrl
return response
}
def tagCommit(project, commitHash, tag) {
withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: Constants.GITLAB_API_TOKEN_CREDENTIALS_ID,
usernameVariable: 'USERNAME', passwordVariable: 'token']]) {
// Call Gitlab API to get project details, read project id
def getProjectResponse = gitlab_getProjectDetails(token, project)
def projectId = parseJsonText(getProjectResponse.content).id
// Call gitlab API to place a tag on the specific project and commithash
def performTagResponse = gitlab_performTag(token, projectId, commitHash, tag)
def tagCreated = parseJsonText(performTagResponse.content)
println 'Tagged commit ' + tagCreated.commit.id + ' with tag "' + tagCreated.name + '".'
}
}
// Return the contents of this script as object so it can be re-used in Jenkinsfiles.
return this;