问题描述
一个简单的buildspec,例如:
A simple buildspec like:
version: 0.2
phases:
install:
commands:
- (cd lambda/src; npm install)
- aws cloudformation package --template-file lambda/sam.yml --s3-bucket skynet-lambda --output-template-file SkynetLambdaPackaged.yml
artifacts:
type: zip
files:
- SkynetLambdaPackaged.yml
在构建阶段执行一项操作时,效果很好.但是,例如,如果我想执行更多构建操作,该怎么办:我想并行构建我的api服务器和前端文件.我该如何建模?
Works fine when I have one action in my build stage. But what if I want to have more build actions for example: I want to build my api server and frontend files in parallel. How do I model this?
更新
在CodePipeline中,我可以创建并行运行的动作,如下所示,在buildspec中如何建模?还是不可能?
In CodePipeline I can create actions that run in parallel like below, how is this modeled in buildspec? Or isit impossible?
推荐答案
您可以在相同的来源中使用两个不同的CodeBuild项目,作为CodePipeline中的两个单独的 parallel 操作.
You can use two different CodeBuild projects from the same source as two separate parallel actions in your CodePipeline.
为此,您可以在源文件中使用两个buildspec
文件.
For this to happen, you can use two buildspec
files in your source.
例如
buildspec-frontend.yml
phases:
install:
commands:
- (cd frontend/src; npm run build)
- aws s3 sync frontend/dist s3://<insert s3 bucket url here>/ --delete
buildspec-backend.yml
phases:
install:
commands:
- (cd lambda/src; npm install)
- aws cloudformation package --template-file lambda/sam.yml --s3-bucket skynet-lambda --output-template-file SkynetLambdaPackaged.yml
然后,创建一个使用前端buildspec的前端CodeBuild项目.对后端重复此操作.
Then, create a frontend CodeBuild project that uses the frontend buildspec. Repeat for the backend.
然后,当您进入CodePipeline中的 Build 阶段时,将两个CodeBuild项目用作并行操作.
Then, when you go to your Build stage in your CodePipeline, use the two CodeBuild projects as parallel actions.
更新:由于我误解了这个问题,因此以下信息现在无关紧要.
如果您的前端可以部署到s3,只需将其部署命令添加到您的api部署命令中即可.
If your frontend can be deployed to s3, just add its deployment commands where you put your api deployment commands.
例如
phases:
install:
commands:
- (cd lambda/src; npm install)
- aws cloudformation package --template-file lambda/sam.yml --s3-bucket skynet-lambda --output-template-file SkynetLambdaPackaged.yml
- (cd frontend/src; npm run build)
- aws s3 sync frontend/dist s3://<insert s3 bucket url here>/ --delete
如果您的前端不在s3上,只需将这些行替换为您自己的前端部署命令即可.
If your frontend is not on s3, just replace those lines with your own frontend deployment commands.
CodeBuild依次执行这些命令.如果您确实需要并行运行它们,则有很多方法可以做到这一点.
CodeBuild executes those commands in sequence. If you really need to run them in parallel, there are many ways to do it.
我的偏好是将命令放在Makefile
中,然后从您的buildspec.yml
(例如make --jobs 2 backend frontend
)调用它们.
My preference is to put the commands in a Makefile
and call them from your buildspec.yml
(e.g. make --jobs 2 backend frontend
).
这篇关于CodePipeline构建规范和多个构建动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!