@@ -44,33 +44,84 @@ export class PipelineBuildAction extends codepipeline.BuildAction {
4444 // https://qiita.com/ikeisuke/items/2fbc0b80b9bbd981b41f
4545
4646 super ( parent , name , {
47- stage : props . stage ,
48- runOrder : props . runOrder ,
4947 provider : 'CodeBuild' ,
50- inputArtifact : props . inputArtifact ,
51- outputArtifactName : props . outputArtifactName ,
5248 configuration : {
53- ProjectName : props . project . projectName
54- }
49+ ProjectName : props . project . projectName ,
50+ } ,
51+ ...props ,
5552 } ) ;
5653
57- const actions = [
54+ setCodeBuildNeededPermissions ( props . stage , props . project , true ) ;
55+ }
56+ }
57+
58+ /**
59+ * Common properties for creating {@link PipelineTestAction} -
60+ * either directly, through its constructor,
61+ * or through {@link ProjectRef#addTestToPipeline}.
62+ */
63+ export interface CommonPipelineTestActionProps extends codepipeline . CommonActionProps {
64+ /**
65+ * The source to use as input for this test.
66+ *
67+ * @default CodePipeline will use the output of the last Action from a previous Stage as input
68+ */
69+ inputArtifact ?: codepipeline . Artifact ;
70+
71+ /**
72+ * The optional name of the output artifact.
73+ * If you provide a value here,
74+ * then the `outputArtifact` property of your Action will be non-null.
75+ * If you don't, `outputArtifact` will be `null`.
76+ *
77+ * @default the Action will not have an output artifact
78+ */
79+ outputArtifactName ?: string ;
80+ }
81+
82+ /**
83+ * Construction properties of the {@link PipelineTestAction CodeBuild test CodePipeline Action}.
84+ */
85+ export interface PipelineTestActionProps extends CommonPipelineTestActionProps ,
86+ codepipeline . CommonActionConstructProps {
87+ /**
88+ * The build Project.
89+ */
90+ project : ProjectRef ;
91+ }
92+
93+ export class PipelineTestAction extends codepipeline . TestAction {
94+ constructor ( parent : cdk . Construct , name : string , props : PipelineTestActionProps ) {
95+ super ( parent , name , {
96+ provider : 'CodeBuild' ,
97+ configuration : {
98+ ProjectName : props . project . projectName ,
99+ } ,
100+ ...props ,
101+ } ) ;
102+
103+ // the Action needs write permissions only if it's producing an output artifact
104+ setCodeBuildNeededPermissions ( props . stage , props . project , ! ! props . outputArtifactName ) ;
105+ }
106+ }
107+
108+ function setCodeBuildNeededPermissions ( stage : codepipeline . IStage , project : ProjectRef ,
109+ needsPipelineBucketWrite : boolean ) {
110+ // grant the Pipeline role the required permissions to this Project
111+ stage . pipelineRole . addToPolicy ( new iam . PolicyStatement ( )
112+ . addResource ( project . projectArn )
113+ . addActions (
58114 'codebuild:BatchGetBuilds' ,
59115 'codebuild:StartBuild' ,
60116 'codebuild:StopBuild' ,
61- ] ;
117+ ) ) ;
62118
63- props . stage . pipelineRole . addToPolicy ( new iam . PolicyStatement ( )
64- . addResource ( props . project . projectArn )
65- . addActions ( ...actions ) ) ;
66-
67- // allow codebuild to read and write artifacts to the pipline's artifact bucket.
68- if ( props . project . role ) {
69- props . stage . grantPipelineBucketReadWrite ( props . project . role ) ;
119+ // allow the Project access to the Pipline's artifact Bucket
120+ if ( project . role ) {
121+ if ( needsPipelineBucketWrite ) {
122+ stage . grantPipelineBucketReadWrite ( project . role ) ;
123+ } else {
124+ stage . grantPipelineBucketRead ( project . role ) ;
70125 }
71-
72- // policy must be added as a dependency to the pipeline!!
73- // TODO: grants - build.addResourcePermission() and also make sure permission
74- // includes the pipeline role AWS principal.
75126 }
76127}
0 commit comments