0

Let's imagine that we have one CodePipeline with 2 stages in the following fashion:

new codepipeline.Pipeline(this, name + "Pipeline", {
    pipelineName: this.projectName + "-" + name,
    crossAccountKeys: false,
    stages: [{
        stageName: 'Source',
        actions: [codeCommitSourceAction]
    },{
        stageName: 'Build',
        actions: [buildAction]
    }]
});

Here the Source stage is where we pull the changes from the repository and the Build one is a CodeBuild project which has the following actions in the buildspec file:

  1. Install the dependencies (npm i).
  2. Run the tests (npm run test).
  3. Pack the project (npm run pack).
  4. Update/deploy lambda function (aws lambda update-function-code).

In general it does what it supposed to do, however, if the build fails, the only way to find out, which part has failed, is to look to the logs. I would like that it is seen straight from CodePipeline. In this case CodePipeline must have more stages which correlate with each action from CodeBuild. Based on my experience I can do it if for every stage I provide different CodeBuild project.

Question: can I provide same CodeBuild project to the different CodePipeline stages so, that it will execute only part of buildspec file (for example, only running the tests)?

Rufi
  • 2,529
  • 1
  • 20
  • 41

2 Answers2

1

You can have your buildspec.yml perform different actions based on environment variables. You can then pass different environment variables to CodeBuildAction with environmentVariables.

new codepipeline_actions.CodeBuildAction({
  actionName: 'Build',
  project: buildProject,
  input: sourceInput,
  runOrder: 1,
  environmentVariables: {
    STEP: { value: 'test' }
  }
}),

And then check STEP environment variable in buildspec.yml.

kichik
  • 33,220
  • 7
  • 94
  • 114
  • yes, that worked, thanks! In case somebody searched how to use environment variable in JSON format, it is here: https://stackoverflow.com/questions/62881332/conditional-statement-in-codebuild-commands-json/70493852#70493852 – Rufi Dec 27 '21 at 10:03
0

Question: can I provide same CodeBuild project to the different CodePipeline stages so, that it will execute only part of buildspec file (for example, only running the tests)?

No, I don't think that is possible.

What you can do however, is to have different buildspecs.yml file called at different stages of your pipeline.

For example, you could have a Codepipeline stage called Init which will call the builspec_init.yml of your project. If that succeed, you could have a following state Apply calling the buildspec_apply.yml file of your project.

Mornor
  • 3,471
  • 8
  • 31
  • 69