如何在AWS CodeBuild中运行并行构建?

4

我正在尝试设置一个 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个回答

6
CodeBuild 用于自动化构建步骤,而不是整个 CICD 过程。在 CodeBuild 中,您需要指定 buildspec.yml 来自动执行特定构建中需要执行的一系列步骤。
如果您需要自动化构建序列,则最简单的选项是使用 CodePipeline,在其中为 CICD 过程中的每个步骤创建一个阶段。在您的情况下,这意味着一个步骤(阶段)将是您在帖子中描述的 CodeBuild 操作,该操作将转换为另一个阶段,在该阶段中,您可以指定其他 CodeBuild 操作,并且可以并行运行这些操作,这些操作可以被指定为以前一个步骤的工件作为输入。
因此,它将如下所示:
输入 -> 阶段(执行初始构建) -> 阶段(并行指定多个构建操作 - 在控制台上,可以将它们横向放置在一起而不是垂直放置
另一个选项是,不使用 CodePipeline,而是使用 Lambda 函数和 CloudWatch 事件。CodeBuild 在构建完成后发布事件。您可以订阅 Lambda 函数到该事件,并编写代码,根据需要执行以下构建。

我的问题是我的输入不被AWS支持,因为源代码在Bitbucket上。 有没有办法解决这个问题? - KyelJmD
有多种方法可以解决这个问题。您应该查看此链接以获取更好的想法-https://lgallardo.com/2018/09/07/codepipeline-bitbucket - Matus Dubrava
好的解释!我们在生产中使用它,但我永远不会再使用那个设置了。现代的CI/CD解决方案例如Github CI工作流、Gitlab CI、CircleCI允许您使用单个配置文件来进行CI和CD。这简化了很多事情。您可以并行处理作业或完成管道,共享数据,配置触发器(何时启动任务),发布部署钩子,跨操作系统构建等等。在AWS Codebuild中,您甚至没有git上下文!您完全被强制锁定在CodeBuild、CodePipeline、Lambda中,而集成只是“可以,它能工作”。 - Dustin Deus
我同意。这就是使用“易于使用”的特定供应商解决方案所付出的代价。AWS强制推行他们的方式,在这种特定情况下,我觉得相当烦人。对于lambda解决方案,我还算可以接受,但是我认为这个过程的可见性(以及故障排除)非常痛苦。 - Matus Dubrava
2
AWS CodeBuild 现在支持 Bitbucket。 - MEMark

0

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

从AWS到达Bitbucket的一种适当方法是提供一个Star Connection(开发者工具>代码构建>设置>连接)。

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

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。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接