This repository has been archived by the owner on Nov 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathJenkinsfile
161 lines (144 loc) · 6.45 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
/*
* Copyright:: Copyright (c) 2017-present Sonatype, Inc. Apache License, Version 2.0.
*/
@Library(['ci-pipeline-library', 'jenkins-shared', 'int-jenkins-shared']) _
import com.sonatype.jenkins.pipeline.GitHub
import com.sonatype.jenkins.pipeline.OsTools
properties([
parameters([
string(defaultValue: '', description: 'New Nexus IQ Version', name: 'nexus_iq_version'),
string(defaultValue: '', description: 'New Nexus IQ Version Sha256', name: 'nexus_iq_version_sha'),
string(defaultValue: '20', description: 'Kitchen concurrency', name: 'KITCHEN_TEST_CONCURRENCY'),
string(defaultValue: '', description: 'Kitchen additional parameters', name: 'KITCHEN_TEST_PARAMS')
])
])
node('ubuntu-chef-zion') {
def commitId, version, imageId, apiToken, branch, defaultsFileLocation
def organization = 'sonatype',
repository = 'chef-nexus-iq-server',
credentialsId = 'integrations-github-api',
archiveName = 'chef-nexus-iq-server.tar.gz',
cookbookName = 'nexus_iq_server'
GitHub gitHub
try {
stage('Preparation') {
deleteDir()
def checkoutDetails = checkout scm
branch = checkoutDetails.GIT_BRANCH == 'origin/master' ? 'master' : checkoutDetails.GIT_BRANCH
commitId = checkoutDetails.GIT_COMMIT
version = getVersion()
setBuildDisplayName(Version: version)
OsTools.runSafe(this, 'git config --global user.email [email protected]')
OsTools.runSafe(this, 'git config --global user.name Sonatype CI')
withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: credentialsId,
usernameVariable: 'GITHUB_API_USERNAME', passwordVariable: 'GITHUB_API_PASSWORD']]) {
apiToken = env.GITHUB_API_PASSWORD
}
gitHub = new GitHub(this, "${organization}/${repository}", apiToken)
}
if (params.nexus_iq_version && params.nexus_iq_version_sha) {
stage('Update IQ Version') {
OsTools.runSafe(this, "git checkout ${branch}")
defaultsFileLocation = "${pwd()}/attributes/default.rb"
def defaultsFile = readFile(file: defaultsFileLocation)
def versionRegex = /(default\['nexus_iq_server'\]\['version'\] = ')(\d\.\d{1,3}\.\d\-\d{2})(')/
def shaRegex = /(default\['nexus_iq_server'\]\['checksum'\] = ')([A-Fa-f0-9]{64})(')/
defaultsFile = defaultsFile.replaceAll(versionRegex, "\$1${params.nexus_iq_version}\$3")
defaultsFile = defaultsFile.replaceAll(shaRegex, "\$1${params.nexus_iq_version_sha}\$3")
writeFile(file: defaultsFileLocation, text: defaultsFile)
}
}
stage('Build') {
gitHub.statusUpdate commitId, 'pending', 'build', 'Build is running'
def gemInstallDirectory = getGemInstallDirectory()
withEnv(["PATH+GEMS=${gemInstallDirectory}/bin"]) {
OsTools.runSafe(this, 'berks package')
dir('build/target') {
OsTools.runSafe(this, "mv ${WORKSPACE}/cookbooks-*.tar.gz ${archiveName}")
}
}
if (currentBuild.result == 'FAILURE') {
gitHub.statusUpdate commitId, 'failure', 'build', 'Build failed'
return
} else {
gitHub.statusUpdate commitId, 'success', 'build', 'Build succeeded'
}
}
stage('Test') {
gitHub.statusUpdate commitId, 'pending', 'test', 'Tests are running'
dir('build/target') {
OsTools.runSafe(this, "tar -zxvf ${archiveName}")
}
OsTools.runSafe(this, 'chef gem install kitchen-docker')
sh 'kitchen test -c ${KITCHEN_TEST_CONCURRENCY} -d always --no-color ${KITCHEN_TEST_PARAMS}'
if (currentBuild.result == 'FAILURE') {
gitHub.statusUpdate commitId, 'failure', 'test', 'Tests failed'
return
} else {
gitHub.statusUpdate commitId, 'success', 'test', 'Tests succeeded'
}
}
if (currentBuild.result == 'FAILURE') {
return
}
if (params.nexus_iq_version && params.nexus_iq_version_sha) {
stage('Commit IQ Version Update') {
withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'integrations-github-api',
usernameVariable: 'GITHUB_API_USERNAME', passwordVariable: 'GITHUB_API_PASSWORD']]) {
OsTools.runSafe(this, """
git add ${defaultsFileLocation}
git commit -m 'Update IQ Server to ${params.nexus_iq_version}'
git push https://${env.GITHUB_API_USERNAME}:${env.GITHUB_API_PASSWORD}@github.com/${organization}/${repository}.git ${branch}
""")
version = getVersion()
setBuildDisplayName(Version: version)
}
}
}
stage('Archive') {
dir('build/target') {
archiveArtifacts artifacts: "${archiveName}", onlyIfSuccessful: true
}
}
if (branch != 'master') {
return
}
stage('Push tags') {
withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: credentialsId,
usernameVariable: 'GITHUB_API_USERNAME', passwordVariable: 'GITHUB_API_PASSWORD']]) {
OsTools.runSafe(this, "git tag \"release-${version}\"")
OsTools.runSafe(this, """
git push \
https://${env.GITHUB_API_USERNAME}:${env.GITHUB_API_PASSWORD}@github.com/${organization}/${repository}.git \
release-${version}
""")
}
}
stage('Create release') {
response = httpRequest customHeaders: [[name: 'Authorization', value: "token ${apiToken}"]],
acceptType: 'APPLICATION_JSON', contentType: 'APPLICATION_JSON', httpMode: 'POST',
requestBody: "{\"tag_name\": \"release-${version}\"}",
url: "https://api.github.com/repos/${organization}/${repository}/releases"
def release = readJSON text: response.content
def releaseId = release.id
response = OsTools.runSafe(this, """
curl -H "Authorization: token ${apiToken}" \
-H "Accept: application/vnd.github.manifold-preview" \
-H "Content-Type: application/gzip" \
--data-binary @build/target/${archiveName} \
"https://uploads.github.com/repos/${organization}/${repository}/releases/${releaseId}/assets?name=${archiveName}"
""")
}
} finally {
OsTools.runSafe(this, 'git clean -f && git reset --hard origin/master')
}
}
def getGemInstallDirectory() {
def content = OsTools.runSafe(this, "gem env")
for (line in content.split('\n')) {
if (line.startsWith(' - USER INSTALLATION DIRECTORY: ')) {
return line.substring(33)
}
}
error 'Could not determine user gem install directory.'
}