tobi
tobi

Reputation: 329

AWS CDK Pipeline - action before synth

I have a standard CDK pipeline and use the output of mvn package to build a docker container. This one is used as DockerImageAsset and gets deployed to different environments. Therefore, the mvn package must run before the cdk synth.

While this works, I don't like the fact that the mvn package runs within the Synth action and I would prefer to have a separate action before, that also publishes test results from unit tests etc.

Is there a way to get an action before Synth ?

This is the current code:

const pipeline = new CodePipeline(this, 'Pipeline', {
    dockerEnabledForSynth: true,
    dockerEnabledForSelfMutation: true,
    crossAccountKeys: true,
    synth: new ShellStep('Synth', {
        input: CodePipelineSource.gitHub('OWNER/repo', 'main', {
            authentication: SecretValue.secretsManager('GITHUB_TOKEN'),
        }),
        commands: [
            './mvnw package',
            'npm ci',
            'npm run build',
            'npx cdk synth',
        ]
    })
});

...

const dockerImage = new DockerImageAsset(this, 'Image', {
    directory: '......'
});

...

Upvotes: 4

Views: 1288

Answers (1)

fenix.shadow
fenix.shadow

Reputation: 461

There is another CDK construct library for AWS CodePipeline that is lower-level and unopinionated, which should allow you to add stages before the synth stage.

There is also a method buildPipeline for the higher-level pipeline construct you are currently using (the CodePipeline construct) that allows you to build the lower-level pipeline (the Pipeline construct). Note that once you call this method you can't modify the higher-level construct anymore. After building the lower-level pipeline construct, you might be able to call addStage on it and pass in the placement property to add the stage prior to the synth stage, like so:

pipeline.buildPipeline()
const builtPipeline = pipeline.pipeline
builtPipeline.addStage({
  stageName: 'Maven',
  placement: {
    rightBefore: pipeline.stages[1]
  }
})

I have not personally tried this method so I cannot guarantee it, but if that doesn't work, then converting the CDK code to the lower-level construct library and using that from the beginning should work. From the docs for the higher-level construct library:

CDK Pipelines is an opinionated construct library....If you want or need more control, we recommend you drop down to using the aws-codepipeline construct library directly.

Upvotes: 1

Related Questions