-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathJenkinsfile
221 lines (161 loc) · 5.93 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
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
conventionalChangelogConventionalCommitsVersion = 'v4.5.0'
semanticReleaseVersion = 'v17.3.1'
def getRepo() {
checkout([$class: 'GitSCM',
branches: scm.branches,
browser: scm.browser,
doGenerateSubmoduleConfigurations: false,
extensions: [
[$class: 'RelativeTargetDirectory', relativeTargetDir: 'repo'],
[$class: 'PruneStaleBranch'],
[$class: 'CleanCheckout']
],
submoduleCfg: scm.submoduleCfg,
userRemoteConfigs: scm.userRemoteConfigs
]).GIT_COMMIT
}
def getNextSemanticReleaseVersion(semanticReleaseOutput) {
def nextReleasePattern = /(?ms).*The next release version is (?<release>.*?)$.*/
def firstReleasePattern = /(?ms).*There is no previous release, the next release version is (?<release>.*?)$.*/
def version = ''
[nextReleasePattern, firstReleasePattern].each { x ->
if (version == '') {
def releaseMatch = semanticReleaseOutput =~ x
if (releaseMatch.matches()) {
version = releaseMatch.group('release')
}
}
}
version
}
def runSemanticRelease(token, preview) {
def dryRun = ''
if (preview) {
dryRun = '--dry-run'
}
def output = sh(returnStdout: true, script: "export GITHUB_TOKEN=$token; npm install --save-dev conventional-changelog-conventionalcommits@$conventionalChangelogConventionalCommitsVersion; npx semantic-release@$semanticReleaseVersion $dryRun")
echo output
getNextSemanticReleaseVersion(output)
}
def getLatestGitHubRelease(token, owner, repo) {
def latestUrl = "https://api.github.com/repos/$owner/$repo/releases/latest"
def output = sh(returnStdout: true, script: "curl --silent -H 'Accept: application/vnd.github.v3+json' -H 'Authorization: token $token' $latestUrl")
echo output
def tagNamePattern = /(?ms).*"tag_name":\s"(?<release>[^"]+)".*/
def tagNameMatch = output =~ tagNamePattern
def version = ''
if (tagNameMatch.matches()) {
version = tagNameMatch.group('release')
}
version
}
pipeline {
options {
skipDefaultCheckout true // checkout via getRepo()
}
agent none
stages {
stage('Release') {
agent {
label 'codedx-release-build-small'
}
stages {
stage('Checkout') {
steps {
script {
currentBuild.displayName = getRepo()
}
}
}
stage('Test') {
steps {
dir ('repo') {
// note: the -CI parameter sets Run.Exit, but it also creates two files in the working directory
sh 'pwsh -command "&{ Import-Module Pester; \\$cfg = [PesterConfiguration]::Default; \\$cfg.Run.Exit = \\$true; \\$cfg.Run.Path = \'./.version/test/version.tests.ps1\'; Invoke-Pester -Configuration \\$cfg }"'
}
}
}
stage('Get Versions') {
steps {
dir ('repo') {
withCredentials([
usernamePassword(credentialsId: 'codedx-build-github', usernameVariable: 'GIT_USERNAME', passwordVariable: 'GIT_TOKEN'),
string(credentialsId: 'codedxownername', variable: 'GIT_OWNER'),
string(credentialsId: 'codedxreponame', variable: 'GIT_CODEDX_REPO'),
string(credentialsId: 'mariadbreponame', variable: 'GIT_MARIADB_REPO')
]) {
script {
currentVersions = []
[GIT_CODEDX_REPO,GIT_MARIADB_REPO].each { x ->
currentVersion = getLatestGitHubRelease(GIT_TOKEN, GIT_OWNER, x)
if (currentVersion == '') {
error("unable to continue because the latest version from repo $x cannot be found")
}
currentVersions += currentVersion
}
codeDxVersion = currentVersions[0]
mariaDBVersion = currentVersions[1]
def isCurrentVersion = sh(returnStdout: true, script: "pwsh -command \"&{ . ./.version/common.ps1; Test-CodeDxVersion './docker-compose.yml' '$codeDxVersion' '$mariaDBVersion' }\"")
if (isCurrentVersion.toBoolean()) {
error("unable to continue because the repository already references the latest versions; codeDxVersion=$codeDxVersion; mariaDBVersion=$mariaDBVersion;")
}
}
}
}
}
}
stage('Confirm') {
steps {
milestone ordinal: 1, label: 'Confirm'
script {
try {
timeout(time: 15) {
// pipeline not triggered by SCM and input response should occur with minimal delay, so invoke input in this stage (leaving container running)
input message: "Continue with latest Code Dx version ($codeDxVersion) and MariaDB version ($mariaDBVersion)?"
}
} catch (err) {
if (err instanceof org.jenkinsci.plugins.workflow.steps.FlowInterruptedException) {
error('Timeout occurred while awaiting release confirmation')
}
error(err.toString())
}
}
}
}
stage('Update Version') {
steps {
milestone ordinal: 2, label: 'Confirmed'
dir ('repo') {
sh 'git config user.name \'Code Dx Build\' && git config user.email support@codedx.com'
sh "git checkout ${scm.branches[0]}"
sh "pwsh ./.version/version.ps1 '$codeDxVersion' '$mariaDBVersion'"
sh 'git add .'
sh "git commit -m 'feat: update to $codeDxVersion and $mariaDBVersion'"
withCredentials([usernamePassword(credentialsId: 'codedx-build-github', usernameVariable: 'GIT_USERNAME', passwordVariable: 'GIT_TOKEN')]){
// note: pipeline requires 'Suppress automatic SCM triggering' behavior
sh('''
git config --local credential.helper "!helper() { echo username=\\$GIT_USERNAME; echo password=\\$GIT_TOKEN; }; helper"
git push
''')
}
}
}
}
stage('Create Release') {
steps {
dir ('repo') {
withCredentials([usernamePassword(credentialsId: 'codedx-build-github', usernameVariable: 'GIT_USERNAME', passwordVariable: 'GIT_TOKEN')]) {
script {
versionReleased = runSemanticRelease(GIT_TOKEN, false)
if (versionReleased == '') {
error('Build failed because commits since the last version do not require a new release')
}
}
}
}
}
}
}
}
}
}