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

VasylK
질문됨 일 년 전1226회 조회
2개 답변
1
수락된 답변

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
답변함 일 년 전
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
전문가
답변함 일 년 전
  • Thanks for the reply, @Didier_AWS. But it does not look like something I am after. I have updated the question .

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠