-
Notifications
You must be signed in to change notification settings - Fork 11
/
Jenkinsfile
334 lines (308 loc) · 9.33 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
#!groovy
import groovy.json.JsonOutput
// Changes the version in the package.json and package-lock.json
def changeVersion(String preId = "") {
// Determine the upcoming release type
nextVersionType = sh(
script: "node ./scripts/next-version.js",
returnStdout: true,
).trim()
// Ask NPM to update the package json and lock and read the next version
// If a preid is specified, perform a pre-release afterwards
if (preId) {
// Update the version of the package again
nextVersion = sh(
script: "npm version pre${nextVersionType} --no-git-tag-version --preid=${preId}",
returnStdout: true,
).trim()
} else {
nextVersion = sh(
script: "npm version ${nextVersionType} --no-git-tag-version",
returnStdout: true,
).trim()
}
// Set the build name
currentBuild.displayName += ": ${nextVersion}"
return nextVersion
}
def packAndArchive() {
// Remove any archives produced by the tests
sh 'rm -f react-realm-context-*.tgz'
// Ignore the prepack running "build" again
sh 'npm pack --ignore-scripts'
// Archive the archive
archiveArtifacts 'react-realm-context-*.tgz'
}
def copyReleaseNotes(versionBefore, versionAfter) {
// Read the release notes and replace in any variables
releaseNotes = readFile 'RELEASENOTES.md'
releaseNotes = releaseNotes
.replaceAll("\\{PREVIOUS_VERSION\\}", versionBefore)
.replaceAll("\\{CURRENT_VERSION\\}", versionAfter)
// Write back the release notes
writeFile file: 'RELEASENOTES.md', text: releaseNotes
// Get todays date
today = new Date().format('yyyy-MM-dd')
// Append the release notes to the change log
changeLog = readFile 'CHANGELOG.md'
writeFile(
file: 'CHANGELOG.md',
text: "# Release ${versionAfter.substring(1)} (${today})\n\n${releaseNotes}\n\n${changeLog}",
)
}
def testExampleApp(String app) {
dir("examples/$app") {
sh 'npm install ../../react-realm-context-*.tgz --no-save'
sh 'npm test -- --forceExit'
}
}
pipeline {
agent {
docker {
image 'node:8'
label 'docker'
// /etc/passwd is mapped so a jenkins users is available from within the container
// ~/.ssh is mapped to allow pushing to GitHub via SSH
args '-e "HOME=${WORKSPACE}" -v /etc/passwd:/etc/passwd:ro -v /home/jenkins/.ssh:/home/jenkins/.ssh:ro'
}
}
environment {
// Tells Jest (used for example app tests) to run non-interactive
CI = true
// Parameters used by the github releases script
GITHUB_OWNER="realm"
GITHUB_REPO="react-realm-context"
}
options {
// Prevent checking out multiple times
skipDefaultCheckout()
}
parameters {
booleanParam(
name: 'PREPARE',
defaultValue: false,
description: '''Prepare for publishing?
Changes version based on release notes,
copies release notes to changelog,
creates a draft GitHub release and
pushes a tagged commit to git.
''',
)
}
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:realm/react-realm-context.git'
]]
])
// 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"'
// Setting the TAG_NAME env as this is not happening when skipping default checkout.
script {
env.TAG_NAME = sh(
script: 'git tag --points-at HEAD',
returnStdout: true,
).trim()
}
}
}
stage('Install') {
steps {
// Perform the install
sh 'npm install'
}
}
stage('Lint & build') {
when {
// Don't do this when preparing for a release
not { environment name: 'PREPARE', value: 'true' }
}
parallel {
stage('Lint') {
steps {
sh 'npm run lint:ts'
}
}
stage('Build') {
steps {
sh 'npm run build'
}
}
stage('Docs') {
steps {
sh 'npm run docs'
}
}
}
}
stage('Pre-package tests') {
when {
// Don't do this when preparing for a release
not { environment name: 'PREPARE', value: 'true' }
}
parallel {
stage('Unit tests') {
steps {
sh 'MOCHA_FILE=pre-unit-test-results.xml npm run test:ci -- src/**/*.test.tsx'
}
}
stage('Environment tests') {
steps {
sh 'MOCHA_FILE=pre-environments-test-results.xml npm run test:ci -- integration-tests/environments.test.ts'
}
}
}
post {
always {
junit(
allowEmptyResults: true,
keepLongStdio: true,
testResults: 'pre-*-test-results.xml'
)
}
}
}
// Simple packaging for PRs and runs that don't prepare for releases
stage('Package') {
when {
// Don't do this when preparing for a release
not { environment name: 'PREPARE', value: 'true' }
}
steps {
script {
if (TAG_NAME && TAG_NAME.startsWith("v")) {
// Update the build display name
currentBuild.displayName += ": ${TAG_NAME} (publish)"
} else {
// Change the version to a prerelease if it's not preparing or is a release
changeVersion "${JOB_BASE_NAME}-${BUILD_NUMBER}"
}
}
// Package and archive the archive
script {
packAndArchive()
}
}
}
stage('Post-packaging tests') {
when {
// Don't do this when preparing for a release
not { environment name: 'PREPARE', value: 'true' }
}
parallel {
stage('Example #1') {
steps {
script { testExampleApp('initializer-and-query') }
}
}
stage('Example #2') {
steps {
script { testExampleApp('multiple-realms') }
}
}
stage('Example #3') {
steps {
script { testExampleApp('simple-context') }
}
}
stage('Example #4') {
steps {
script { testExampleApp('simple-render-props') }
}
}
}
}
// More advanced packaging for commits tagged as versions
stage('Publish') {
when {
// Don't do this when preparing for a release
not { environment name: 'PREPARE', value: 'true' }
// Check if a tag starting with a v (for version) is pointing at this commit
tag "v*"
}
steps {
// Upload artifacts to GitHub and publish release
withCredentials([
string(credentialsId: 'github-release-token', variable: 'GITHUB_TOKEN')
]) {
script {
for (file in findFiles(glob: 'react-realm-context-*.tgz')) {
sh "node scripts/github-releases upload-asset $TAG_NAME $file"
}
}
script {
sh "node scripts/github-releases publish $TAG_NAME"
}
}
// Update the gh-pages branch
sshagent(['realm-ci-ssh']) {
sh "npx gh-pages -d generated-docs"
}
// Publish archive to NPM
withCredentials([
file(
credentialsId: 'npm-registry-npmrc',
variable: 'NPM_CONFIG_USERCONFIG',
)
]) {
sh 'npm publish react-realm-context-*.tgz'
}
}
}
// Prepares for a release by
// 1. changing version,
// 2. copying release notes to the changelog,
// 3. creating a draft GitHub release and
// 4. pushing a tagged commit to git
stage('Prepare') {
when {
environment name: 'PREPARE', value: 'true'
}
steps {
script {
// Read the current version of the package
packageJson = readJSON file: 'package.json'
versionBefore = "v${packageJson.version}"
// Change the version
nextVersion = changeVersion()
// Add to the displa name of the build job that we're preparing a release
currentBuild.displayName += " (prepare)"
}
// Append the RELEASENOTES to the CHANGELOG
script {
copyReleaseNotes(versionBefore, nextVersion)
}
// Create a draft release on GitHub
script {
withCredentials([
string(credentialsId: 'github-release-token', variable: 'GITHUB_TOKEN')
]) {
sh "node scripts/github-releases create-draft $nextVersion RELEASENOTES.md"
}
}
// Restore the release notes from the template
sh 'cp docs/RELEASENOTES.template.md RELEASENOTES.md'
// 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 ${nextVersion}'"
sh "git tag -f ${nextVersion}"
// Push to GitHub with tags
sshagent(['realm-ci-ssh']) {
sh "git push --tags origin HEAD"
}
}
}
}
}