How to add more steps before CDK Pipelines' synth step?

0

Hello,

I'm working on a application deployed as a CDK Application, and I'm looking to leverage AWS CDK Pipelines for CI/CD automation.

I have multiple modules in my application, and before the synthesis phase, I need to build the source code of each module. I want these build operations to be performed in parallel for efficiency.

The examples provided in the CDK Pipeline documentation only demonstrate that it can be configured via "synth" property.

    const pipeline = new pipelines.CodePipeline(this, 'Pipeline', {
      synth: new pipelines.ShellStep('Synth', {
        input: pipelines.CodePipelineSource.connection('my-org/my-app', 'main', {
          connectionArn: 'arn:aws:codestar-connections:us-east-1:222222222222:connection/7d2469ff-514a-4e4f-9003-5ca4a43cdc41', // Created using the AWS console * });',
        }),
        commands: [
          'npm ci',
          'npm run build',
          'npx cdk synth',
        ],
      }),
    });

How can one extend CDK Pipelines with additional stage that can be run before "synth" and perform necessary operations on the source code - build, unit testing etc., ideally in parallel, and the output will serve as an input to the "synth" step ?

The pipeline should look something like on the picture. Build stage is created by Pipeline and I would like to add a custom ModuleBuild stage where I can perform necessary build steps for each module. Enter image description here

Thanks

2 Answers
1
Accepted Answer

Found solution myself. Posting the answer here in case it might be useful.

Option 1

It is possible to do it by using an existing pipeline as described in the documentation. https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.pipelines-readme.html#using-an-existing-aws-codepipeline

One can create pre Synth stages and have more fine grained control over the build process.

Example from the documentation:

declare const codePipeline: codepipeline.Pipeline;

const sourceArtifact = new codepipeline.Artifact('MySourceArtifact');

const pipeline = new pipelines.CodePipeline(this, 'Pipeline', {
  codePipeline: codePipeline,
  synth: new pipelines.ShellStep('Synth', {
    input: pipelines.CodePipelineFileSet.fromArtifact(sourceArtifact),
    commands: ['npm ci','npm run build','npx cdk synth'],
  }),
});

Option 2

It turned out that additionalInputs accept Step as a source. It is mentioned in the documentation as well

https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.pipelines-readme.html#synth-and-sources

Example from the documentation:

const prebuild = new pipelines.ShellStep('Prebuild', {
  input: pipelines.CodePipelineSource.gitHub('myorg/repo1', 'main'),
  primaryOutputDirectory: './build',
  commands: ['./build.sh'],
});

const pipeline = new pipelines.CodePipeline(this, 'Pipeline', {
  synth: new pipelines.ShellStep('Synth', {
    input: pipelines.CodePipelineSource.gitHub('myorg/repo2', 'main'),
    additionalInputs: {
      'subdir': pipelines.CodePipelineSource.gitHub('myorg/repo3', 'main'),
      '../siblingdir': prebuild,
    },

    commands: ['./build.sh'],
  })
});

Option 2 is what I ended up with since it looks cleaner.

VasylK
answered 10 months ago
0

Hi, cdk --build option seems to be what you are looking for

See https://github.com/aws/aws-cdk/commit/eb9b8e23906e2e1375f45f795d71b905bc0a52af

Or type 'cdk build --help' on your laptop

profile pictureAWS
EXPERT
answered a year ago
  • Thanks for the reply, @Didier_AWS. But it does not look like something I am after. I have updated the question .

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions