-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathrelease.Jenkinsfile
More file actions
224 lines (192 loc) · 6.75 KB
/
release.Jenkinsfile
File metadata and controls
224 lines (192 loc) · 6.75 KB
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
@Library('ontotext-platform@v0.1.51') _
import groovy.json.JsonOutput
pipeline {
parameters {
gitParameter name: 'GIT_BRANCH',
description: 'The branch to check out',
branchFilter: 'origin/(.*)',
defaultValue: 'master',
selectedValue: 'DEFAULT',
type: 'PT_BRANCH',
listSize: '0',
quickFilterEnabled: true
string name: 'RELEASE_VERSION',
description: 'Version to release',
defaultValue: ''
text name: 'RELEASE_DESCRIPTION',
description: 'Release description',
defaultValue: ''
booleanParam name: 'PRE_RELEASE',
description: 'This is a pre-release. Will not publish to npm if selected',
defaultValue: false
booleanParam(
name: 'NOTIFY_SLACK',
defaultValue: false,
description: 'Send Slack notification after successful build'
)
string(
name: 'SLACK_CHANNEL',
defaultValue: '#frontend-notifications',
description: 'Slack channel for notification (only used if checkbox above is selected)'
)
}
agent {
label 'aws-small'
}
options {
disableConcurrentBuilds()
timeout(time: 15, unit: 'MINUTES')
timestamps()
}
tools {
nodejs 'nodejs-18.9.0'
}
environment {
API_URL = "https://api.github.com/repos/Ontotext-AD/graphdb.js"
NPM_CONFIG_REGISTRY = 'https://registry.npmjs.org/'
}
stages {
stage ('Prepare') {
steps {
script {
git_cmd.checkout(branch: params.GIT_BRANCH)
npm.prepareRelease(version: params.RELEASE_VERSION, scripts: ['build'])
npm.prepareRelease(version: params.RELEASE_VERSION, dir: "test-e2e/")
}
}
}
stage ('Publish') {
steps {
withKsm(
application: [
[
credentialsId: 'ksm-jenkins',
secrets: [
[destination: 'env', envVar: 'NPM_TOKEN', filePath: '', notation: 'keeper://FcbEgbi287PN2yx_3uCz4Q/field/note']
]
]
]
) {
script {
if (params.PRE_RELEASE == false) {
sh "echo //registry.npmjs.org/:_authToken=${NPM_TOKEN} > .npmrc"
sh "npm publish"
}
}
}
}
}
}
post {
success {
dir("${env.WORKSPACE}") {
sh "rm -f .npmrc"
withKsm(application: [
[
credentialsId: 'ksm-jenkins',
secrets: [
[destination: 'env', envVar: 'GIT_USER', filePath: '', notation: 'keeper://8hm1g9HCfBPgoWAmpiHn6w/field/login'],
[destination: 'env', envVar: 'GIT_TOKEN', filePath: '', notation: 'keeper://BsykE_Za61bPoY8a1KwH_Q/field/note']
]
]
]) {
// Commit, tag and push the changes in Git
sh "git commit -a -m 'Release ${params.RELEASE_VERSION}'"
sh "git tag -a v${params.RELEASE_VERSION} -m 'Release v${params.RELEASE_VERSION}'"
sh 'mkdir -p ~/.ssh'
sh 'ssh-keyscan github.com >> ~/.ssh/known_hosts'
sh 'git config --global user.name "$GIT_USER"'
sh 'git config --global user.email "$GIT_USER@users.noreply.github.com"'
sh 'git remote set-url origin git@github.com:Ontotext-AD/graphdb.js.git'
sh "git push --set-upstream origin '${params.GIT_BRANCH}'"
sh "git push --tags"
script {
def latest = getLatestReleaseTagName()
echo "Last revision ${latest}"
def gitMessages = getReleaseMessagesFromGit(latest)
echo "Recent merge commit messages collected"
def result = postRelease(composeReleaseMessage(gitMessages), "$GIT_TOKEN")
echo result
}
}
}
// Optional Slack notification if enabled and channel is provided
if (params.NOTIFY_SLACK && params.SLACK_CHANNEL?.trim()) {
try {
slack.notifyResult(channel: params.SLACK_CHANNEL, color:'good', message:"Released graphdb-workbench v${params.RELEASE_VERSION}")
} catch (e) {
echo "Slack notification failed: ${e.getMessage()}"
}
}
}
failure {
dir("${env.WORKSPACE}") {
wrap([$class: 'BuildUser']) {
sendMail(env.BUILD_USER_EMAIL)
}
}
}
always {
dir("${env.WORKSPACE}") {
sh "rm -f .npmrc"
}
}
}
}
// Latest revision tag name getter
def getLatestReleaseTagName() {
def latest = readJSON text: sh(script: 'curl -H "Accept: application/vnd.github.v3+json" -H "Authorization: Bearer $GIT_TOKEN" $API_URL/releases/latest', returnStdout: true)
return latest.tag_name
}
// Merge commit messages getter
// Returns commit messages between given commit tag and master
def getReleaseMessagesFromGit(String latest) {
def response = sh(script: "curl -H \"Accept: application/vnd.github.v3+json\" -H \"Authorization: Bearer $GIT_TOKEN\" ${env.API_URL}/compare/${latest}...master", returnStdout: true)
def resp = readJSON text: response
def commits = resp.commits
def message = ""
def matcher = "Merge pull request #"
for(commit in commits) {
if(commit.commit.message != null && commit.commit.message.startsWith(matcher)) {
// Remove unnecessary repetitive merge descriptions
def commitMessage = commit.commit.message.substring(matcher.length() - 1)
message += newlineToHtml("* ${commitMessage}")
}
}
return message
}
// Composes final release message from jenkins build configuration, github commit messages and environment variables
def composeReleaseMessage(String gitMessages) {
def message = ""
def releaseDescription = newlineToHtml(params.RELEASE_DESCRIPTION)
wrap([$class: 'BuildUser']) {
message = "${releaseDescription} <br/> ${gitMessages} <br/> Released on ${new Date().format('yyyy/MM/dd HH:mm', TimeZone.getTimeZone('UTC'))} UTC by ${env.BUILD_USER}"
}
return message
}
// Post release to github
// returns response from the operation
def postRelease(String desc, String auth) {
def body = [
tag_name: "v${params.RELEASE_VERSION}",
target_commitish: "${params.GIT_BRANCH}",
name: "v${params.RELEASE_VERSION}",
body: desc,
draft: false,
prerelease: params.PRE_RELEASE
]
def json = JsonOutput.toJson(body)
writeFile file: 'release.json', text: json
def curlCommand = "curl -X POST -H \"Accept: application/vnd.github.v3+json\" -H \"Authorization: Bearer ${auth}\" --data @release.json ${API_URL}/releases"
return sh(script: curlCommand, returnStdout: true)
}
// New line symbol to html br tag converter.
def newlineToHtml(String desc) {
def description = ""
def lines = desc.tokenize("\n")
for (line in lines) {
description += line
description += "<br/>"
}
return description
}