-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathrelease.ts
406 lines (378 loc) · 13.2 KB
/
release.ts
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
import { github, typescript } from 'projen';
import { ACTIONS_CHECKOUT, ACTIONS_SETUP_NODE, YARN_INSTALL } from './common';
export const enum PublishTargetOutput {
DIST_TAG = 'dist-tag',
GITHUB_RELEASE = 'github-release',
IS_LATEST = 'latest',
IS_PRERELEASE = 'prerelease',
}
// The ARN of the OpenPGP key used to sign release artifacts uploaded to GH Releases.
const CODE_SIGNING_USER_ID = '[email protected]';
export class ReleaseWorkflow {
public constructor(private readonly project: typescript.TypeScriptProject) {
new ReleaseTask(project);
new TagReleaseTask(project);
let release = project.github!.addWorkflow('release');
release.runName = 'Release ${{ github.ref_name }}';
release.on({ push: { tags: ['v*.*.*'] } });
const nodeVersion = project.minNodeVersion?.split('.', 1).at(0) ?? 'lts/*';
const releasePackageName = 'release-package';
const publishTarget = 'publish-target';
const federateToAwsStep: github.workflows.JobStep = {
name: 'Federate to AWS',
uses: 'aws-actions/configure-aws-credentials@v1',
with: {
'aws-region': 'us-east-1',
'role-to-assume': '${{ secrets.AWS_ROLE_TO_ASSUME }}',
'role-session-name': 'GHA-aws-jsii-rosetta@${{ github.ref_name }}',
},
};
release.addJob('build', {
name: 'Build release package',
env: {
CI: 'true',
},
outputs: {
[PublishTargetOutput.DIST_TAG]: { stepId: publishTarget, outputName: PublishTargetOutput.DIST_TAG },
[PublishTargetOutput.IS_LATEST]: { stepId: publishTarget, outputName: PublishTargetOutput.IS_LATEST },
[PublishTargetOutput.GITHUB_RELEASE]: { stepId: publishTarget, outputName: PublishTargetOutput.GITHUB_RELEASE },
[PublishTargetOutput.IS_PRERELEASE]: { stepId: publishTarget, outputName: PublishTargetOutput.IS_PRERELEASE },
},
permissions: {
idToken: github.workflows.JobPermission.WRITE,
contents: github.workflows.JobPermission.READ,
},
runsOn: ['ubuntu-latest'],
steps: [
ACTIONS_CHECKOUT,
ACTIONS_SETUP_NODE(nodeVersion),
YARN_INSTALL,
{
name: 'Prepare Release',
run: 'yarn release ${{ github.ref_name }}',
},
{
name: 'Determine Target',
id: publishTarget,
run: 'yarn ts-node projenrc/publish-target.ts ${{ github.ref_name }}',
env: {
// A GitHub token is required to list GitHub Releases, so we can tell if the `latest` dist-tag is needed.
GITHUB_TOKEN: '${{ github.token }}',
},
},
{
...federateToAwsStep,
// Only necessary if we're going to be publishing assets to GitHub Releases.
if: `fromJSON(steps.publish-target.outputs.${PublishTargetOutput.GITHUB_RELEASE})`,
},
{
name: 'Sign Tarball',
if: `fromJSON(steps.publish-target.outputs.${PublishTargetOutput.GITHUB_RELEASE})`,
run: [
'set -eo pipefail',
// First, we're going to be configuring GPG "correctly"
'export GNUPGHOME=$(mktemp -d)',
'echo "charset utf-8" > ${GNUPGHOME}/gpg.conf',
'echo "no-comments" >> ${GNUPGHOME}/gpg.conf',
'echo "no-emit-version" >> ${GNUPGHOME}/gpg.conf',
'echo "no-greeting" >> ${GNUPGHOME}/gpg.conf',
// Now, we need to import the OpenPGP private key into the keystore
'secret=$(aws secretsmanager get-secret-value --secret-id=${{ secrets.OPEN_PGP_KEY_ARN }} --query=SecretString --output=text)',
'privatekey=$(node -p "(${secret}).PrivateKey")',
'passphrase=$(node -p "(${secret}).Passphrase")',
'echo "::add-mask::${passphrase}"', // !!! IMPORTANT !!! (Ensures the value does not leak into public logs)
'unset secret',
'echo ${passphrase} | gpg --batch --yes --import --armor --passphrase-fd=0 <(echo "${privatekey}")',
'unset privatekey',
// Now we can actually detach-sign the artifacts
'for file in $(find dist -type f -not -iname "*.asc"); do',
` echo \${passphrase} | gpg --pinentry-mode=loopback --batch --yes --local-user=${JSON.stringify(
CODE_SIGNING_USER_ID,
)} --detach-sign --armor --passphrase-fd=0 \${file}`,
'done',
'unset passphrase',
// Clean up the GnuPG home directory (secure-wipe)
'find ${GNUPGHOME} -type f -exec shred --remove {} \\;',
].join('\n'),
},
{
name: 'Upload artifact',
uses: 'actions/[email protected]',
with: {
name: releasePackageName,
path: '${{ github.workspace }}/dist',
overwrite: true,
},
},
],
});
const downloadArtifactStep: github.workflows.JobStep = {
name: 'Download artifact',
uses: 'actions/download-artifact@v4',
with: {
name: releasePackageName,
},
};
release.addJob('release-to-github', {
name: 'Create GitHub Release',
env: {
CI: 'true',
},
if: `fromJSON(needs.build.outputs.${PublishTargetOutput.GITHUB_RELEASE})`,
needs: ['build'],
permissions: {
contents: github.workflows.JobPermission.WRITE,
},
runsOn: ['ubuntu-latest'],
steps: [
downloadArtifactStep,
{
id: 'release-exists',
name: 'Verify if release exists',
run: [
'if gh release view ${{ github.ref_name }} --repo=${{ github.repository }} &>/dev/null',
'then',
'echo "result=true" >> $GITHUB_OUTPUT',
'else',
'echo "result=false" >> $GITHUB_OUTPUT',
'fi',
].join('\n'),
env: {
GH_TOKEN: '${{ github.token }}',
},
},
{
name: 'Create PreRelease',
if: `!fromJSON(steps.release-exists.outputs.result) && fromJSON(needs.build.outputs.${PublishTargetOutput.IS_PRERELEASE})`,
run: [
'gh release create ${{ github.ref_name }}',
'--repo=${{ github.repository }}',
'--generate-notes',
'--title=${{ github.ref_name }}',
'--verify-tag',
'--prerelease',
`--latest=\${{ needs.build.outputs.${PublishTargetOutput.IS_LATEST} }}`,
].join(' '),
env: {
GH_TOKEN: '${{ github.token }}',
},
},
{
name: 'Create Release',
if: `!fromJSON(steps.release-exists.outputs.result) && !fromJSON(needs.build.outputs.${PublishTargetOutput.IS_PRERELEASE})`,
run: [
'gh release create ${{ github.ref_name }}',
'--repo=${{ github.repository }}',
'--generate-notes',
'--title=${{ github.ref_name }}',
'--verify-tag',
`--latest=\${{ needs.build.outputs.${PublishTargetOutput.IS_LATEST} }}`,
].join(' '),
env: {
GH_TOKEN: '${{ github.token }}',
},
},
{
name: 'Attach assets',
run: [
'gh release upload ${{ github.ref_name }}',
'--repo=${{ github.repository }}',
'--clobber',
'${{ github.workspace }}/**/*',
].join(' '),
env: {
GH_TOKEN: '${{ github.token }}',
},
},
],
});
release.addJob('release-npm-package', {
name: `Release to registry.npmjs.org`,
env: {
CI: 'true',
},
needs: ['build'],
permissions: {
idToken: github.workflows.JobPermission.WRITE,
contents: github.workflows.JobPermission.READ,
},
runsOn: ['ubuntu-latest'],
steps: [
downloadArtifactStep,
{
...ACTIONS_SETUP_NODE(),
with: {
'always-auth': true,
'node-version': nodeVersion,
'registry-url': `https://registry.npmjs.org/`,
},
},
federateToAwsStep,
{
name: 'Set NODE_AUTH_TOKEN',
run: [
'secret=$(aws secretsmanager get-secret-value --secret-id=${{ secrets.NPM_TOKEN_ARN }} --query=SecretString --output=text)',
'token=$(node -p "(${secret}).token")',
'unset secret',
'echo "::add-mask::${token}"', // !!! IMPORTANT !!! (Ensures the value does not leak into public logs)
'echo "NODE_AUTH_TOKEN=${token}" >> $GITHUB_ENV',
'unset token',
].join('\n'),
},
{
name: 'Publish',
run: [
'npm publish ${{ github.workspace }}/js/jsii-*.tgz',
'--access=public',
`--tag=\${{ needs.build.outputs.${PublishTargetOutput.DIST_TAG} }}`,
].join(' '),
},
{
name: 'Tag "latest"',
if: `fromJSON(needs.build.outputs.${PublishTargetOutput.IS_LATEST})`,
run: 'npm dist-tag add jsii-rosetta@${{ github.ref_name }} latest',
},
],
});
}
public autoTag(opts: AutoTagWorkflowProps): this {
const suffix = opts.nameSuffix ? `-${opts.nameSuffix}` : opts.branch ? `-${opts.branch}` : '';
new AutoTagWorkflow(this.project, `auto-tag-${opts.preReleaseId ?? 'releases'}${suffix}`, opts);
return this;
}
}
class ReleaseTask {
public constructor(project: typescript.TypeScriptProject) {
const task = project.addTask('release', {
description: 'Prepare a release bundle',
});
task.exec('ts-node projenrc/set-version.ts', {
name: 'set-version',
receiveArgs: true,
});
task.spawn(project.preCompileTask);
task.spawn(project.compileTask);
task.spawn(project.postCompileTask);
task.spawn(project.testTask);
task.spawn(project.packageTask);
task.exec('yarn version --no-git-tag-version --new-version 0.0.0', {
name: 'reset-version',
});
}
}
class TagReleaseTask {
public constructor(project: typescript.TypeScriptProject) {
const task = project.addTask('tag-release', {
description: 'Tag this commit for release',
});
task.exec('ts-node projenrc/tag-release.ts', {
name: 'tag-release',
receiveArgs: true,
});
}
}
interface AutoTagWorkflowProps {
/**
* The version used as the tagging base
*/
readonly releaseLine: string;
/**
* The branch on which to trigger this AutoTagWorkflow.
*
* @default - the repository's default branch
*/
readonly branch?: string;
/**
* The schedule on which to run this AutoTagWorkflow instance.
*
* @see https://pubs.opengroup.org/onlinepubs/9699919799/utilities/crontab.html#tag_20_25_07
*
* @default none
*/
readonly schedule?: string;
/**
* The run name to use for this workflow.
*
* @default - GitHub's default run name will be used.
*/
readonly runName?: string;
/**
* The pre-release identifier to be used.
*
* @default - a regular release will be tagged.
*/
readonly preReleaseId?: string;
/**
* The workflow name suffix. A single `-` will be prepended to this value if
* present, which is then appended at the end of the workflow name.
*
* @default - derived from the branch name (if present).
*/
readonly nameSuffix?: string;
}
class AutoTagWorkflow {
public constructor(project: typescript.TypeScriptProject, name: string, props: AutoTagWorkflowProps) {
const nodeVersion = project.minNodeVersion?.split('.', 1).at(0) ?? 'lts/*';
const workflow = project.github!.addWorkflow(name);
workflow.runName = props.runName;
workflow.on({
schedule:
props.schedule != null
? [
{
cron: props.schedule,
},
]
: undefined,
workflowDispatch: {},
});
workflow.addJob('pre-flight', {
name: 'Pre-Flight Checks',
runsOn: ['ubuntu-latest'],
outputs: {
sha: {
stepId: 'git',
outputName: 'sha',
},
},
permissions: { contents: github.workflows.JobPermission.READ },
steps: [
{ ...ACTIONS_CHECKOUT, with: { ...ACTIONS_CHECKOUT.with, ref: props.branch } },
ACTIONS_SETUP_NODE(nodeVersion),
YARN_INSTALL,
{ name: 'Build', run: 'yarn build' },
{ id: 'git', name: 'Identify git SHA', run: 'echo sha=$(git rev-parse HEAD) >> $GITHUB_OUTPUT' },
],
});
workflow.addJob('auto-tag', {
name: 'Auto-Tag Release',
needs: ['pre-flight'],
runsOn: ['ubuntu-latest'],
permissions: {},
steps: [
{
...ACTIONS_CHECKOUT,
with: {
...ACTIONS_CHECKOUT.with,
ref: '${{ needs.pre-flight.outputs.sha }}',
token: '${{ secrets.PROJEN_GITHUB_TOKEN }}',
},
},
ACTIONS_SETUP_NODE(nodeVersion),
YARN_INSTALL,
{
name: 'Set git identity',
run: ['git config user.name "github-actions"', 'git config user.email "[email protected]"'].join(
'\n',
),
},
{
name: `Tag ${props.preReleaseId ? 'PreRelease' : 'Release'}`,
run: `yarn tag-release --idempotent --no-sign --push ${
props.preReleaseId ? `--prerelease=${props.preReleaseId} ` : ''
}--release-line=${props.releaseLine}`,
},
],
});
}
}