Skip to content

Commit 44fcb4e

Browse files
authored
feat(pipelines): SimpleSynthAction takes array of build commands (#10152)
Change the single-string versions of `buildCommand` and `installCommand`, and turn them into arrays. People don't have to do `'command1 && command2'` anymore but can now simply supply an array of `['command1', 'command2']` which is more natural. Fixes #9357. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent a65dd19 commit 44fcb4e

File tree

3 files changed

+104
-12
lines changed

3 files changed

+104
-12
lines changed

packages/@aws-cdk/pipelines/README.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,8 @@ const pipeline = new CdkPipeline(this, 'Pipeline', {
199199
synthAction: new SimpleSynthAction({
200200
sourceArtifact,
201201
cloudAssemblyArtifact,
202-
installCommand: 'npm install -g aws-cdk',
203-
buildCommand: 'mvn package',
202+
installCommands: ['npm install -g aws-cdk'],
203+
buildCommands: ['mvn package'],
204204
synthCommand: 'cdk synth',
205205
})
206206
});
@@ -402,7 +402,7 @@ const pipeline = new CdkPipeline(this, 'Pipeline', {
402402
synthAction: SimpleSynthAction.standardNpmSynth({
403403
sourceArtifact,
404404
cloudAssemblyArtifact,
405-
buildCommand: 'npm run build',
405+
buildCommands: ['npm run build'],
406406
additionalArtifacts: [
407407
{
408408
directory: 'test',
@@ -449,7 +449,10 @@ class MyPipelineStack extends Stack {
449449
// Then you can login to codeartifact repository
450450
// and npm will now pull packages from your repository
451451
// Note the codeartifact login command requires more params to work.
452-
buildCommand: 'aws codeartifact login --tool npm && npm run build',
452+
buildCommands: [
453+
'aws codeartifact login --tool npm',
454+
'npm run build',
455+
],
453456
}),
454457
});
455458
}

packages/@aws-cdk/pipelines/lib/synths/simple-synth-action.ts

Lines changed: 53 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -101,21 +101,56 @@ export interface SimpleSynthActionProps extends SimpleSynthOptions {
101101
/**
102102
* The install command
103103
*
104+
* If not provided by the build image or another dependency
105+
* management tool, at least install the CDK CLI here using
106+
* `npm install -g aws-cdk`.
107+
*
104108
* @default - No install required
109+
* @deprecated Use `installCommands` instead
105110
*/
106111
readonly installCommand?: string;
107112

108113
/**
109114
* The build command
110115
*
111-
* By default, we assume NPM projects are either written in JavaScript or are
112-
* using `ts-node`, so don't need a build command.
113-
*
114-
* Otherwise, put the build command here, for example `npm run build`.
116+
* If your programming language requires a compilation step, put the
117+
* compilation command here.
115118
*
116119
* @default - No build required
120+
* @deprecated Use `buildCommands` instead
117121
*/
118122
readonly buildCommand?: string;
123+
124+
/**
125+
* Install commands
126+
*
127+
* If not provided by the build image or another dependency
128+
* management tool, at least install the CDK CLI here using
129+
* `npm install -g aws-cdk`.
130+
*
131+
* @default - No install required
132+
*/
133+
readonly installCommands?: string[];
134+
135+
/**
136+
* The build commands
137+
*
138+
* If your programming language requires a compilation step, put the
139+
* compilation command here.
140+
*
141+
* @default - No build required
142+
*/
143+
readonly buildCommands?: string[];
144+
145+
/**
146+
* Test commands
147+
*
148+
* These commands are run after the build commands but before the
149+
* synth command.
150+
*
151+
* @default - No test commands
152+
*/
153+
readonly testCommands?: string[];
119154
}
120155

121156
/**
@@ -190,6 +225,14 @@ export class SimpleSynthAction implements codepipeline.IAction {
190225
outputs: [props.cloudAssemblyArtifact, ...(props.additionalArtifacts ?? []).map(a => a.artifact)],
191226
};
192227

228+
if (this.props.installCommand && this.props.installCommands) {
229+
throw new Error('Pass either \'installCommand\' or \'installCommands\', but not both');
230+
}
231+
232+
if (this.props.buildCommand && this.props.buildCommands) {
233+
throw new Error('Pass either \'buildCommand\' or \'buildCommands\', but not both');
234+
}
235+
193236
const addls = props.additionalArtifacts ?? [];
194237
if (Object.keys(addls).length > 0) {
195238
if (!props.cloudAssemblyArtifact.artifactName) {
@@ -214,9 +257,10 @@ export class SimpleSynthAction implements codepipeline.IAction {
214257
* Exists to implement IAction
215258
*/
216259
public bind(scope: Construct, stage: codepipeline.IStage, options: codepipeline.ActionBindOptions): codepipeline.ActionConfig {
217-
const buildCommand = this.props.buildCommand;
260+
const buildCommands = this.props.buildCommands ?? [this.props.buildCommand];
261+
const installCommands = this.props.installCommands ?? [this.props.installCommand];
262+
const testCommands = this.props.testCommands ?? [];
218263
const synthCommand = this.props.synthCommand;
219-
const installCommand = this.props.installCommand;
220264

221265
const project = new codebuild.PipelineProject(scope, 'CdkBuildProject', {
222266
projectName: this.props.projectName ?? this.props.projectName,
@@ -227,12 +271,13 @@ export class SimpleSynthAction implements codepipeline.IAction {
227271
pre_build: {
228272
commands: filterEmpty([
229273
this.props.subdirectory ? `cd ${this.props.subdirectory}` : '',
230-
installCommand,
274+
...installCommands,
231275
]),
232276
},
233277
build: {
234278
commands: filterEmpty([
235-
buildCommand,
279+
...buildCommands,
280+
...testCommands,
236281
synthCommand,
237282
]),
238283
},

packages/@aws-cdk/pipelines/test/builds.test.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,50 @@ afterEach(() => {
2121
app.cleanup();
2222
});
2323

24+
test('SimpleSynthAction takes arrays of commands', () => {
25+
// WHEN
26+
new TestGitHubNpmPipeline(pipelineStack, 'Cdk', {
27+
sourceArtifact,
28+
cloudAssemblyArtifact,
29+
synthAction: new cdkp.SimpleSynthAction({
30+
sourceArtifact,
31+
cloudAssemblyArtifact,
32+
installCommands: ['install1', 'install2'],
33+
buildCommands: ['build1', 'build2'],
34+
testCommands: ['test1', 'test2'],
35+
synthCommand: 'cdk synth',
36+
}),
37+
});
38+
39+
// THEN
40+
expect(pipelineStack).toHaveResourceLike('AWS::CodeBuild::Project', {
41+
Environment: {
42+
Image: 'aws/codebuild/standard:4.0',
43+
},
44+
Source: {
45+
BuildSpec: encodedJson(deepObjectLike({
46+
phases: {
47+
pre_build: {
48+
commands: [
49+
'install1',
50+
'install2',
51+
],
52+
},
53+
build: {
54+
commands: [
55+
'build1',
56+
'build2',
57+
'test1',
58+
'test2',
59+
'cdk synth',
60+
],
61+
},
62+
},
63+
})),
64+
},
65+
});
66+
});
67+
2468
test.each([['npm'], ['yarn']])('%s build automatically determines artifact base-directory', (npmYarn) => {
2569
// WHEN
2670
new TestGitHubNpmPipeline(pipelineStack, 'Cdk', {

0 commit comments

Comments
 (0)