Skip to content

Commit 719edfc

Browse files
authored
fix(pipelines): specifying the Action Role for CodeBuild steps (#18293)
This fix should address the issue #18291 fixes #18291 ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 7e824ab commit 719edfc

File tree

3 files changed

+88
-1
lines changed

3 files changed

+88
-1
lines changed

packages/@aws-cdk/pipelines/lib/codepipeline/codebuild-step.ts

+15
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,13 @@ export interface CodeBuildStepProps extends ShellStepProps {
6666
*/
6767
readonly role?: iam.IRole;
6868

69+
/**
70+
* Custom execution role to be used for the Code Build Action
71+
*
72+
* @default - A role is automatically created
73+
*/
74+
readonly actionRole?: iam.IRole;
75+
6976
/**
7077
* Changes to environment
7178
*
@@ -146,6 +153,13 @@ export class CodeBuildStep extends ShellStep {
146153
*/
147154
public readonly role?: iam.IRole;
148155

156+
/**
157+
* Custom execution role to be used for the Code Build Action
158+
*
159+
* @default - A role is automatically created
160+
*/
161+
readonly actionRole?: iam.IRole;
162+
149163
/**
150164
* Build environment
151165
*
@@ -183,6 +197,7 @@ export class CodeBuildStep extends ShellStep {
183197
this.vpc = props.vpc;
184198
this.subnetSelection = props.subnetSelection;
185199
this.role = props.role;
200+
this.actionRole = props.actionRole;
186201
this.rolePolicyStatements = props.rolePolicyStatements;
187202
this.securityGroups = props.securityGroups;
188203
this.timeout = props.timeout;

packages/@aws-cdk/pipelines/lib/codepipeline/private/codebuild-factory.ts

+9
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ export interface CodeBuildFactoryProps {
4444
*/
4545
readonly role?: iam.IRole;
4646

47+
/**
48+
* Custom execution role to be used for the Code Build Action
49+
*
50+
* @default - A role is automatically created
51+
*/
52+
readonly actionRole?: iam.IRole;
53+
4754
/**
4855
* If true, the build spec will be passed via the Cloud Assembly instead of rendered onto the Project
4956
*
@@ -145,6 +152,7 @@ export class CodeBuildFactory implements ICodePipelineActionFactory {
145152
const factory = CodeBuildFactory.fromShellStep(constructId, step, {
146153
projectName: step.projectName,
147154
role: step.role,
155+
actionRole: step.actionRole,
148156
...additional,
149157
projectOptions: mergeCodeBuildOptions(additional?.projectOptions, {
150158
buildEnvironment: step.buildEnvironment,
@@ -322,6 +330,7 @@ export class CodeBuildFactory implements ICodePipelineActionFactory {
322330
outputs: outputArtifacts,
323331
project,
324332
runOrder: options.runOrder,
333+
role: this.props.actionRole,
325334
variablesNamespace: options.variablesNamespace,
326335

327336
// Inclusion of the hash here will lead to the pipeline structure for any changes

packages/@aws-cdk/pipelines/test/codepipeline/codebuild-step.test.ts

+64-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Template, Match } from '@aws-cdk/assertions';
2+
import * as iam from '@aws-cdk/aws-iam';
23
import { Duration, Stack } from '@aws-cdk/core';
34
import * as cdkp from '../../lib';
45
import { PIPELINE_ENV, TestApp, ModernTestGitHubNpmPipeline, AppWithOutput } from '../testhelpers';
@@ -143,6 +144,68 @@ test('envFromOutputs works even with very long stage and stack names', () => {
143144
// THEN - did not throw an error about identifier lengths
144145
});
145146

147+
test('role passed it used for project and code build action', () => {
148+
const projectRole = new iam.Role(
149+
pipelineStack,
150+
'ProjectRole',
151+
{
152+
roleName: 'ProjectRole',
153+
assumedBy: new iam.ServicePrincipal('codebuild.amazon.com'),
154+
},
155+
);
156+
const buildRole = new iam.Role(
157+
pipelineStack,
158+
'BuildRole',
159+
{
160+
roleName: 'BuildRole',
161+
assumedBy: new iam.ServicePrincipal('codebuild.amazon.com'),
162+
},
163+
);
164+
// WHEN
165+
new cdkp.CodePipeline(pipelineStack, 'Pipeline', {
166+
synth: new cdkp.CodeBuildStep('Synth', {
167+
commands: ['/bin/true'],
168+
input: cdkp.CodePipelineSource.gitHub('test/test', 'main'),
169+
role: projectRole,
170+
actionRole: buildRole,
171+
}),
172+
});
173+
174+
// THEN
175+
Template.fromStack(pipelineStack).hasResourceProperties('AWS::CodeBuild::Project', {
176+
ServiceRole: {
177+
'Fn::GetAtt': [
178+
'ProjectRole5B707505',
179+
'Arn',
180+
],
181+
},
182+
});
183+
184+
expect(pipelineStack).toHaveResourceLike('AWS::CodePipeline::Pipeline', {
185+
Stages: [
186+
// source stage
187+
{},
188+
// build stage,
189+
{
190+
Actions: [
191+
{
192+
ActionTypeId: {
193+
Category: 'Build',
194+
Owner: 'AWS',
195+
Provider: 'CodeBuild',
196+
},
197+
RoleArn: {
198+
'Fn::GetAtt': [
199+
'BuildRole41B77417',
200+
'Arn',
201+
],
202+
},
203+
},
204+
],
205+
},
206+
],
207+
});
208+
});
146209
test('exportedVariables', () => {
147210
const pipeline = new ModernTestGitHubNpmPipeline(pipelineStack, 'Cdk');
148211

@@ -207,4 +270,4 @@ test('exportedVariables', () => {
207270
})),
208271
},
209272
});
210-
});
273+
});

0 commit comments

Comments
 (0)