如何在 cdk 中编写 buildspec yaml 代码

发布于 2025-01-12 17:15:19 字数 1503 浏览 0 评论 0原文

我使用 cdk 进行代码构建,

它可以接受构建规范作为 yaml,但是,我如何在 cdk 中做同样的事情?

我想做的就是这样,当然不行,

我强行把yaml代码放到命令里。

    const buildProject = new codebuild.PipelineProject(this, 'project', {
      environment: {// I guess I need to select ubuntu and image 4.0},
      buildSpec: codebuild.BuildSpec.fromObject({
        version: '0.2',
        phases: {
          build: {
            commands:['
              version: 0.2
                phases:
                    install:
                        runtime-versions:
                            docker: 18
                    build:
                        commands:
                            - apt-get install jq -y
                            - ContainerName="tnkDjangoContainer"
                            - ImageURI=$(cat imageDetail.json | jq -r '.ImageURI')
                            - printf '[{"name":"CONTAINER_NAME","imageUri":"IMAGE_URI"}]' > imagedefinitions.json
                            - sed -i -e "s|CONTAINER_NAME|$ContainerName|g" imagedefinitions.json
                            - sed -i -e "s|IMAGE_URI|$ImageURI|g" imagedefinitions.json
                            - cat imagedefinitions.json

                artifacts:
                    files:
                        - imagedefinitions.json
              
              ',         
            ],
          },
        }
      })
    });

而且我想我需要选择图像来执行构建规范,例如 Ubuntu

我可以在哪里设置这些?

I making the codebuild with cdk

It can accept the buildspec as yaml, however ,how can I do the same thing in cdk?

What I want to do is like this, of course it doesn't work though,

I forcely put the yaml code in commands.

    const buildProject = new codebuild.PipelineProject(this, 'project', {
      environment: {// I guess I need to select ubuntu and image 4.0},
      buildSpec: codebuild.BuildSpec.fromObject({
        version: '0.2',
        phases: {
          build: {
            commands:['
              version: 0.2
                phases:
                    install:
                        runtime-versions:
                            docker: 18
                    build:
                        commands:
                            - apt-get install jq -y
                            - ContainerName="tnkDjangoContainer"
                            - ImageURI=$(cat imageDetail.json | jq -r '.ImageURI')
                            - printf '[{"name":"CONTAINER_NAME","imageUri":"IMAGE_URI"}]' > imagedefinitions.json
                            - sed -i -e "s|CONTAINER_NAME|$ContainerName|g" imagedefinitions.json
                            - sed -i -e "s|IMAGE_URI|$ImageURI|g" imagedefinitions.json
                            - cat imagedefinitions.json

                artifacts:
                    files:
                        - imagedefinitions.json
              
              ',         
            ],
          },
        }
      })
    });

And also I guess I need to choose the image to do the buildspec such as Ubuntu

Where can I set these?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迷迭香的记忆 2025-01-19 17:15:19

CDK 不公开在合成时内联 YAML 构建规范的方法。您可以通过将现有 YAML 解析为 JS 对象并将结果传递给 BuildSpec.fromObject 来自行完成此操作。

CDK 的 codebuild.Project 为您提供了其他几种提供 buildSpec 的方法:

  • BuildSpec.fromObject 在合成时从键值对内联构建规范。它应该遵循 CodeBuild 构建规范格式。 CDK 将在 CloudFormation 模板中输出字符串化的 JSON 构建规范。如果您希望 CDK 输出 YAML,请使用 fromObjectToYaml。这两种方法都采用键值对(type:[key: string]:any;)作为输入,因此 TS 无法提供太多类型检查帮助。
  • BuildSpec.fromSourceFilename 告诉 CodeBuild 在运行时使用源代码中的构建规范文件。文件名在 CloudFormation 模板中传递。

以下是使用 yaml 包将 YAML 字符串解析为内联 YAML 输出的示例。请注意,环境是在构建规范之外定义的:

import * as yaml from 'yaml';

const fromYaml = yaml.parse(`
  version: '0.2'
  phases:
    build:
      commands:
        - npm run build
`);

new codebuild.Project(this, 'YamlInYamlOutProject', {
  environment: {
    buildImage: codebuild.LinuxBuildImage.STANDARD_5_0, // Ubuntu Standard 5
  },
  buildSpec: codebuild.BuildSpec.fromObjectToYaml(fromYaml),
});

CDK does not expose a method to inline a YAML buildspec at synth-time. You could do this yourself by parsing existing YAML into a JS object and passing the result to BuildSpec.fromObject.

CDK's codebuild.Project gives you several other ways to provide a buildSpec:

  • BuildSpec.fromObject inlines a buildspec from key-value pairs at synth-time. It should follow the CodeBuild buildspec format. CDK will output a stringified JSON buildspec in the CloudFormation template. If you want CDK to output YAML instead, use fromObjectToYaml. Both methods take key-value pairs (type: [key: string]: any;) as input, so TS can't offer much typechecking help.
  • BuildSpec.fromSourceFilename tells CodeBuild to use a buildspec file in your source at run-time. The filename is passed in the CloudFormation template.

Here's an example of parsing a YAML string into inlined YAML output, using the yaml package. Note that the environment is defined outside the buildspec:

import * as yaml from 'yaml';

const fromYaml = yaml.parse(`
  version: '0.2'
  phases:
    build:
      commands:
        - npm run build
`);

new codebuild.Project(this, 'YamlInYamlOutProject', {
  environment: {
    buildImage: codebuild.LinuxBuildImage.STANDARD_5_0, // Ubuntu Standard 5
  },
  buildSpec: codebuild.BuildSpec.fromObjectToYaml(fromYaml),
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文