How can I disable transition in codepipeline via CDK?

Viewed 283

I am using nodejs CDK to deploy codepipeline to AWS. Below is the code:

const pipeline = new codepipeline.Pipeline(this, this.projectName, {
      pipelineName: this.projectName,
      role: this.pipelineRole,
      stages,
      artifactBucket: s3.Bucket.fromBucketName(
        this,
        'deploymentS3Bucket',
        cdk.Fn.importValue(this.s3Bucket)
      ),
    });

It has all stages defined inside stages array. The question I have is how to disable transition in one of the stage on this pipeline?

I tried below code:

const primaryDeployStage: codepipeline.CfnPipeline = pipeline.node.findChild('Approve') as codepipeline.CfnPipeline;
      const stageTransitionProperty: codepipeline.CfnPipeline.StageTransitionProperty = {
        reason: 'reason',
        stageName: 'stageName',
      };
      primaryDeployStage. addPropertyOverride('DisableInboundStageTransitions', stageTransitionProperty);

but it says no such method addOverride error.

1 Answers

As of CDK v2.1, the codepipeline.Pipeline class does not expose this property, but the Level1 CfnPipeline class it builds on does (github issue).

Option 1: Quick and dirty workaround: reach into codepipeline.Pipeline's implementation to get a reference to its CfnPipeline (this is the approach you tried):

// pipeline is a codepipeline.Pipeline
// DANGER - 'Resource' is the CfnPipeline construct's id, assigned in the Pipeline's constructor implementation
const cfnPipeline = pipeline.node.findChild('Resource') as codepipeline.CfnPipeline;

cfnPipeline.addPropertyOverride('DisableInboundStageTransitions', [
  {
    StageName: 'Stage2',
    Reason: 'No particular reason',
  },
]);

Option 2: instantiate a Level1 CfnPipeline, which accepts a disableInboundStageTransitions prop.

// CfnPipelineProps
disableInboundStageTransitions: [{
  reason: 'reason',
  stageName: 'stageName',
}],

Edit: Explain that Resource is the name of the CfnPipeline child node

We disable stage transitions by passing stage names to a L1 CfnPipeline. Approach #2 does this directly by creating one. But we'd rather use a L2 Pipeline, because it's easier. This is Approach #1, the one you are taking. Lucky for us, our pipeline has a CfnPipeline child node named 'Resource'. How do we know this? We look in the Pipeline constructor's source code on github. Once we have a reference to the CfnPipeline using pipeline.node.findChild('Resource'), we add the disabled stages to it as a property override, in the same {StageName: Reason:} format as in #2.

Related