buildspec.yml放在CDK还是源码根目录哪种方式比较好
Which way is better to put buildspec.yml, in CDK or root directory of the source code
在CDK包和源码包中添加buildspec.yml哪种方式比较好?
上下文
现在有两种添加构建规范代码的方法。
第一个选项是将其添加到源代码包中,如前所述here:“如果将构建规范作为源代码的一部分,默认情况下,构建规范文件必须命名为buildspec.yml 并放置在源目录的根目录中。"
但是,这个选项感觉不太好,因为构建规范实际上与服务实现无关,而更多地与如何部署服务有关。而且它更特定于 AWS。就是感觉不应该是源码包的一部分
第二个选项是将其添加到CDK中。但我真的不喜欢内联构建规范代码,因为它看起来很臃肿,只是一个例子:
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
',
],
},
}
})
});
构建规范代码会变得更复杂、更长。
所以我打算添加:
const buildspecFile = FS.readFileSync('./config/buildspec.yml', 'utf-8');
const buildspecFileYaml = YAML.parse(buildspecFile,{
prettyErrors:true
}
);
const codeBuildProject = new CodeBuild.PipelineProject(this, "Build", {
projectName: "Build",
environment: {
buildImage: CodeBuild.LinuxBuildImage.AMAZON_LINUX_2_2,
privileged: true,
},
environmentVariables: {
CLUSTER_NAME: {
value: `${props.fargateCluster.clusterName}`,
},
ECR_REPO_URI: {
value: `${ecrRepo.repositoryUri}`,
},
},
buildSpec: CodeBuild.BuildSpec.fromObjectToYaml(buildspecFileYaml),
});
哪种方式更好?我不认为 AWS CodePipeline 支持从 CDK 包中的本地文件读取?我现在只看到三个选项:
CodeBuild.BuildSpec.fromObject
CodeBuild.BuildSpec.fromObjectToYaml
CodeBuild.BuildSpec.fromSourceFilename
Which way is better to add buildspec.yml, in CDK package or source code package?
通过。这取决于用例和开发人员偏好。
I don't think AWS CodePipeline support read from a local file in CDK package yet?
是的,但您可以使用语言功能自行将本地 yaml buildspec 文件内联到 CDK 输出中。读取并解析文件,然后将结果传递给fromObject
方法:
import * as yaml from 'yaml'; // https://www.npmjs.com/package/yaml
const stringified: string = fs.readFileSync(path.join(__dirname, './buildspec.yml'), { encoding: 'utf-8', });
const parsed: any = yaml.parse(stringified);
new codebuild.Project(this, 'MyProject', {
buildSpec: codebuild.BuildSpec.fromObject(parsed),
});
在CDK包和源码包中添加buildspec.yml哪种方式比较好?
上下文
现在有两种添加构建规范代码的方法。
第一个选项是将其添加到源代码包中,如前所述here:“如果将构建规范作为源代码的一部分,默认情况下,构建规范文件必须命名为buildspec.yml 并放置在源目录的根目录中。"
但是,这个选项感觉不太好,因为构建规范实际上与服务实现无关,而更多地与如何部署服务有关。而且它更特定于 AWS。就是感觉不应该是源码包的一部分
第二个选项是将其添加到CDK中。但我真的不喜欢内联构建规范代码,因为它看起来很臃肿,只是一个例子:
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
',
],
},
}
})
});
构建规范代码会变得更复杂、更长。
所以我打算添加:
const buildspecFile = FS.readFileSync('./config/buildspec.yml', 'utf-8');
const buildspecFileYaml = YAML.parse(buildspecFile,{
prettyErrors:true
}
);
const codeBuildProject = new CodeBuild.PipelineProject(this, "Build", {
projectName: "Build",
environment: {
buildImage: CodeBuild.LinuxBuildImage.AMAZON_LINUX_2_2,
privileged: true,
},
environmentVariables: {
CLUSTER_NAME: {
value: `${props.fargateCluster.clusterName}`,
},
ECR_REPO_URI: {
value: `${ecrRepo.repositoryUri}`,
},
},
buildSpec: CodeBuild.BuildSpec.fromObjectToYaml(buildspecFileYaml),
});
哪种方式更好?我不认为 AWS CodePipeline 支持从 CDK 包中的本地文件读取?我现在只看到三个选项:
CodeBuild.BuildSpec.fromObject
CodeBuild.BuildSpec.fromObjectToYaml
CodeBuild.BuildSpec.fromSourceFilename
Which way is better to add buildspec.yml, in CDK package or source code package?
通过。这取决于用例和开发人员偏好。
I don't think AWS CodePipeline support read from a local file in CDK package yet?
是的,但您可以使用语言功能自行将本地 yaml buildspec 文件内联到 CDK 输出中。读取并解析文件,然后将结果传递给fromObject
方法:
import * as yaml from 'yaml'; // https://www.npmjs.com/package/yaml
const stringified: string = fs.readFileSync(path.join(__dirname, './buildspec.yml'), { encoding: 'utf-8', });
const parsed: any = yaml.parse(stringified);
new codebuild.Project(this, 'MyProject', {
buildSpec: codebuild.BuildSpec.fromObject(parsed),
});