Skip to content

Commit

Permalink
fix(pipelines): can't use CodePipeline variables in Synth environment…
Browse files Browse the repository at this point in the history
… variables (#12602)

Fixes #12061
Fixes #11178

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
pgarbe authored Jan 25, 2021
1 parent 4df91fc commit 736b260
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,6 @@ export class SimpleSynthAction implements codepipeline.IAction, iam.IGrantable {

const environmentVariables = {
...copyEnvironmentVariables(...this.props.copyEnvironmentVariables || []),
...this.props.environmentVariables,
};

// A hash over the values that make the CodeBuild Project unique (and necessary
Expand Down Expand Up @@ -360,6 +359,7 @@ export class SimpleSynthAction implements codepipeline.IAction, iam.IGrantable {
// Hence, the pipeline will be restarted. This is necessary if the users
// adds (for example) build or test commands to the buildspec.
environmentVariables: {
...this.props.environmentVariables,
_PROJECT_CONFIG_HASH: { value: projectConfigHash },
},
project,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ export interface ShellScriptActionProps {
*/
readonly environment?: codebuild.BuildEnvironment

/**
* Environment variables to send into build
*
* @default - No additional environment variables
*/
readonly environmentVariables?: Record<string, codebuild.BuildEnvironmentVariable>;

/**
* RunOrder for this action
*
Expand Down Expand Up @@ -210,6 +217,7 @@ export class ShellScriptAction implements codepipeline.IAction, iam.IGrantable {
extraInputs: inputs.slice(1),
runOrder: this.props.runOrder ?? 100,
project: this._project,
environmentVariables: this.props.environmentVariables,
});
// Replace the placeholder actionProperties at the last minute
this._actionProperties = this._action.actionProperties;
Expand Down
54 changes: 47 additions & 7 deletions packages/@aws-cdk/pipelines/test/builds.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,49 @@ test.each([['npm'], ['yarn']])('%s assumes no build step by default', (npmYarn)
});
});

test('environmentVariables must be rendered in the action', () => {
// WHEN
new TestGitHubNpmPipeline(pipelineStack, 'Cdk', {
sourceArtifact,
cloudAssemblyArtifact,
synthAction: new cdkp.SimpleSynthAction({
sourceArtifact,
cloudAssemblyArtifact,
environmentVariables: {
VERSION: { value: codepipeline.GlobalVariables.executionId },
},
synthCommand: 'synth',
}),
});

// THEN
const theHash = Capture.aString();
expect(pipelineStack).toHaveResourceLike('AWS::CodePipeline::Pipeline', {
Stages: arrayWith({
Name: 'Build',
Actions: [
objectLike({
Name: 'Synth',
Configuration: objectLike({
EnvironmentVariables: encodedJson([
{
name: 'VERSION',
type: 'PLAINTEXT',
value: '#{codepipeline.PipelineExecutionId}',
},
{
name: '_PROJECT_CONFIG_HASH',
type: 'PLAINTEXT',
value: theHash.capture(),
},
]),
}),
}),
],
}),
});
});

test('complex setup with environemnt variables still renders correct project', () => {
// WHEN
new TestGitHubNpmPipeline(pipelineStack, 'Cdk', {
Expand Down Expand Up @@ -184,11 +227,6 @@ test('complex setup with environemnt variables still renders correct project', (
Type: 'PLAINTEXT',
Value: 'InnerValue',
},
{
Name: 'SOME_ENV_VAR',
Type: 'PLAINTEXT',
Value: 'SomeValue',
},
],
}),
Source: {
Expand Down Expand Up @@ -386,8 +424,10 @@ test('Pipeline action contains a hash that changes as the buildspec changes', ()
const hash4 = synthWithAction((sa, cxa) => cdkp.SimpleSynthAction.standardNpmSynth({
sourceArtifact: sa,
cloudAssemblyArtifact: cxa,
environmentVariables: {
xyz: { value: 'SOME-VALUE' },
environment: {
environmentVariables: {
xyz: { value: 'SOME-VALUE' },
},
},
}));

Expand Down
36 changes: 35 additions & 1 deletion packages/@aws-cdk/pipelines/test/validation.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { anything, arrayWith, deepObjectLike, encodedJson } from '@aws-cdk/assert';
import { anything, arrayWith, deepObjectLike, encodedJson, objectLike } from '@aws-cdk/assert';
import '@aws-cdk/assert/jest';
import * as codebuild from '@aws-cdk/aws-codebuild';
import * as codepipeline from '@aws-cdk/aws-codepipeline';
Expand Down Expand Up @@ -389,6 +389,40 @@ test('run ShellScriptAction with specified BuildEnvironment', () => {
});
});

test('run ShellScriptAction with specified environment variables', () => {
// WHEN
pipeline.addStage('Test').addActions(new cdkp.ShellScriptAction({
actionName: 'imageAction',
additionalArtifacts: [integTestArtifact],
commands: ['true'],
environmentVariables: {
VERSION: { value: codepipeline.GlobalVariables.executionId },
},
}));

// THEN
expect(pipelineStack).toHaveResourceLike('AWS::CodePipeline::Pipeline', {
Stages: arrayWith({
Name: 'Test',
Actions: [
objectLike({
Name: 'imageAction',
Configuration: objectLike({
EnvironmentVariables: encodedJson([
{
name: 'VERSION',
type: 'PLAINTEXT',
value: '#{codepipeline.PipelineExecutionId}',
},
]),
}),
}),
],
}),
});

});

class AppWithStackOutput extends Stage {
public readonly output: CfnOutput;

Expand Down

0 comments on commit 736b260

Please sign in to comment.