parameters - 使用 CDK 'codepipeline_actions.CloudFormationCreateUpdateStackAction' 中的部署时间参数

标签 parameters aws-cloudformation aws-codepipeline aws-codebuild dynamicparameters

我显然不明白在 CodePipeline 中部署模板时如何使用动态参数。这是基本场景:

CodeBuild BUILD 操作 - 例如,我在这里检索了 COMMIT_NUMBER 的值。这可以设置为环境变量,或保存到文件中......无论需要做什么。在 post_build 步骤中,我使用“sam package ....”打包模板

CloudFormation DEPLOY 阶段 - 我在这里使用

new codepipeline_actions.CloudFormationCreateUpdateStackAction({
      actionName: `The_Deploy`,
      templatePath: buildOutput.atPath(TEMPLATE_FILE_NAME),
      parameterOverrides, --These are known when I synth the pipeline
      stackName: envStackName,
      cfnCapabilities: [CfnCapabilities.AUTO_EXPAND, CfnCapabilities.ANONYMOUS_IAM],
      adminPermissions: true,
      role: buildRole,
      runOrder: runOrder || 1
    });

部署打包好的模板。在此方法的 props 中,有一个parameterOverrides 属性,但其中的任何内容都必须在构建时知道。

我的问题是如何将构建步骤中已知的动态参数值设置为部署步骤使用的参数。

感谢您的澄清!

最佳答案

谢谢@Ronan Cunningham 提供的链接:here 。澄清如何做到这一点的关键点是下面这句话,我还没有找到很好的解释:

"Since namespaces are native CodePipeline feature, they are not restricted only to CloudFormation actions. Now you can for example define CodeBuild environment variables in a dynamic way as well (it is also possible to define export variables in CodeBuild - these values will will become step output values):" ....

将所有内容整合在一起是允许我在代码构建操作中设置环境变量并将其传递给 CDK 中的 cloudformation 参数的关键部分:

1.) 在构建项目中添加 env 变量和导出变量

const buildProject = new codebuild.PipelineProject(construct, `buildProject`, {
    buildSpec: codebuild.BuildSpec.fromObject({
    version: '0.2',
    env: {
      "variables": {
        "MY_IMPORTANT_INFO": "some-default-value-here"
      },
      "exported-variables": [ "MY_IMPORTANT_INFO" ]
    },
    phases: {
      install: {...},
      build: {
        commands: [ NOTE... variable above can be changed here...]
      }
    },
    artifacts: {...}
    role: buildRole
  })

2.) 将命名空间添加到构建操作:

new codepipeline_actions.CodeBuildAction({
      actionName: `The_Build`,
      project: buildProject,
      input: sourceOutput,
      outputs: [buildOutput],
      role: buildRole,
      variablesNamespace: 'build'
    })

3.) 在部署阶段,我现在可以通过将参数名称设置为在构建步骤中导出的环境变量来设置 ParameterOverride:

new codepipeline_actions.CloudFormationCreateUpdateStackAction({
      actionName: `The_Deploy`,
      templatePath: buildOutput.atPath(TEMPLATE_FILE_NAME),
      parameterOverrides: [
        {"COMMIT_NUMBER": "#{build.MY_IMPORTANT_INFO}"}
      ],
      stackName: envStackName,
      cfnCapabilities: [CfnCapabilities.AUTO_EXPAND, CfnCapabilities.ANONYMOUS_IAM],
      adminPermissions: true,
      role: buildRole,
      runOrder: runOrder || 1
    });

哈利路亚!

并且,再次感谢 Ronan 提供的信息链接!

关于parameters - 使用 CDK 'codepipeline_actions.CloudFormationCreateUpdateStackAction' 中的部署时间参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69317989/

相关文章:

powershell - 将参数数组传递给 powershell.exe

java - 启动tomcat时如何传递-D附加参数?

amazon-web-services - 如何将 CloudFormation 参数作为类型 : Number? 传递

amazon-web-services - 通过 cloudformation 更新时 ECS 任务陷入 PENDING

aws-cloudformation - GitHub 操作 : Deleting stack in AWS returns Waiter StackDeleteComplete failed: Max attempts exceeded

Java 更好的方法来编码这个简单的参数摄入?

c# - 为什么我们不能在派生类中使用带参数的构造函数

python - AWS CDK 管道 : How to access internal stack values in post-stage steps?

python - 在 AWS Code Pipeline 中使用 docker compose 时出错

codeship - 我可以跳过AWS CodePipeline构建吗?