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

时间:2019-06-19 08:27:30

标签: amazon-web-services continuous-integration continuous-deployment aws-codepipeline aws-codebuild

我正在尝试建立一个代码构建,其中我有一个模块可以为其他构建创建依赖关系。这些其他构建将使用由第一个构建生成的工件,并且这些构建应并行运行。我在看CodeBuild的文档,似乎那里没有信息。我的buildspec.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

2 个答案:

答案 0 :(得分:2)

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

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

所以看起来像这样

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

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

答案 1 :(得分:0)

正如@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