You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CDK中如何正确断言CodePipeline存在指定Name的Stage?

解决CDK断言CodePipeline特定Stage存在的问题

问题原因

你当前的断言代码要求Stages数组长度必须为1,但实际生成的CloudFormation模板中Stages包含多个阶段(比如Source、Build等),因此匹配失败,抛出"Expected array of length 1 but received 3"的错误。

正确解决方案

使用CDK断言库中的Match.array_with方法,它的作用是验证目标数组包含指定元素,不会限制数组总长度,刚好满足仅确认特定Stage存在的需求。

示例代码

import aws_cdk.assertions as assertions

# 验证CodePipeline中存在名为Build的Stage
template.has_resource_properties(
    "AWS::CodePipeline::Pipeline",
    {
        "Stages": assertions.Match.array_with([
            assertions.Match.object_like({"Name": "Build"})
        ])
    }
)

扩展验证多个Stage

如果需要同时验证多个Stage存在(比如Source和Build),可以在array_with中添加多个匹配规则:

"Stages": assertions.Match.array_with([
    assertions.Match.object_like({"Name": "Source"}),
    assertions.Match.object_like({"Name": "Build"})
])

内容的提问来源于stack exchange,提问作者marcin2x4

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 08:31:02