AWS::CodePipeline::Pipeline - RestartExecutionOnUpdate - Pipeline is not restarted after modification of AWS::CodeBuild::Project Buildspe

0

Looking for guidance on the CodePipeline feature to RestartExecutionOnUpdate. Using the cdk we are able to modify the pipeline, however, when the modification changes a buildspec within a CodeBuild action, then the pipeline is not restarted. Is this expected behavior?

In the example below, the CodePipeline has been deployed with the cdk constructs. Then in a subsequent commit a new change occurs within a buildspec and is later redeployed by the pipeline, however the pipeline does not restart for a codebuild buildspec change Example code:

const pipeline = new Pipeline(this, 'Pipeline-' + branch, {
      restartExecutionOnUpdate: true,
      pipelineName: repo_name + branch,
    } )
    
    // add a source stage
    const sourceStage = pipeline.addStage({
      stageName: 'Source',
    });
    const sourceOutput = new Artifact();
    const sourceAction = new CodeStarConnectionsSourceAction({
      runOrder: 1,
      actionName: 'BitBucket_Source',
      owner: 'funsystems',
      repo: repo_name,
      branch: branch,
      output: sourceOutput,
      connectionArn: 'arn:aws:codestar-connections:us-east-2:bla',
    });

    sourceStage.addAction(sourceAction)

    const lightCheckProject = new PipelineProject(this, `LightCheckProject`, {
      projectName: `${repo_name}-${branch}-LightCheck`,
      buildSpec: BuildSpec.fromObject({
        version: '0.2',
        phases: {
          build: {
            commands:[
              'mkdir results',
              'echo "some test results" >> results/test_one.txt',  // existing 
              'echo "some test results" >> results/test_two.txt',  // added and deployed in a later stage of the pipeline
            ],
          },
        },
      }),
    });

    // add a stage
    const buildStage = pipeline.addStage({ stageName: 'Build' });
    const codeBuildAction = new CodeBuildAction({
      role: codepipelineRole,
      project: lightCheckProject,
      actionName: "unit",
      input: sourceOutput,
    })
    buildStage.addAction(codeBuildAction)
2 Answers
1

I believe the restartExecutionOnUpdate property means that it will restart execution when the CodePipeline resource itself is updated; that is, a cdk deploy takes place that changes the CodePipeline.

In the snippet you've provided, it looks like you are using CodeStar connections to pull from Bitbucket. Changes will need to occur on the BitBucket repo and branch indicated to trigger a new CodePipeline deploy, assuming the connection is correctly established.

I am not familiar with the construct, but I suspect that changing the PipelineProject and the included build spec won't restart execution because that will result in changes to Code Build, no the CodePipeline.

profile picture
answered a year ago
  • Thanks for your feedback, although I am still unsure of the expected behavior and perhaps wasnt fully clear in my description.

    I am deploying in fact a Pipeline construct, and PipelineProject is the opinionated CodeBuild Project for CodePipelines. In addition, there is a step in the pipeline to do the self update (I purposely left this out for brevity in the example), eg cdk synth and deploy, and synth shows a change in the cloud formation pipeline construct. Furthermore my pipeline does in fact update itself but never restarts.

1

When the restartExecutionOnUpdate flag is set to True, then any update to the CodePipeline Structure will invoke a restart on the Pipeline. With that being said, this property is only for the CodePipeline Structure itself. This includes the JSON content of the pipeline which can be observed through the GetPipeline API. From the information you provided, it is seen that you are performing an update to the buildspec which is a property of the CodeBuild Project, not the Pipeline.

An update to the buildspec will not change the version of the CodePipeline (e.g. from version 1 to 2), as the pipeline structure will not change. Because the structure is not changing, the restart will not occur.

However, there is a workaround to restart the Pipeline when there is a change to the buildspec which is via the use of 'EnvironmentVariables'. Can add an environment variable with a unique name, not used by the build itself. Its sole purpose is to make the pipeline definition change when the buildspec changes, causing a pipeline restart.

Updating the CodeBuild Action's environment variable should work as it is apart of the CodePipeline structure. As long you are not modifying the CodeBuild Project's environment variable as this is on the project level.

answered a year ago
  • Thanks for you explanation, but I was hoping this would not be the case. My main construct is a cdk pipeline so I'd assume anything changing on this construct would change constitute a restart.

    Even for "environmentVariables", which like "buildspec" is also a field of PipelineProject, I would expect a restart. So why does one work without the other. This still feels like a bug.

    With the main construct being a pipeline I would say "any" change which causes a new deployment should cause a restart, eg cdk diff.

    I do like you creative work around but still seems like a bug.

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