-
Notifications
You must be signed in to change notification settings - Fork 14
327 lines (275 loc) · 13.5 KB
/
release.yaml
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
name: Release
on:
push:
branches:
- main
- release/v*
concurrency: ${{ github.workflow }}-${{ github.ref }}
jobs:
changeset-magic:
name: Set up Changeset PR or Release Packages
runs-on: ubuntu-latest
steps:
# Checkout the Branch which was pushed ('main' or 'release/v*')
- name: Checkout
uses: actions/checkout@v4
- name: Setup Node & pnpm
uses: ./.github/actions/setup-pnpm
- name: Install & build Dependencies
run: pnpm bootstrap
# This will fail the build if something in the publish setup is not correct
# before changeset magic is starting to run
- name: Test publish step
run: pnpm -r publish --dry-run
# The changeset action will behave differently based on whether there are
# new changesets on the github.ref branch:
#
# - new changesets: create a preview PR with the new version bumps and changelogs
# - no new changesets (the preview PR got merged into github.ref branch): publish packages
- name: Changeset Magic
id: changesets
uses: changesets/action@v1
env:
GITHUB_TOKEN: ${{ secrets.SWISSPOSTDEVS_ACCESS_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
with:
title: 'chore(changesets): 🦋📦 publish packages (${{ github.ref_name }})'
commit: 'chore(changesets): publish packages'
publish: pnpm changeset:publish
version: pnpm changeset:version
- name: Create Summary
id: summary
uses: actions/github-script@v7
env:
PUBLISHED: ${{ steps.changesets.outputs.published }}
PUBLISHED_PACKAGES: ${{ steps.changesets.outputs.publishedPackages }}
with:
script: |
return `# Changesets
- Changesets published: <code>${process.env.PUBLISHED}</code>
- Published Packages:
<pre>${JSON.stringify(JSON.parse(process.env.PUBLISHED_PACKAGES), null, 2)}</pre>
`
- name: Output Summary
run: echo -e ${{ steps.summary.outputs.result }} >> $GITHUB_STEP_SUMMARY
# Only run this Job if:
# - Workflow runs on the 'main' Branch
collect-release-data:
name: Collect Release Data
runs-on: ubuntu-latest
needs: changeset-magic
if: github.ref_name == 'release/v8'
outputs:
release-data: ${{ steps.release-data.outputs.result }}
changeset-branch-exists: ${{ steps.changeset-branch-exists.outputs.exists }}
release-branch-exists: ${{ steps.release-branch-exists.outputs.exists }}
release-changeset-branch-exists: ${{ steps.release-changeset-branch-exists.outputs.exists }}
steps:
# Checkout the Branch which has been pushed ('main' or 'release/v*')
- name: Checkout
uses: actions/checkout@v4
- name: Setup Node & pnpm
uses: ./.github/actions/setup-pnpm
- name: Install Dependencies
run: pnpm i
- name: Create Status Files
run: |
pnpm m ls --depth=0 --json > monorepo.json
pnpm changeset status --output=changesets.json
# Read the status Files and collect release data on the @swisspost/design-system-styles package
- name: Prepare Release Data
id: release-data-pre
uses: actions/github-script@v7
with:
script: |
const fs = require('fs')
const changesets = JSON.parse(fs.readFileSync('changesets.json', 'utf8'))
const release = changesets.releases.find(r => r.name === '@swisspost/design-system-styles')
const monorepo = JSON.parse(fs.readFileSync('monorepo.json', 'utf8'))
const styles = JSON.parse(fs.readFileSync('./packages/styles/package.json', 'utf8'))
fs.unlinkSync('changesets.json')
fs.unlinkSync('monorepo.json')
if (!release) return null
const oldMajor = release.oldVersion.split('.')[0];
const newMajor = release.newVersion.split('.')[0];
const updatedVersions = changesets.releases.reduce((deps, r) => ({ ...deps, [r.name]: r.newVersion }), {})
const dependencies = monorepo
.filter(pkg => pkg.version !== undefined)
.reduce((packages, pkg) => ({ ...packages, [pkg.name]: updatedVersions[pkg.name] ?? pkg.version }), { ...(styles.peerDependencies ?? {}) })
return {
isMajor: release.type === 'major',
old: {
version: release.oldVersion,
major: oldMajor,
siteUrl: `design-system-version-${oldMajor}`,
dependencies: {}
},
new: {
version: release.newVersion,
major: newMajor,
siteUrl: 'design-system',
dependencies
},
branchName: `release/v${oldMajor}`,
tagName: `@swisspost/design-system-styles@${release.oldVersion}`,
tagPattern: `@swisspost/design-system-styles@${oldMajor}.*`
}
- name: Check if changset-release/main Branch exists
id: changeset-branch-exists
uses: GuillaumeFalourd/branch-exists@v1
with:
branch: changeset-release/release/v8
- name: Check if release/v* Branch exists
id: release-branch-exists
uses: GuillaumeFalourd/branch-exists@v1
with:
branch: ${{ fromJSON(steps.release-data-pre.outputs.result).branchName }}
- name: Check if changeset-release/release/v* Branch exists
id: release-changeset-branch-exists
uses: GuillaumeFalourd/branch-exists@v1
with:
branch: changeset-release/${{ fromJSON(steps.release-data-pre.outputs.result).branchName }}
- name: Get previous Major Version Tag Name
id: previous-major-version
run: |
tagNameList=$(git tag --list ${{ fromJSON(steps.release-data-pre.outputs.result).tagPattern }} --sort="-refname")
set -- $tagNameList
echo "tagName=$1" >> $GITHUB_OUTPUT
- name: Checkout previous Major Version
if: fromJSON(steps.release-data-pre.outputs.result).isMajor == true
uses: actions/checkout@v4
with:
ref: ${{ steps.release-branch-exists.outputs.exists == 'true' && fromJSON(steps.release-data-pre.outputs.result).branchName || steps.previous-major-version.outputs.tagName }}
- name: Create Status Files
if: fromJSON(steps.release-data-pre.outputs.result).isMajor == true
run: pnpm m ls --depth=0 --json > monorepo.json
- name: Finalize Release Data
id: release-data
uses: actions/github-script@v7
with:
script: |
const fs = require('fs')
const releaseData = ${{ steps.release-data-pre.outputs.result }}
if (!releaseData) return null
if (releaseData.isMajor) {
const monorepo = JSON.parse(fs.readFileSync('monorepo.json', 'utf8'))
const styles = JSON.parse(fs.readFileSync('./packages/styles/package.json', 'utf8'))
releaseData.old.dependencies = monorepo
.filter(pkg => pkg.version !== undefined)
.reduce((packages, pkg) => ({ ...packages, [pkg.name]: pkg.version }), { ...(styles.peerDependencies ?? {}) })
}
return releaseData
- name: Create Summary
id: summary
uses: actions/github-script@v7
env:
REF_NAME: ${{ github.ref_name }}
RELEASE_DATA: ${{ steps.release-data.outputs.result }}
CHANGESET_BRANCH_EXISTS: ${{ steps.changeset-branch-exists.outputs.exists }}
RELEASE_BRANCH_EXISTS: ${{ steps.release-branch-exists.outputs.exists }}
RELEASE_CHANGESET_BRANCH_EXISTS: ${{ steps.release-changeset-branch-exists.outputs.exists }}
with:
script: |
const release = JSON.parse(process.env.RELEASE_DATA)
const releaseBranch = release?.branchName ? `(<code>${release.branchName}</code>)` : ''
const releaseChangesetBranch = release?.branchName ? `(<code>changeset-release/${release.branchName}</code>)` : ''
return `# Release Data
- Collecting Data: ${process.env.RELEASE_DATA === 'null' ? ':warning: WARNING: No Release Data found!' : ':rocket: SUCCESS!'}
<pre>${JSON.stringify(release, null, 2)}</pre>
- The Changeset Branch (<code>changeset-release/${process.env.REF_NAME}</code>) exists already: <code>${process.env.CHANGESET_BRANCH_EXISTS}</code>
- The Release Branch ${releaseBranch} exists already: <code>${process.env.RELEASE_BRANCH_EXISTS}</code>
- The Release-Changeset Branch ${releaseChangesetBranch} exists already: <code>${process.env.RELEASE_CHANGESET_BRANCH_EXISTS}</code>
`
- name: Output Summary
run: echo -e ${{ steps.summary.outputs.result }} >> $GITHUB_STEP_SUMMARY
# Only run this Job if:
# - Workflow runs on the 'main' Branch
# - Release-Changeset Branch exists
# - Release Data exists
update-changeset-branch:
name: Update changeset-release/{main,release/v*} Branch
runs-on: ubuntu-latest
needs: collect-release-data
if: github.ref_name == 'release/v8' && needs.collect-release-data.outputs.release-changeset-branch-exists == 'true' && fromJSON(needs.collect-release-data.outputs.release-data) != null
steps:
# Checkout the changeset Branch
- name: Checkout
uses: actions/checkout@v4
with:
token: ${{ secrets.SWISSPOSTDEVS_ACCESS_TOKEN }}
ref: changeset-release/changeset-release/release/v8
- name: Setup Node & pnpm
uses: ./.github/actions/setup-pnpm
with:
use_cache: false # do not use the pnpm cache if you do not install any dependencies
# Only run this Step if a major release was detected
- name: Update or create Documentation _redirects
id: update-redirects
if: fromJSON(needs.collect-release-data.outputs.release-data).isMajor == true
uses: actions/github-script@v7
with:
script: |
const fs = require('fs')
const release = ${{ needs.collect-release-data.outputs.release-data }}
const redirectsPath = './packages/documentation/public/_redirects'
let redirects = ''
const redirect = `/v${release.old.major} https://${release.old.siteUrl}.netlify.app`
if (fs.existsSync(redirectsPath)) {
redirects = fs.readFileSync(redirectsPath, 'utf8')
} else {
redirects = '# Redirects from what the browser requests to what we serve\n'
}
if(!redirects.includes(redirect)) redirects += redirect
fs.writeFileSync(redirectsPath, redirects + '\n')
return true
- name: Update Documentation versions.json
uses: actions/github-script@v7
with:
script: |
const fs = require('fs')
const release = ${{ needs.collect-release-data.outputs.release-data }}
const versionsPath = './packages/documentation/public/assets/versions.json'
const versions = JSON.parse(fs.readFileSync(versionsPath, 'utf8'))
const currentVersion = versions.find(version => version.version.startsWith(release.old.major))
currentVersion.version = release.new.version
currentVersion.dependencies = release.new.dependencies
if (release.isMajor) {
currentVersion.version = release.old.version
currentVersion.url = `https://${release.old.siteUrl}.netlify.app`
currentVersion.dependencies = release.old.dependencies
versions.unshift({
title: `Version ${release.new.major}`,
version: release.new.version,
description: 'Pattern documentation, code snippets and implementation guidelines for the Design System Styles.',
url: `https://${release.new.siteUrl}.post.ch`,
dependencies: release.new.dependencies
})
}
fs.writeFileSync(versionsPath, JSON.stringify(versions, null, 2) + '\n')
- name: Remove Status Files
run: rm -f monorepo.json
- name: Commit Changes and Push Branch
uses: EndBug/add-and-commit@v9
with:
author_name: Swiss Post Bot
author_email: [email protected]
message: 'chore(changesets): update release specific files'
push: true
- name: Create Summary
id: summary
uses: actions/github-script@v7
env:
REF_NAME: ${{ github.ref_name }}
RELEASE_DATA: ${{ needs.collect-release-data.outputs.release-data }}
RELEASE_BRANCH_EXISTS: ${{ needs.collect-release-data.outputs.release-changeset-branch-exists }}
with:
script: |
const release = JSON.parse(process.env.RELEASE_DATA)
return `# Changeset Branch
The Release-Changeset Branch <code>changeset-release/${process.env.REF_NAME}</code> has been created by the Changeset Action.
In addition to that, the following updates have been made to this Branch:
${release.isMajor && process.env.RELEASE_BRANCH_EXISTS === 'false' ? `- Netlify <code>_redirects</code> File has been created on to the <code>changeset-release/${process.env.REF_NAME}</code> Branch.` : ''}
- The Netlify <code>versions.json</code> File has been updated on to the <code>changeset-release/${process.env.REF_NAME}</code> Branch.
`
- name: Output Summary
run: echo -e ${{ steps.summary.outputs.result }} >> $GITHUB_STEP_SUMMARY