From bf47ebbd13408245cd442c6707e4f2d49f3d190e Mon Sep 17 00:00:00 2001 From: Ian Gilham Date: Wed, 27 Oct 2021 16:06:07 +0100 Subject: [PATCH 1/4] feat(pipelines): add support for CodePipeline variables - Add variables namespace to CodeBuildStep - Add `variable` function to get a fully qualified CodePipeline variable name from CodeBuildStep instances - Add support for variables namespace to CodeBuild factory Refer to #15964 for a previous attempt by @berenddeboer, from which I've copied some implementation details. Fixes #15943 Partially addresses #16407 --- .../lib/codepipeline/_codebuild-factory.ts | 5 ++++- .../lib/codepipeline/codebuild-step.ts | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/packages/@aws-cdk/pipelines/lib/codepipeline/_codebuild-factory.ts b/packages/@aws-cdk/pipelines/lib/codepipeline/_codebuild-factory.ts index fb523cf7d6818..d805845fb5c1a 100644 --- a/packages/@aws-cdk/pipelines/lib/codepipeline/_codebuild-factory.ts +++ b/packages/@aws-cdk/pipelines/lib/codepipeline/_codebuild-factory.ts @@ -98,6 +98,7 @@ export interface CodeBuildFactoryProps { readonly env?: Record; readonly envFromCfnOutputs?: Record; + readonly variablesNamespace?: string; /** * If given, override the scope from the produce call with this scope. @@ -146,6 +147,7 @@ export class CodeBuildFactory implements ICodePipelineActionFactory { subnetSelection: step.subnetSelection, ...additional?.projectOptions, }, + variablesNamespace: step.variablesNamespace, ...additional, }); @@ -308,6 +310,7 @@ export class CodeBuildFactory implements ICodePipelineActionFactory { outputs: outputArtifacts, project, runOrder: options.runOrder, + variablesNamespace: this.props.variablesNamespace, // Inclusion of the hash here will lead to the pipeline structure for any changes // made the config of the underlying CodeBuild Project. @@ -510,4 +513,4 @@ function filterBuildSpecCommands(buildSpec: codebuild.BuildSpec, osType: ec2.Ope } return [undefined, x]; } -} \ No newline at end of file +} diff --git a/packages/@aws-cdk/pipelines/lib/codepipeline/codebuild-step.ts b/packages/@aws-cdk/pipelines/lib/codepipeline/codebuild-step.ts index 3047b43c5c755..3e47a792e6be3 100644 --- a/packages/@aws-cdk/pipelines/lib/codepipeline/codebuild-step.ts +++ b/packages/@aws-cdk/pipelines/lib/codepipeline/codebuild-step.ts @@ -130,6 +130,13 @@ export class CodeBuildStep extends ShellStep { */ public readonly role?: iam.IRole; + /** + * The name of the namespace to use for variables emitted by this action. + * + * @default The projectName will be used if it is defined. Otherwise, the construct ID will be used. + */ + public readonly variablesNamespace: string; + /** * Build environment * @@ -155,6 +162,7 @@ export class CodeBuildStep extends ShellStep { this.vpc = props.vpc; this.subnetSelection = props.subnetSelection; this.role = props.role; + this.variablesNamespace = this.projectName ?? this.id; this.rolePolicyStatements = props.rolePolicyStatements; this.securityGroups = props.securityGroups; } @@ -178,6 +186,17 @@ export class CodeBuildStep extends ShellStep { return this.project.grantPrincipal; } + /** + * Reference a CodePipeline variable defined by the CodeBuildStep. + * + * Variables in CodeBuild actions are defined using the 'exported-variables' subsection of the 'env' section of the buildspec. + * + * @param variableName the name of the variable for reference. + */ + public variable(variableName: string): string { + return `#{${this.variablesNamespace}.${variableName}}`; + } + /** * Set the internal project value * From abb7b5186f8f5bbb47a238a8705d94c0204b97b7 Mon Sep 17 00:00:00 2001 From: Ian Gilham Date: Wed, 27 Oct 2021 17:42:27 +0100 Subject: [PATCH 2/4] test(pipelines): add namespaces to existing integration test cases --- .../pipelines/test/integ.newpipeline-with-vpc.expected.json | 6 +++++- .../@aws-cdk/pipelines/test/integ.newpipeline.expected.json | 3 ++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/packages/@aws-cdk/pipelines/test/integ.newpipeline-with-vpc.expected.json b/packages/@aws-cdk/pipelines/test/integ.newpipeline-with-vpc.expected.json index 80a17d7243cb1..57b3f445a8fcc 100644 --- a/packages/@aws-cdk/pipelines/test/integ.newpipeline-with-vpc.expected.json +++ b/packages/@aws-cdk/pipelines/test/integ.newpipeline-with-vpc.expected.json @@ -795,6 +795,7 @@ } ], "Name": "Synth", + "NameSpace": "Synth", "OutputArtifacts": [ { "Name": "Synth_Output" @@ -832,6 +833,7 @@ } ], "Name": "SelfMutate", + "NameSpace": "SelfMutate", "RoleArn": { "Fn::GetAtt": [ "PipelineUpdatePipelineSelfMutateCodePipelineActionRoleD6D4E5CF", @@ -863,6 +865,7 @@ } ], "Name": "FileAsset1", + "Namespace": "FileAsset1", "RoleArn": { "Fn::GetAtt": [ "PipelineAssetsFileAsset1CodePipelineActionRoleC0EC649A", @@ -889,6 +892,7 @@ } ], "Name": "FileAsset2", + "Namespace": "FileAsset2", "RoleArn": { "Fn::GetAtt": [ "PipelineAssetsFileAsset2CodePipelineActionRole06965A59", @@ -2398,4 +2402,4 @@ ] } } -} \ No newline at end of file +} diff --git a/packages/@aws-cdk/pipelines/test/integ.newpipeline.expected.json b/packages/@aws-cdk/pipelines/test/integ.newpipeline.expected.json index 935ad4ce5136a..5bc6074fd73cd 100644 --- a/packages/@aws-cdk/pipelines/test/integ.newpipeline.expected.json +++ b/packages/@aws-cdk/pipelines/test/integ.newpipeline.expected.json @@ -298,6 +298,7 @@ } ], "Name": "SelfMutate", + "Namespace": "SelfMutate", "RoleArn": { "Fn::GetAtt": [ "PipelineUpdatePipelineSelfMutateCodePipelineActionRoleD6D4E5CF", @@ -2333,4 +2334,4 @@ ] } } -} \ No newline at end of file +} From 479a2a94afd45b9ece31f9957d93ec83e0ddb6d2 Mon Sep 17 00:00:00 2001 From: Ian Gilham Date: Thu, 28 Oct 2021 11:37:39 +0100 Subject: [PATCH 3/4] fix(pipelines): test data capitalisation --- .../pipelines/test/integ.newpipeline-with-vpc.expected.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/@aws-cdk/pipelines/test/integ.newpipeline-with-vpc.expected.json b/packages/@aws-cdk/pipelines/test/integ.newpipeline-with-vpc.expected.json index 57b3f445a8fcc..5e3badf4b4daa 100644 --- a/packages/@aws-cdk/pipelines/test/integ.newpipeline-with-vpc.expected.json +++ b/packages/@aws-cdk/pipelines/test/integ.newpipeline-with-vpc.expected.json @@ -795,7 +795,7 @@ } ], "Name": "Synth", - "NameSpace": "Synth", + "Namespace": "Synth", "OutputArtifacts": [ { "Name": "Synth_Output" @@ -833,7 +833,7 @@ } ], "Name": "SelfMutate", - "NameSpace": "SelfMutate", + "Namespace": "SelfMutate", "RoleArn": { "Fn::GetAtt": [ "PipelineUpdatePipelineSelfMutateCodePipelineActionRoleD6D4E5CF", From 03e98612aa5ccd6ee8439e6c92d7c0d1a5810316 Mon Sep 17 00:00:00 2001 From: Ian Gilham Date: Thu, 28 Oct 2021 12:22:29 +0100 Subject: [PATCH 4/4] test(pipelines): undo added namespace field in test data --- .../pipelines/test/integ.newpipeline-with-vpc.expected.json | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/@aws-cdk/pipelines/test/integ.newpipeline-with-vpc.expected.json b/packages/@aws-cdk/pipelines/test/integ.newpipeline-with-vpc.expected.json index 5e3badf4b4daa..59a6d0034efce 100644 --- a/packages/@aws-cdk/pipelines/test/integ.newpipeline-with-vpc.expected.json +++ b/packages/@aws-cdk/pipelines/test/integ.newpipeline-with-vpc.expected.json @@ -795,7 +795,6 @@ } ], "Name": "Synth", - "Namespace": "Synth", "OutputArtifacts": [ { "Name": "Synth_Output"