Skip to content

Commit 672ce46

Browse files
authored
chore(toolkit-lib): publish toolkit-lib docs to s3 (in dryrun) (#114)
Adds a release for the toolkit-lib s3 docs to be published to s3. --- By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license
1 parent 3e228d6 commit 672ce46

File tree

4 files changed

+194
-2
lines changed

4 files changed

+194
-2
lines changed

.github/workflows/release.yml

+45
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.projenrc.ts

+19-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { CodeCovWorkflow } from './projenrc/codecov';
88
import { ESLINT_RULES } from './projenrc/eslint';
99
import { JsiiBuild } from './projenrc/jsii';
1010
import { RecordPublishingTimestamp } from './projenrc/record-publishing-timestamp';
11+
import { S3DocsPublishing } from './projenrc/s3-docs-publishing';
1112

1213
// 5.7 sometimes gives a weird error in `ts-jest` in `@aws-cdk/cli-lib-alpha`
1314
// https://github.com/microsoft/TypeScript/issues/60159
@@ -1127,6 +1128,13 @@ const toolkitLib = configureProject(
11271128
}),
11281129
);
11291130

1131+
new S3DocsPublishing(toolkitLib, {
1132+
docsStream: 'toolkit-lib',
1133+
artifactPath: 'docs.zip',
1134+
bucketName: '${{ vars.DOCS_BUCKET_NAME }}',
1135+
roleToAssume: '${{ vars.PUBLISH_TOOLKIT_LIB_DOCS_ROLE_ARN }}',
1136+
});
1137+
11301138
// Eslint rules
11311139
toolkitLib.eslint?.addRules({
11321140
'@cdklabs/no-throw-default-error': ['error'],
@@ -1195,9 +1203,19 @@ for (const tsconfig of [toolkitLib.tsconfigDev]) {
11951203
}
11961204
}
11971205

1198-
toolkitLib.addTask('docs', {
1206+
const toolkitLibDocs = toolkitLib.addTask('docs', {
11991207
exec: 'typedoc lib/index.ts',
1208+
receiveArgs: true,
12001209
});
1210+
toolkitLib.packageTask.spawn(toolkitLibDocs, {
1211+
// the nested directory is important
1212+
// the zip file needs to have this structure when created
1213+
args: ['--out dist/docs/cdk/api/toolkit-lib'],
1214+
});
1215+
toolkitLib.packageTask.exec('zip -r ../docs.zip cdk ', {
1216+
cwd: 'dist/docs',
1217+
});
1218+
12011219
toolkitLib.addTask('publish-local', {
12021220
exec: './build-tools/package.sh',
12031221
receiveArgs: true,

packages/@aws-cdk/toolkit-lib/.projen/tasks.json

+12-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

projenrc/s3-docs-publishing.ts

+118
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
import { Monorepo, TypeScriptWorkspace } from 'cdklabs-projen-project-types/lib/yarn';
2+
import { Component, github } from 'projen';
3+
4+
export interface S3DocsPublishingProps {
5+
/**
6+
* The docs stream to publish to.
7+
*/
8+
readonly docsStream: string;
9+
10+
/**
11+
* The path to the artifact in the dist folder
12+
*/
13+
readonly artifactPath: string;
14+
15+
/**
16+
* The role arn (or github expression) for OIDC to assume to do the actual publishing.
17+
*/
18+
readonly roleToAssume: string;
19+
20+
/**
21+
* The bucket name (or github expression) to publish to.
22+
*/
23+
readonly bucketName: string;
24+
}
25+
26+
export class S3DocsPublishing extends Component {
27+
private readonly github: github.GitHub;
28+
private readonly props: S3DocsPublishingProps;
29+
30+
constructor(project: TypeScriptWorkspace, props: S3DocsPublishingProps) {
31+
super(project);
32+
33+
const gh = (project.parent! as Monorepo).github;
34+
if (!gh) {
35+
throw new Error('This workspace does not have a GitHub instance');
36+
}
37+
this.github = gh;
38+
39+
this.props = props;
40+
}
41+
42+
public preSynthesize() {
43+
const releaseWf = this.github.tryFindWorkflow('release');
44+
if (!releaseWf) {
45+
throw new Error('Could not find release workflow');
46+
}
47+
48+
const safeName = this.project.name.replace('@', '').replace('/', '-');
49+
50+
releaseWf.addJob(`${safeName}_release_docs`, {
51+
name: `${this.project.name}: Publish docs to S3`,
52+
environment: 'releasing', // <-- this has the configuration
53+
needs: [`${safeName}_release_npm`],
54+
runsOn: ['ubuntu-latest'],
55+
permissions: {
56+
idToken: github.workflows.JobPermission.WRITE,
57+
contents: github.workflows.JobPermission.READ,
58+
},
59+
steps: [
60+
{
61+
name: 'Download build artifacts',
62+
uses: 'actions/download-artifact@v4',
63+
with: {
64+
name: `${this.project.name}_build-artifact`,
65+
path: 'dist',
66+
},
67+
},
68+
{
69+
name: 'Authenticate Via OIDC Role',
70+
id: 'creds',
71+
uses: 'aws-actions/configure-aws-credentials@v4',
72+
with: {
73+
'aws-region': 'us-east-1',
74+
'role-duration-seconds': 14400,
75+
'role-to-assume': '${{ vars.AWS_ROLE_TO_ASSUME_FOR_ACCOUNT }}',
76+
'role-session-name': 'releasing@aws-cdk-cli',
77+
},
78+
},
79+
{
80+
name: 'Assume the publishing role',
81+
id: 'publishing-creds',
82+
uses: 'aws-actions/configure-aws-credentials@v4',
83+
with: {
84+
'aws-region': 'us-east-1',
85+
'role-duration-seconds': 14400,
86+
'role-to-assume': this.props.roleToAssume,
87+
'role-session-name': 's3publishing@aws-cdk-cli',
88+
'role-chaining': true,
89+
},
90+
},
91+
{
92+
name: 'Publish docs',
93+
env: {
94+
BUCKET_NAME: this.props.bucketName,
95+
DOCS_STREAM: this.props.docsStream,
96+
},
97+
run: [
98+
'::add-mask::$BUCKET_NAME', // always hide bucket name
99+
100+
// setup paths
101+
`echo "S3_PATH=$DOCS_STREAM/${safeName}-v$(cat dist/version.txt).zip" >> "$GITHUB_ENV"`,
102+
'echo "S3_URI=s3://$BUCKET_NAME/$S3_PATH" >> "$GITHUB_ENV"',
103+
`echo "LATEST=latest-${this.props.docsStream}" >> "$GITHUB_ENV"`,
104+
105+
// create the latest marker
106+
'echo $S3_PATH > $LATEST',
107+
108+
// check if the target file already exists and upload
109+
'(! aws s3 ls --human-readable $S3_URI \\',
110+
`&& aws s3 cp --dryrun dist/${this.props.artifactPath} $S3_URI \\`,
111+
'&& aws s3 cp --dryrun $LATEST s3://$BUCKET_NAME/$LATEST) \\',
112+
'|| (echo "Docs artifact already published, skipping upload")',
113+
].join('\n'),
114+
},
115+
],
116+
});
117+
}
118+
}

0 commit comments

Comments
 (0)