8888
8888

Reputation: 2064

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 console showing two stages

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.

Upvotes: 4

Views: 4082

Answers (1)

gshpychka
gshpychka

Reputation: 11512

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=['...']
)

Upvotes: 7

Related Questions