1

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.

Joey Yi Zhao
  • 37,514
  • 71
  • 268
  • 523
  • Assuming this transition is not disabled forever, have you considered a manual approval step for this use case? – kichik Dec 17 '21 at 19:32

1 Answers1

1

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.

fedonev
  • 20,327
  • 2
  • 25
  • 34
  • 1
    I tried your option1 but I got `addPropertyOverride is not a function` error. – Joey Yi Zhao Dec 11 '21 at 00:54
  • 1
    [addPropertyOverride](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_codepipeline.CfnPipeline.html#addwbrpropertywbroverridepropertypath-value) is a documented method on `codepipeline.CfnPipeline`. It has been [documented escape hatch](https://docs.aws.amazon.com/cdk/latest/guide/cfn_layer.html#cfn_layer_raw) syntax on the parent class `CfnResource` since 2019. Make sure you are properly casting with `as codepipeline.CfnPipeline`. I can't comment on your invisible code, but can confirm the answer's code compiles and deploys as expected without error. – fedonev Dec 11 '21 at 09:55
  • 1
    Another thought: make sure you are passing `'Resource'` as the child name in `pipeline.node.findChild('Resource')`, as the code comment explains. Passing `'Approve'` as you did in the OP will not work. – fedonev Dec 11 '21 at 17:07
  • 1
    There are multiple stages in my pipeline and what does `Resource` mean? I just want to disable transition in one stage not all. – Joey Yi Zhao Dec 12 '21 at 08:23
  • 1
    Yeah, we found your problem! `Resource` is the name AWS's devs gave to `pipeline's` `CfnPipeline` child. I added an explainer to my answer. My original answer works - just add the stage names you wish to disable to the `addPropertyOverride` array! – fedonev Dec 12 '21 at 13:18