如何使用 AWS Codebuild 运行并行构建?



我正在尝试设置一个代码构建,其中我有一个模块可以为其他构建创建依赖项。这些其他构建将使用第一个构建生成的工件,这些构建应并行运行。我正在查看CodeBuild的文档,似乎那里没有信息。我的构建规范.yml 示例

version: 0.2
#env:
#variables:
# key: "value"
# key: "value"
#parameter-store:
# key: "value"
# key: "value"
#git-credential-helper: yes
phases:
#install:
#If you use the Ubuntu standard image 2.0 or later, you must specify runtime-versions.
#If you specify runtime-versions and use an image other than Ubuntu standard image 2.0, the build fails.
#runtime-versions:
# name: version
# name: version
#commands:
# - command
# - command
#  pre_build:
#    commands:
#       - mkdir artifacts
#       - pwd
#       - ls
build:
commands:
- cd frontend
- npm install
- cd ..
- cd othermodule
- npm install
#post_build:
#commands:
#  - cp -a $CODEBUILD_SRC_DIR/frontend/dist. 
# - command
artifacts:
files:
- package-lock.json
- node_modules/*
base-directory: frontend
#cache:
#paths:
# - paths

CodeBuild 用于自动化构建步骤,而不是自动化整个 CICD 过程。在 CodeBuild 中,您可以指定buildspec.yml来自动执行需要在该特定构建中执行的一系列步骤。

如果您需要自动执行构建顺序,那么最简单的选择是使用 CodePipeline,您可以在其中为 CICD 流程中的每个步骤创建阶段。在您的情况下,这意味着一个步骤(阶段)将是您在帖子中描述的 CodeBuild 操作,该操作将过渡到另一个阶段,您可以在其中指定另一个 CodeBuild 操作,并且可以指定这些操作以将上一步中的工件作为输入,您可以并行运行它们。

所以它看起来像这样

输入 -> STAGE(执行初始构建)-> STAGE(并行指定多个构建操作 -在控制台中,这可以通过将它们水平而不是垂直放置来完成)

不使用 CodePipeline 的另一种选择是将 Lambda 函数与 CloudWatch 事件一起使用。CodeBuild 在构建完成后发布事件。您可以将 Lambda 函数订阅到该事件,并编写将根据需要执行后续构建的代码。

正如@MEMark所指出的,目前AWS CodePipeline服务支持Bitbuket。

从 AWS 访问 Bitbucket 的一种合适方法是提供星形连接(开发人员工具>代码构建>设置>连接)。

创建连接后,可以使用以下代码部署管道:

AWSTemplateFormatVersion: "2010-09-09"
Description: TBD
Parameters:
DeployStage:
Type: String
Description: name of the stage to deploy(dev, test, prod)
BranchName:
Type: String
Description: TBD 
CodeStarConnectionARN:
Type: String
Description: ARN of the codestar connection to Bitbucket.
Resources:

CodeBuildRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
Effect: Allow
Principal:
Service: codebuild.amazonaws.com
Action: sts:AssumeRole
ManagedPolicyArns:
- arn:aws:iam::aws:policy/AdministratorAccess  
PipelineRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Statement:
- Action: 'sts:AssumeRole'
Effect: Allow
Principal:
Service: codepipeline.amazonaws.com
Version: '2012-10-17'
Policies:
- PolicyName: CodePipelineAccess
PolicyDocument:
Version: '2012-10-17'
Statement:
- Action:
- TBD
- ...
Effect: Allow
Resource: '*'
-
Effect: Allow
Action:
- TBD
- ...
Resource: "*"
BuildUi:
Type: AWS::CodeBuild::Project
Properties:
Name: !Join ['', ['build-ui-', !Ref DeployStage]]
Description: TBD
ServiceRole: !Ref CodeBuildRole
Artifacts:
Type: CODEPIPELINE
Environment:
Type: LINUX_CONTAINER
ComputeType: BUILD_GENERAL1_SMALL
Image: aws/codebuild/standard:5.0
EnvironmentVariables:
- Name: DEPLOY_STAGE
Type: PLAINTEXT
Value: !Ref DeployStage
- Name: DEPLOY_BRANCH_NAME
Type: PLAINTEXT
Value: !Ref BranchName    
Source:
Type: CODEPIPELINE
Location: https://user@bitbucket.org/organization/projectName.git 
BuildSpec: buildspec.ui.yml
SourceVersion: !Ref BranchName  
Tags:
-
Key: cost
Value: ui_build
TimeoutInMinutes: 10
BuildApi:
Type: AWS::CodeBuild::Project
Properties:
Name: !Join ['', ['build-api-', !Ref DeployStage]]
Description: TBD
ServiceRole: !Ref CodeBuildRole
Artifacts:
Type: CODEPIPELINE
Environment:
Type: LINUX_CONTAINER
ComputeType: BUILD_GENERAL1_SMALL
Image: aws/codebuild/standard:5.0
EnvironmentVariables:
- Name: DEPLOY_STAGE
Type: PLAINTEXT
Value: !Ref DeployStage
- Name: DEPLOY_BRANCH_NAME
Type: PLAINTEXT
Value: !Ref BranchName    
Source:
Type: CODEPIPELINE
Location: https://user@bitbucket.org/organization/projectName.git 
BuildSpec:buildspec.api.yml
SourceVersion: !Ref BranchName  
Tags:
-
Key: cost
Value: api_build
TimeoutInMinutes: 10

ArtifactStoreBucket:
Type: AWS::S3::Bucket
Properties:
AccessControl: 'BucketOwnerFullControl'
VersioningConfiguration:
Status: Enabled    
NamePipeline:
Type: AWS::CodePipeline::Pipeline
Properties:
ArtifactStore:
Location: !Ref ArtifactStoreBucket
Type: S3
Name: !Join ['', ['pipeline-', !Ref DeployStage]]
RoleArn: !GetAtt PipelineRole.Arn
Stages:
- Name: Source
Actions:
- Name: Source
ActionTypeId:
Category: Source
Owner: AWS
Provider: CodeStarSourceConnection
Version: '1'
Configuration:
ConnectionArn: !Ref CodeStarConnectionARN
FullRepositoryId: "organization/projectName"
BranchName: !Ref BranchName                
OutputArtifacts:
- Name: project-source
RunOrder: '1'
- Name: ParallelBuild
Actions:
- Name: BuildUi
ActionTypeId:
Category: Build
Owner: AWS
Version: 1
Provider: CodeBuild
OutputArtifacts:
- Name: project-build-ui
InputArtifacts:
- Name: project-source
Configuration:
ProjectName: !Ref BuildUi
RunOrder: 1
- Name: BuildApi
ActionTypeId:
Category: Build
Owner: AWS
Version: 1
Provider: CodeBuild
OutputArtifacts:
- Name: build-api
InputArtifacts:
- Name: project-source
Configuration:
ProjectName: !Ref BuildApi
RunOrder: 1                   

注释

  • 它假设存储库具有以下 git URL:https://user@bitbucket.org/organization/projectName.git
  • CodeBuildRole 和 PipelineRole 应该为用例正确定义

相关内容

  • 没有找到相关文章

最新更新