Home > Software design >  AWS CodePipeline CDK TypeScript
AWS CodePipeline CDK TypeScript

Time:01-25

The install commands in the Build Step of the AWS CodePipeline do not update when there are changes made in the AWS CDK Code (TypeScript) and are pushed to the repository. The Buildspec section under the Build details of the project has the same configuration as when it was created.

Is there a way to fix it? We've made some changes to the BuildStep CDK but does not take effect on the AWS CodeBuild configuration details. I'm only new to AWS CodeBuild and CodePipeline. Any answer/suggestion would be a great help.

Sample Code

const pipeline = new CodePipeline(this, 'SamplePipeline', {
    pipelineName: 'SamplePipeline',
    synth: new CodeBuildStep('BuildSynthStep', {
            input: source,
            buildEnvironment: {
                buildImage: codebuild.LinuxBuildImage.STANDARD_5_0
            },
            installCommands: [
                'install_command_1',
                'install_command_2',
                ...
                'install_command_n'
            ],
            commands: [
                'command_2',
                ...
                'command_n'
            ],
        }
    )
});

Artifact Provider: Amazon S3

CodePudding user response:

The self-mutation of a CDK Pipeline is only applied, when you change something on an application stage (precisely CDK Stage) or other phases after the synth codebuild job.

If you have something running before, e.g. unit tests, then you won't get into the self-update job.

So, what are your options now?

Well, changes according to a pipeline itself are mostly done manually. So you have to run a cdk deploy PipelineStack with your changes committed to the source branch aside.

CodePudding user response:

You can do something like this with 3 steps. ( Since CDK reads this file during runtime, any updates will also be picked up by CDK) :-

  • Create a build spec file.

  • Create a codebuild constructor

  • Consume that constructor and pass your build-spec file to BuildSpec.fromObject method.

Create a buildspec directory under the root of your project and create a file of any of your choice for this example plan-build spec.yml

version: 0.2
env:
  variables:
    

phases:
  install:
    commands:
      - echo "hello"
  pre_build:
    commands:
      - printenv
      

  build:
    commands:
      - echo 'in build stage'
    

This will be the constructor for your codebuild project

this.project = new codebuild.PipelineProject(this, 'codebuild-project', {
        projectName: props.projectName,
        description: props.description, // a Bucket used as a source in CodePipeline must be versioned
        environment: {
            computeType: ComputeType.SMALL,
            buildImage: LinuxBuildImage.AMAZON_LINUX_2_4
        },
        buildSpec: props.buildspec,

    });
}

This will be the code where you will consume that constructor in another file.

import { codeBuildProject } from '../lib/constructs/pipeline-codebuild-project'
     const TfPlan = new codeBuildProject(this, 'project', {
          projectName: 'some name',
          description: 'project description',
          buildspec: codebuild.BuildSpec.fromObject(yaml.parse(fs.readFileSync('buildspec/plan-buildspec.yml', 'utf8'))),
        })
  • Related