-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
144 lines (132 loc) · 4.53 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
/*
* SPDX-FileCopyrightText: 2021 Zextras <https://www.zextras.com>
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
String getRepositoryName() {
return sh(script: '''#!/bin/bash
git remote -v | head -n1 | cut -d$'\t' -f2 | cut -d' ' -f1 | sed -e 's!https://github.com/!!g' -e 's!git@github.com:!!g' -e 's!.git!!g'
''', returnStdout: true).trim()
}
String getLastTag() {
return sh(script: '''#!/bin/bash
git describe --tags --abbrev=0
''', returnStdout: true).trim()
}
Boolean tagExistsAtHead() {
try {
sh(script: '''#!/bin/bash
git describe --tags --exact-match
''', returnStdout: true)
return true
} catch (err) {
return false
}
}
// node utils
def nodeCmd(String cmd) {
sh '. load_nvm && nvm install && nvm use && npm ci && ' + cmd
}
void npmLogin(String npmAuthToken) {
if (!fileExists(file: '.npmrc')) {
sh(
script: """
touch .npmrc;
echo "//registry.npmjs.org/:_authToken=${npmAuthToken}" > .npmrc
""",
returnStdout: false
)
}
}
// FLAGS
Boolean isPullRequest
Boolean isReleaseBranch
pipeline {
agent {
node {
label "nodejs-agent-v4"
}
}
options {
timeout(time: 20, unit: "MINUTES")
buildDiscarder(logRotator(numToKeepStr: "50"))
}
post {
always {
script {
def commitEmail = sh(
script: "git --no-pager show -s --format='%ae'",
returnStdout: true
).trim()
emailext(
attachLog: true,
body: "\$DEFAULT_CONTENT",
recipientProviders: [requestor()],
subject: "\$DEFAULT_SUBJECT",
to: "${commitEmail}"
)
}
}
}
stages {
stage("Read settings") {
steps {
script {
isPullRequest = "${BRANCH_NAME}" ==~ /PR-\d+/
echo "isPullRequest: ${isPullRequest}"
isReleaseBranch = "${BRANCH_NAME}" ==~ /release/
echo "isReleaseBranch: ${isReleaseBranch}"
}
}
}
// ============================================ Release Automation ==============================================
stage("Release") {
when {
beforeAgent true
allOf {
expression { isPullRequest == false }
}
}
steps {
script {
withCredentials([usernamePassword(credentialsId: 'npm-zextras-bot-auth-token', usernameVariable: 'AUTH_USERNAME', passwordVariable: 'NPM_TOKEN')]) {
withCredentials([usernamePassword(credentialsId: 'tarsier-bot-pr-token-github', usernameVariable: 'GH_USERNAME', passwordVariable: 'GH_TOKEN')]) {
nodeCmd("npx semantic-release")
}
}
}
}
}
stage('Open release to devel pull request') {
when {
beforeAgent true
allOf {
expression { isReleaseBranch == true }
expression { tagExistsAtHead() == true }
}
}
steps {
script {
String versionBumperBranchName = "version-bumper/${getLastTag()}"
sh(script: """#!/bin/bash
git push origin HEAD:refs/heads/${versionBumperBranchName}
""")
withCredentials([usernamePassword(credentialsId: 'tarsier-bot-pr-token-github', usernameVariable: 'GH_USERNAME', passwordVariable: 'GH_TOKEN')]) {
sh(script: """
curl https://api.github.com/repos/${getRepositoryName()}/pulls \
-X POST \
-H 'Accept: application/vnd.github.v3+json' \
-H 'Authorization: token ${GH_TOKEN}' \
-d '{
\"title\": \"chore(release): ${getLastTag()}\",
\"head\": \"${versionBumperBranchName}\",
\"base\": \"devel\",
\"maintainer_can_modify\": true
}'
""")
}
}
}
}
}
}