简体   繁体   中英

How to specify AWS pipeline as source provider for codebuild project in CDK?

When I create a codebuild project in AWS console, I can select AWS CodePipeline as source provider. See below screenshot.

在此处输入图像描述

But in CDK, https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-codebuild.Source.html , I can't find how I can specify AWS CodePipeline as source provider. How can I achieve it in CDK?

Seems like you assign a codebuild action to the codepipeline instead of adding a codepipeline to the codebuild project source.

https://docs.aws.amazon.com/cdk/api/latest/docs/aws-codebuild-readme.html#codepipeline

To add a CodeBuild Project as an Action to CodePipeline, use the PipelineProject class instead of Project. It's a simple class that doesn't allow you to specify sources, secondarySources, artifacts or secondaryArtifacts, as these are handled by setting input and output CodePipeline Artifact instances on the Action, instead of setting them on the Project.

https://docs.aws.amazon.com/cdk/api/latest/docs/aws-codepipeline-actions-readme.html#build--test

Example of a CodeBuild Project used in a Pipeline, alongside CodeCommit:

import * as codebuild from '@aws-cdk/aws-codebuild';
import * as codecommit from '@aws-cdk/aws-codecommit';
import * as codepipeline_actions from '@aws-cdk/aws-codepipeline-actions';

const repository = new codecommit.Repository(this, 'MyRepository', {
  repositoryName: 'MyRepository',
});
const project = new codebuild.PipelineProject(this, 'MyProject');

const sourceOutput = new codepipeline.Artifact();
const sourceAction = new codepipeline_actions.CodeCommitSourceAction({
  actionName: 'CodeCommit',
  repository,
  output: sourceOutput,
});
const buildAction = new codepipeline_actions.CodeBuildAction({
  actionName: 'CodeBuild',
  project,
  input: sourceOutput,
  outputs: [new codepipeline.Artifact()], // optional
  executeBatchBuild: true // optional, defaults to false
});

new codepipeline.Pipeline(this, 'MyPipeline', {
  stages: [
    {
      stageName: 'Source',
      actions: [sourceAction],
    },
    {
      stageName: 'Build',
      actions: [buildAction],
    },
  ],
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM