/
Jenkinsfile
371 lines (351 loc) · 12.4 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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#!groovy
@Library('realm-ci') _
pipeline {
agent {
label 'osx'
}
options {
// Prevent checking out multiple times
skipDefaultCheckout()
}
environment {
// Parameters used by the github releases script
GITHUB_OWNER='realm'
GITHUB_REPO='realm-studio'
NODE_VERSION='10'
}
parameters {
booleanParam(
name: 'PREPARE',
defaultValue: false,
description: '''
<p>Prepare for publishing:</p>
<ol>
<li>Changes version based on release notes.</li>
<li>Copies release notes to changelog.</li>
<li>Restores the release notes from a template.</li>
<li>Commits the changes to a branch and pushes it to GitHub.</li>
<li>Creates a pull-request from the branch into master.</li>
</ol>
''',
)
booleanParam(
name: 'PACKAGE',
defaultValue: false,
description: '''
<p>Produce packaged artifacts for all supported platforms.</p>
<p>NOTE: PRs jobs don't get packaged by default, rebuild with this enabled to produce packages.</p>
''',
)
}
stages {
stage('Checkout') {
steps {
checkout([
$class: 'GitSCM',
branches: scm.branches,
extensions: scm.extensions + [
[$class: 'WipeWorkspace'],
[$class: 'CleanCheckout'],
[$class: 'LocalBranch']
],
userRemoteConfigs: [[
credentialsId: 'realm-ci-ssh',
name: 'origin',
url: "git@github.com:${GITHUB_OWNER}/${GITHUB_REPO}.git"
]]
])
script {
// Read the current version of the package
packageJson = readJSON file: 'package.json'
env.VERSION = "v${packageJson.version}"
// Determine if this commit changes the version, if so:
previousVersion = sh(
script: 'git show HEAD~1:package.json | jq -r .version',
returnStdout: true,
).trim()
// Did the version change?
if (BRANCH_NAME == 'master' && previousVersion != packageJson.version) {
sh "git tag -a ${VERSION} -m 'Release ${packageJson.version}'"
// Push to GitHub with tags
sshagent(['realm-ci-ssh']) {
sh 'git push origin --tags'
}
}
// Determine what tags are pointing at the current commit
def tagName = sh(
script: 'git tag --points-at HEAD',
returnStdout: true,
).trim()
// Publish if the tag starts with a "v"
if (tagName && tagName.startsWith('v')) {
// Assert that the tag matches the version in the package.json
assert "v${packageJson.version}" == tagName : "Tag doesn't match package.json version"
// Package and publish when building a version tag
env.PUBLISH = 'true'
env.PACKAGE = 'true'
} else {
env.PUBLISH = 'false'
}
}
}
}
stage('Install & update version') {
steps {
nvm(env.NODE_VERSION) {
// Install dependencies
sh 'npm ci'
// Update the version
script {
if (PUBLISH == 'true') {
// Update the build display name
currentBuild.displayName += ": ${VERSION} (publish)"
} else {
// Determine the upcoming release type
nextVersionType = sh(
script: "node ./scripts/next-version.js",
returnStdout: true,
).trim()
// Bump the version accordingly
env.NEXT_VERSION = sh(
script: "npm version ${nextVersionType} --no-git-tag-version",
returnStdout: true,
).trim()
// If we're preparing, add this to the display name .. alternatively add a pre-release id
if (env.PREPARE == 'true') {
// Set the build display name
currentBuild.displayName += ": ${NEXT_VERSION} (prepare)"
} else {
// Update the version of the package again, this time prepending pre release id
env.NEXT_VERSION = sh(
script: "npm version ${NEXT_VERSION}-${JOB_BASE_NAME}-${BUILD_NUMBER} --no-git-tag-version",
returnStdout: true,
).trim()
// Set the build display name
currentBuild.displayName += ": ${NEXT_VERSION}"
}
}
}
}
}
}
stage('Build, lint & check') {
when {
// Don't do this when preparing for a release
not { environment name: 'PREPARE', value: 'true' }
}
parallel {
stage('Build') {
steps {
nvm(env.NODE_VERSION) {
sh 'npm run build'
}
}
}
stage('Lint TypeScript') {
steps {
nvm(env.NODE_VERSION) {
sh 'npm run lint:ts'
}
}
}
stage('Lint SASS') {
steps {
nvm(env.NODE_VERSION) {
sh 'npm run lint:sass'
}
}
}
stage('Check package-lock') {
steps {
nvm(env.NODE_VERSION) {
sh 'npm run check:package-lock'
}
}
}
}
}
stage('Pre-package tests') {
steps {
// Run the tests with the JUnit reporter
nvm(env.NODE_VERSION) {
sh 'MOCHA_FILE=pre-test-results.xml npm test -- --reporter mocha-junit-reporter'
}
}
post {
always {
// Archive the test results
junit(
allowEmptyResults: true,
keepLongStdio: true,
testResults: 'pre-test-results.xml'
)
// Archive any screenshots emitted by failing tests
archiveArtifacts(
artifacts: 'failure-*.png',
allowEmptyArchive: true,
)
}
}
}
stage('Package') {
when {
environment name: 'PACKAGE', value: 'true'
}
stages {
stage("Electron build") {
steps {
// Run the electron builder
nvm(env.NODE_VERSION) {
withCredentials([
file(credentialsId: 'cose-sign-certificate-windows', variable: 'WIN_CSC_LINK'),
string(credentialsId: 'cose-sign-password-windows', variable: 'WIN_CSC_KEY_PASSWORD')
]) {
sh 'npx build -mlw -c.forceCodeSigning --publish never'
}
// Archive the packaged artifacts
archiveArtifacts 'dist/*'
}
}
}
stage('Post-packaging tests') {
steps {
nvm(env.NODE_VERSION) {
// Run the tests with the JUnit reporter
sh 'MOCHA_FILE=post-test-results.xml npm run test:post-package -- --reporter mocha-junit-reporter'
}
}
post {
always {
// Archive the test results
junit(
allowEmptyResults: true,
keepLongStdio: true,
testResults: 'post-test-results.xml'
)
}
}
}
}
}
/*
* Publish the packaged artifacts:
* - Await user input to allow manual testing of the packaged artifacts.
* - Extract the latest release notes from the changelog.
* - Create a draft GitHub release.
* - Upload the packaged artifacts to the draft release.
* - Upload the packaged artifacts to S3.
* - Upload the auto-updating .yml files to S3.
* - Publish the GitHub release.
* - Announce the release on Slack.
*
* This stage runs when building a commit tagged with a version.
*/
stage('Publish') {
when {
environment name: 'PUBLISH', value: 'true'
}
steps {
nvm(env.NODE_VERSION) {
// Wait for input
input(message: "Ready to publish $VERSION?", id: 'publish')
// Extract release notes from the changelog
sh "node scripts/tools extract-release-notes ./RELEASENOTES.extracted.md"
// Handle GitHub release
withCredentials([
string(credentialsId: 'github-release-token', variable: 'GITHUB_TOKEN')
]) {
// Create a draft release on GitHub
sh "node scripts/github-releases create-draft $VERSION RELEASENOTES.extracted.md"
// Delete all the unpackaged directories
sh 'rm -rf dist/*/'
// Move yml files to another folder and upload them after other archives.
// This is to prevent clients from trying to upgrade before the files are there.
sh 'mkdir dist-finally && mv dist/*.yml dist-finally'
// Upload artifacts to GitHub
script {
for (file in findFiles(glob: 'dist/*')) {
sh "node scripts/github-releases upload-asset $VERSION '$file'"
}
}
// Upload the build artifacts to S3
script {
def s3Config = packageJson.build.publish[0]
dir('dist') {
rlmS3Put(bucket: s3Config.bucket, path: s3Config.path)
}
// Upload the json and yml files
dir('dist-finally') {
rlmS3Put(bucket: s3Config.bucket, path: s3Config.path)
}
}
// Publish the release
sh "node scripts/github-releases publish $VERSION"
}
// Post success message to Slack
script {
// Read in the extracted release notes
def releaseNotes = readFile "./RELEASENOTES.extracted.md"
def releaseUrl = "https://github.com/$GITHUB_OWNER/$GITHUB_REPO/releases/tag/$VERSION"
// Post to Slack
postToSlack('slack-releases-webhook', [[
'title': "Realm Studio $VERSION has been released!",
'title_link': releaseUrl,
'text': "Github Release and artifacts are available <${releaseUrl}|here>\n${releaseNotes}",
'mrkdwn_in': ['text'],
'color': 'good',
'unfurl_links': false
]])
}
}
}
}
stage('Prepare') {
when {
environment name: 'PREPARE', value: 'true'
}
environment {
PREPARED_BRANCH = "ci/prepared-${NEXT_VERSION}"
}
steps {
nvm(env.NODE_VERSION) {
// Append the RELEASENOTES to the CHANGELOG
sh "node scripts/tools copy-release-notes ${VERSION} ${NEXT_VERSION}"
// Restore RELEASENOTES.md from the template
sh 'cp docs/RELEASENOTES.template.md RELEASENOTES.md'
// Set the email and name used when committing
sh 'git config --global user.email "ci@realm.io"'
sh 'git config --global user.name "Jenkins CI"'
// Checkout a branch
sh "git checkout -b ${PREPARED_BRANCH}"
// Stage the updates to the files, commit and tag the commit
sh 'git add package.json package-lock.json CHANGELOG.md RELEASENOTES.md'
sh "git commit -m 'Prepare version ${NEXT_VERSION}'"
// Push to GitHub with tags
sshagent(['realm-ci-ssh']) {
sh "git push --set-upstream --tags --force origin ${PREPARED_BRANCH}"
}
// Create a pull-request
withCredentials([
string(credentialsId: 'github-release-token', variable: 'GITHUB_TOKEN')
]) {
script {
// Determine who started the build and should therefore be assigned the pull request.asSynchronized()
// This assumes they have the same Jenkins user ID as their GitHub handle
def assignee = currentBuild.rawBuild.getCause(hudson.model.Cause$UserIdCause).getUserId()
// Create a draft release on GitHub
def prId = sh(
script: "node scripts/github-releases create-pull-request ${PREPARED_BRANCH} master 'Prepare version ${NEXT_VERSION}' --assignee ${assignee} --reviewer bmunkholm --print-number",
returnStdout: true,
).trim()
// Update the description of the build to include a link for the pull request.
currentBuild.description = """
Created pull request <a href='https://github.com/${GITHUB_OWNER}/${GITHUB_REPO}/pull/${prId}'>#${prId}</a>
"""
}
}
}
}
}
}
}