All files pipeline-actions.ts

100% Statements 7/7
100% Branches 0/0
100% Functions 1/1
100% Lines 7/7

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60  1x 1x                                                 1x         1x                 1x           1x         1x              
import codebuild = require('@aws-cdk/aws-codebuild');
import codepipeline = require('@aws-cdk/aws-codepipeline');
import cdk = require('@aws-cdk/cdk');
 
/**
 * Construction properties of the {@link PipelineBuildAction CodeBuild build CodePipeline Action}.
 */
export interface PipelineBuildActionProps {
    /**
     * The source to use as input for this build
     */
    inputArtifact: codepipeline.Artifact;
 
    /**
     * The name of the build's output artifact
     */
    artifactName?: string;
 
    /**
     * The build project
     */
    project: codebuild.ProjectRef;
}
 
/**
 * CodePipeline build Action that uses AWS CodeBuild.
 */
export class PipelineBuildAction extends codepipeline.BuildAction {
    constructor(parent: codepipeline.Stage, name: string, props: PipelineBuildActionProps) {
        // This happened when ProjectName was accidentally set to the project's ARN:
        // https://qiita.com/ikeisuke/items/2fbc0b80b9bbd981b41f
 
        super(parent, name, {
            provider: 'CodeBuild',
            inputArtifact: props.inputArtifact,
            artifactName: props.artifactName,
            configuration: {
                ProjectName: props.project.projectName
            }
        });
 
        const actions = [
            'codebuild:BatchGetBuilds',
            'codebuild:StartBuild',
            'codebuild:StopBuild',
        ];
 
        parent.pipeline.addToRolePolicy(new cdk.PolicyStatement()
            .addResource(props.project.projectArn)
            .addActions(...actions));
 
        // allow codebuild to read and write artifacts to the pipline's artifact bucket.
        parent.pipeline.artifactBucket.grantReadWrite(props.project.role);
 
        // policy must be added as a dependency to the pipeline!!
        // TODO: grants - build.addResourcePermission() and also make sure permission
        // includes the pipeline role AWS principal.
    }
}