如何在 cdk 中编写 buildspec yaml 代码
我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
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 输出的示例。请注意,环境是在构建规范之外定义的:
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 abuildSpec
: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, usefromObjectToYaml
. 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: