Skip to content

Commit

Permalink
feat(pipelines): SimpleSynthAction takes array of build commands (#…
Browse files Browse the repository at this point in the history
…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*
  • Loading branch information
rix0rrr committed Sep 3, 2020
1 parent a65dd19 commit 44fcb4e
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 12 deletions.
11 changes: 7 additions & 4 deletions packages/@aws-cdk/pipelines/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,8 @@ const pipeline = new CdkPipeline(this, 'Pipeline', {
synthAction: new SimpleSynthAction({
sourceArtifact,
cloudAssemblyArtifact,
installCommand: 'npm install -g aws-cdk',
buildCommand: 'mvn package',
installCommands: ['npm install -g aws-cdk'],
buildCommands: ['mvn package'],
synthCommand: 'cdk synth',
})
});
Expand Down Expand Up @@ -402,7 +402,7 @@ const pipeline = new CdkPipeline(this, 'Pipeline', {
synthAction: SimpleSynthAction.standardNpmSynth({
sourceArtifact,
cloudAssemblyArtifact,
buildCommand: 'npm run build',
buildCommands: ['npm run build'],
additionalArtifacts: [
{
directory: 'test',
Expand Down Expand Up @@ -449,7 +449,10 @@ class MyPipelineStack extends Stack {
// Then you can login to codeartifact repository
// and npm will now pull packages from your repository
// Note the codeartifact login command requires more params to work.
buildCommand: 'aws codeartifact login --tool npm && npm run build',
buildCommands: [
'aws codeartifact login --tool npm',
'npm run build',
],
}),
});
}
Expand Down
61 changes: 53 additions & 8 deletions packages/@aws-cdk/pipelines/lib/synths/simple-synth-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,21 +101,56 @@ export interface SimpleSynthActionProps extends SimpleSynthOptions {
/**
* The install command
*
* If not provided by the build image or another dependency
* management tool, at least install the CDK CLI here using
* `npm install -g aws-cdk`.
*
* @default - No install required
* @deprecated Use `installCommands` instead
*/
readonly installCommand?: string;

/**
* The build command
*
* By default, we assume NPM projects are either written in JavaScript or are
* using `ts-node`, so don't need a build command.
*
* Otherwise, put the build command here, for example `npm run build`.
* If your programming language requires a compilation step, put the
* compilation command here.
*
* @default - No build required
* @deprecated Use `buildCommands` instead
*/
readonly buildCommand?: string;

/**
* Install commands
*
* If not provided by the build image or another dependency
* management tool, at least install the CDK CLI here using
* `npm install -g aws-cdk`.
*
* @default - No install required
*/
readonly installCommands?: string[];

/**
* The build commands
*
* If your programming language requires a compilation step, put the
* compilation command here.
*
* @default - No build required
*/
readonly buildCommands?: string[];

/**
* Test commands
*
* These commands are run after the build commands but before the
* synth command.
*
* @default - No test commands
*/
readonly testCommands?: string[];
}

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

if (this.props.installCommand && this.props.installCommands) {
throw new Error('Pass either \'installCommand\' or \'installCommands\', but not both');
}

if (this.props.buildCommand && this.props.buildCommands) {
throw new Error('Pass either \'buildCommand\' or \'buildCommands\', but not both');
}

const addls = props.additionalArtifacts ?? [];
if (Object.keys(addls).length > 0) {
if (!props.cloudAssemblyArtifact.artifactName) {
Expand All @@ -214,9 +257,10 @@ export class SimpleSynthAction implements codepipeline.IAction {
* Exists to implement IAction
*/
public bind(scope: Construct, stage: codepipeline.IStage, options: codepipeline.ActionBindOptions): codepipeline.ActionConfig {
const buildCommand = this.props.buildCommand;
const buildCommands = this.props.buildCommands ?? [this.props.buildCommand];
const installCommands = this.props.installCommands ?? [this.props.installCommand];
const testCommands = this.props.testCommands ?? [];
const synthCommand = this.props.synthCommand;
const installCommand = this.props.installCommand;

const project = new codebuild.PipelineProject(scope, 'CdkBuildProject', {
projectName: this.props.projectName ?? this.props.projectName,
Expand All @@ -227,12 +271,13 @@ export class SimpleSynthAction implements codepipeline.IAction {
pre_build: {
commands: filterEmpty([
this.props.subdirectory ? `cd ${this.props.subdirectory}` : '',
installCommand,
...installCommands,
]),
},
build: {
commands: filterEmpty([
buildCommand,
...buildCommands,
...testCommands,
synthCommand,
]),
},
Expand Down
44 changes: 44 additions & 0 deletions packages/@aws-cdk/pipelines/test/builds.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,50 @@ afterEach(() => {
app.cleanup();
});

test('SimpleSynthAction takes arrays of commands', () => {
// WHEN
new TestGitHubNpmPipeline(pipelineStack, 'Cdk', {
sourceArtifact,
cloudAssemblyArtifact,
synthAction: new cdkp.SimpleSynthAction({
sourceArtifact,
cloudAssemblyArtifact,
installCommands: ['install1', 'install2'],
buildCommands: ['build1', 'build2'],
testCommands: ['test1', 'test2'],
synthCommand: 'cdk synth',
}),
});

// THEN
expect(pipelineStack).toHaveResourceLike('AWS::CodeBuild::Project', {
Environment: {
Image: 'aws/codebuild/standard:4.0',
},
Source: {
BuildSpec: encodedJson(deepObjectLike({
phases: {
pre_build: {
commands: [
'install1',
'install2',
],
},
build: {
commands: [
'build1',
'build2',
'test1',
'test2',
'cdk synth',
],
},
},
})),
},
});
});

test.each([['npm'], ['yarn']])('%s build automatically determines artifact base-directory', (npmYarn) => {
// WHEN
new TestGitHubNpmPipeline(pipelineStack, 'Cdk', {
Expand Down

0 comments on commit 44fcb4e

Please sign in to comment.