【问题标题】:How can I disable transition in codepipeline via CDK?如何通过 CDK 禁用代码管道中的转换?
【发布时间】:2022-01-14 19:03:02
【问题描述】:

我正在使用 nodejs CDK 将 codepipeline 部署到 AWS。下面是代码:

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)
      ),
    });

它在stages 数组中定义了所有阶段。我的问题是如何在此管道的某个阶段禁用转换?

我试过下面的代码:

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

但它显示no such method addOverride 错误。

【问题讨论】:

  • 假设此转换不会永久禁用,您是否考虑过对此用例的手动批准步骤?

标签: amazon-web-services aws-cdk aws-codepipeline


【解决方案1】:

从 CDK v2.1 开始,codepipeline.Pipeline 类不公开此属性,但它所构建的 Level1 CfnPipeline 类会公开 (github issue)。

选项 1:快速而肮脏的解决方法:进入 codepipeline.Pipeline 的实现以获取对其 CfnPipeline 的引用(这是您尝试过的方法):

// 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',
  },
]);

选项 2:实例化一个 Level1 CfnPipeline,它接受一个 disableInboundStageTransitions 属性。

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

编辑:说明ResourceCfnPipeline子节点的名称

我们通过将阶段名称传递给 L1 CfnPipeline 来禁用阶段转换。方法 #2 通过创建一个来直接做到这一点。 但我们宁愿使用 L2 Pipeline,因为它更容易。这是方法#1,你正在采用的方法。幸运的是,我们的pipeline 有一个名为“资源”的CfnPipeline 子节点。我们怎么知道呢?我们在 Pipeline constructor's source code on github。 一旦我们使用pipeline.node.findChild('Resource') 引用了CfnPipeline,我们将禁用的阶段作为属性覆盖添加到它,其格式与#2 中的{StageName: Reason:} 格式相同。

【讨论】:

  • 我尝试了您的选项 1,但我收到了 addPropertyOverride is not a function 错误。
  • addPropertyOverride 是在 codepipeline.CfnPipeline 上记录的方法。自 2019 年以来,父类 CfnResource 上一直是 documented escape hatch 语法。确保使用 as codepipeline.CfnPipeline 正确转换。我无法评论您的隐形代码,但可以确认答案的代码按预期编译和部署,没有错误。
  • 另一个想法:确保您将'Resource' 作为pipeline.node.findChild('Resource') 中的子名称传递,正如代码注释所解释的那样。像在 OP 中那样传递 'Approve' 是行不通的。
  • 我的管道中有多个阶段,Resource 是什么意思?我只想在一个阶段禁用过渡,而不是全部。
  • 是的,我们找到了您的问题! Resource 是 AWS 开发人员给 pipeline's CfnPipeline 孩子起的名字。我在答案中添加了解释器。我的原始答案有效 - 只需将您希望禁用的阶段名称添加到 addPropertyOverride 数组!
猜你喜欢
  • 1970-01-01
  • 2020-08-06
  • 2020-01-08
  • 2022-11-12
  • 2021-10-18
  • 1970-01-01
  • 2020-09-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多