Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(pipelines): specifying the Action Role for CodeBuild steps #18293

Merged
merged 6 commits into from May 19, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 15 additions & 0 deletions packages/@aws-cdk/pipelines/lib/codepipeline/codebuild-step.ts
Expand Up @@ -66,6 +66,13 @@ export interface CodeBuildStepProps extends ShellStepProps {
*/
readonly role?: iam.IRole;

/**
* Custom execution role to be used for the Code Build Action
*
* @default - A role is automatically created
*/
readonly actionRole?: iam.IRole;

/**
* Changes to environment
*
Expand Down Expand Up @@ -146,6 +153,13 @@ export class CodeBuildStep extends ShellStep {
*/
public readonly role?: iam.IRole;

/**
* Custom execution role to be used for the Code Build Action
*
* @default - A role is automatically created
*/
readonly actionRole?: iam.IRole;

/**
* Build environment
*
Expand Down Expand Up @@ -183,6 +197,7 @@ export class CodeBuildStep extends ShellStep {
this.vpc = props.vpc;
this.subnetSelection = props.subnetSelection;
this.role = props.role;
this.actionRole = props.actionRole;
this.rolePolicyStatements = props.rolePolicyStatements;
this.securityGroups = props.securityGroups;
this.timeout = props.timeout;
Expand Down
Expand Up @@ -44,6 +44,13 @@ export interface CodeBuildFactoryProps {
*/
readonly role?: iam.IRole;

/**
* Custom execution role to be used for the Code Build Action
*
* @default - A role is automatically created
*/
readonly actionRole?: iam.IRole;

/**
* If true, the build spec will be passed via the Cloud Assembly instead of rendered onto the Project
*
Expand Down Expand Up @@ -145,6 +152,7 @@ export class CodeBuildFactory implements ICodePipelineActionFactory {
const factory = CodeBuildFactory.fromShellStep(constructId, step, {
projectName: step.projectName,
role: step.role,
actionRole: step.actionRole,
...additional,
projectOptions: mergeCodeBuildOptions(additional?.projectOptions, {
buildEnvironment: step.buildEnvironment,
Expand Down Expand Up @@ -322,6 +330,7 @@ export class CodeBuildFactory implements ICodePipelineActionFactory {
outputs: outputArtifacts,
project,
runOrder: options.runOrder,
role: this.props.actionRole,
variablesNamespace: options.variablesNamespace,

// Inclusion of the hash here will lead to the pipeline structure for any changes
Expand Down
@@ -1,4 +1,5 @@
import { Template, Match } from '@aws-cdk/assertions';
import * as iam from '@aws-cdk/aws-iam';
import { Duration, Stack } from '@aws-cdk/core';
import * as cdkp from '../../lib';
import { PIPELINE_ENV, TestApp, ModernTestGitHubNpmPipeline, AppWithOutput } from '../testhelpers';
Expand Down Expand Up @@ -143,6 +144,68 @@ test('envFromOutputs works even with very long stage and stack names', () => {
// THEN - did not throw an error about identifier lengths
});

test('role passed it used for project and code build action', () => {
const projectRole = new iam.Role(
pipelineStack,
'ProjectRole',
{
roleName: 'ProjectRole',
assumedBy: new iam.ServicePrincipal('codebuild.amazon.com'),
},
);
const buildRole = new iam.Role(
pipelineStack,
'BuildRole',
{
roleName: 'BuildRole',
assumedBy: new iam.ServicePrincipal('codebuild.amazon.com'),
},
);
// WHEN
new cdkp.CodePipeline(pipelineStack, 'Pipeline', {
synth: new cdkp.CodeBuildStep('Synth', {
commands: ['/bin/true'],
input: cdkp.CodePipelineSource.gitHub('test/test', 'main'),
role: projectRole,
actionRole: buildRole,
}),
});

// THEN
Template.fromStack(pipelineStack).hasResourceProperties('AWS::CodeBuild::Project', {
ServiceRole: {
'Fn::GetAtt': [
'ProjectRole5B707505',
'Arn',
],
},
});

expect(pipelineStack).toHaveResourceLike('AWS::CodePipeline::Pipeline', {
Stages: [
// source stage
{},
// build stage,
{
Actions: [
{
ActionTypeId: {
Category: 'Build',
Owner: 'AWS',
Provider: 'CodeBuild',
},
RoleArn: {
'Fn::GetAtt': [
'BuildRole41B77417',
'Arn',
],
},
},
],
},
],
});
});
test('exportedVariables', () => {
const pipeline = new ModernTestGitHubNpmPipeline(pipelineStack, 'Cdk');

Expand Down Expand Up @@ -207,4 +270,4 @@ test('exportedVariables', () => {
})),
},
});
});
});