-
Notifications
You must be signed in to change notification settings - Fork 11
/
JenkinsfileRelease
173 lines (145 loc) · 5.01 KB
/
JenkinsfileRelease
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
pipeline {
parameters {
gitParameter name: 'branch',
description: 'The branch to check out',
branchFilter: 'origin/(.*)',
defaultValue: 'master',
selectedValue: 'DEFAULT',
type: 'PT_BRANCH',
listSize: '0',
quickFilterEnabled: true
string name: 'ReleaseVersion',
description: 'Version to release',
defaultValue: ''
text name: 'ReleaseDescription',
description: 'Release description',
defaultValue: ''
booleanParam name: 'PreRelease',
description: 'This is a pre-release. Will not publish to npm if selected ',
defaultValue: false
string name: 'auth',
description: 'Jenkins credential ID'
}
agent {
label 'graphdb-jenkins-node'
}
options {
disableConcurrentBuilds()
timeout(time: 15, unit: 'MINUTES')
timestamps()
}
tools {
nodejs 'nodejs-14.17.0'
}
environment {
CI = "true"
NPM_TOKEN = credentials('npm-token')
API_URL = "https://api.github.com/repos/Ontotext-AD/graphdb.js"
AUTH = credentials("${auth}")
}
stages {
stage ('Prepare') {
steps {
// show node version
sh "node --version"
// Switch to branch
sh "git checkout ${branch}"
// Change versions
sh "npm version --git-tag-version=false --allow-same-version=true ${ReleaseVersion}"
dir("test-e2e/") {
sh "npm version --git-tag-version=false --allow-same-version=true ${ReleaseVersion}"
}
// Install
sh "npm ci"
// Build
sh "npm run build"
}
}
stage ('Publish') {
steps {
// Publish on npm
script {
if (params.PreRelease == false) {
sh "echo //registry.npmjs.org/:_authToken=${NPM_TOKEN} > .npmrc && npm publish"
}
}
}
}
}
post {
success {
// Commit, tag and push the changes in Git
sh "git commit -a -m 'Release ${ReleaseVersion}'"
sh "git tag -a v${ReleaseVersion} -m 'Release v${ReleaseVersion}'"
sh "git push --set-upstream origin ${branch} && 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))
echo result
}
}
failure {
wrap([$class: 'BuildUser']) {
emailext(
to: env.BUILD_USER_EMAIL,
from: "Jenkins <hudson@ontotext.com>",
subject: '''[Jenkins] $PROJECT_NAME - Build #$BUILD_NUMBER - $BUILD_STATUS!''',
mimeType: 'text/html',
body: '''${SCRIPT, template="groovy-html.template"}'''
)
}
}
always {
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\" ${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\" ${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.ReleaseDescription)
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) {
return sh(script: "curl -X POST -H \"Accept: application/vnd.github.v3+json\" -H \"Authorization: token ${AUTH}\" --data '{\"tag_name\": \"v${ReleaseVersion}\", \"target_commitish\": \"${branch}\", \"name\": \"v${ReleaseVersion}\", \"body\": \"${desc}\", \"draft\": false, \"prerelease\": ${PreRelease}}' ${API_URL}/releases" , 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
}