简体   繁体   中英

AWS CDK CodePipeline add a stage between Source and Build

I followed the Continuous integration and delivery (CI/CD) using CDK Pipelines guide to implement a CodePipeline. I would like to know how to add a stage to my pipeline in CDK that will run after the Source stage but before the Build stage.

This is my pipeline code:

import * as cdk from 'aws-cdk-lib';
import { Construct } from 'constructs';
import { Repository } from 'aws-cdk-lib/aws-codecommit';
import { CodePipeline, CodePipelineSource, ShellStep } from 'aws-cdk-lib/pipelines';

export class MyPipelineStack extends cdk.Stack {
  constructor(scope: Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    const repo = Repository.fromRepositoryName(this, 'CogClientRepo', 'cog-client');

    const pipeline = new CodePipeline(this, 'Pipeline', {
      pipelineName: 'MyPipeline',
      synth: new ShellStep('Synth', {
        input: CodePipelineSource.codeCommit(repo, 'main'),
        commands: ['npm ci', 'npm run build', 'npx cdk synth']
      })
    });
  }
}

After running cdk deploy I see that I can add a stage betwen Source and Build using the console, but I would like this to be a part of the CDK code. 显示两个阶段的 AWS CodePipeline 控制台

CDK version 2.3.0 written in TypeScript
I am using the more recent aws-cdk-lib.pipelines module and not the aws-cdk-lib.aws_codepipeline module.

So, the way CDK figures out where to put the actions you create, is by their inputs and outputs. To add an action between the source and the build, you would need to create an action (or a sequence of actions) that take the source output as input, and produce an output that's used by the synth step as input.

Here's an example in Python, it works the same way in TS:

my_source_action = CodePipelineSource.code_commit(repo, "main")

my_intermediary_action = CodeBuildStep("myAction", input=my_source_action)

my_synth_action = ShellStep(
    "Synth",
    input=my_intermediary_action,
    commands=['...']
)

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