如何在AWS CodeBuild云形态模板中设置分支过滤器选项?

时间:2018-06-14 12:47:35

标签: amazon-web-services continuous-integration amazon-cloudformation devops aws-codebuild

如果使用Github存储库作为CodeBuild项目中的源,Branch Filter选项只允许为名称与某个正则表达式匹配的分支运行构建。

  1. AWS管理控制台
  2. 在AWS管理控制台中,您可以在创建或编辑CodeBuild项目时配置分支过滤器:

    AWS console

    1. AWS CLI
    2. 对于awscli,存在一个选项--update-webhook(记录为here

          $ aws codebuild update-webhook --project-name myproject --branch-filter ^master$
      
      1. CloudFormation
      2. 在CodeBuild中,cloudformation模板存在一个选项Triggers > Webhook(记录为here),但此选项只是一个简单启用/禁用github webhook的布尔值。

        Resources:
            MyCodeBuildProject:
            Type: AWS::CodeBuild::Project
            Properties:
                Name: myproject
                ...
                Triggers:
                    Webhook: true
        

        所以我的问题是,如何在云形态模板中直接定义分支过滤器,而无需执行awscli命令或使用AWS管理控制台?

3 个答案:

答案 0 :(得分:1)

您可以尝试使用AWS CodePipeline

        Stages:
            -
                Name: "Source"
                Actions:
                    -
                        Name: "Checkout"
                        ActionTypeId:
                            Category: "Source"
                            Owner: "ThirdParty"
                            Provider: "GitHub"
                            Version: "1"
                        Configuration:
                            Owner: !Ref "UsernameOrOrg"
                            Repo: !Ref "ProjectName"
                            Branch: "master"
                            OAuthToken: !Ref "GitHubOAuthToken"
                        OutputArtifacts:
                            -
                                Name: "checkout"
            -
                Name: "Build"
                Actions:
                    -
                        Name: "Build"
                        ActionTypeId:
                            Category: "Build"
                            Owner: "AWS"
                            Provider: "CodeBuild"
                            Version: "1"
                        Configuration:
                            ProjectName: !Ref "BuildProject"
                        InputArtifacts:
                            -
                                Name: "checkout"

然后,您只需要使用CodePipeline集成定义CodeBuild项目:

BuildProject:
    Type: "AWS::CodeBuild::Project"
    Properties:
       ... 
        Artifacts:
            Type: "CODEPIPELINE"
        Source:
            Type: "CODEPIPELINE"

答案 1 :(得分:1)

这是使用触发器和webhook过滤器的一个最小示例,过滤器组模式也可以类似于^refs/heads/.*

AWSTemplateFormatVersion: "2010-09-09"
Description: "CodeBuild project and IAM role"
Parameters:
  Image:
    Type: String
    Description: "Name of the docker image."
    Default: "my-image"
Resources:
  CodeBuildRole:
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument:
        Version: "2012-10-17"
        Statement:
          Effect: Allow
          Principal:
            Service: codebuild.amazonaws.com
          Action: sts:AssumeRole
      Policies:
        - PolicyName: "CodeBuild-Service-Policy"
          PolicyDocument:
            Version: "2012-10-17"
            Statement:
              - Effect: "Allow"
                Action:
                  - "ecr:BatchCheckLayerAvailability"
                  - "ecr:CompleteLayerUpload"
                  - "ecr:DescribeImages"
                  - "ecr:GetAuthorizationToken"
                  - "ecr:InitiateLayerUpload"
                  - "ecr:ListImages"
                  - "ecr:PutImage"
                  - "ecr:UploadLayerPart"
                  - "logs:*"
                Resource: "*"
  CodeBuildProject:
    Type: AWS::CodeBuild::Project
    Properties:
      Artifacts:
        Type: NO_ARTIFACTS
      Environment:
        ComputeType: "BUILD_GENERAL1_SMALL"
        Image: "aws/codebuild/docker:18.09.0"
        Type: LINUX_CONTAINER
      ServiceRole: !GetAtt CodeBuildRole.Arn
      Source:
        Type: GITHUB
        Location: "https://github.com/ORG/REPO.git"
        BuildSpec: "codebuild/create_docker_image.yml"
      Triggers:
        Webhook: true
        FilterGroups:
          - - Type: EVENT
              Pattern: PUSH
            - Type: HEAD_REF
              Pattern: master

另请参阅: https://docs.amazonaws.cn/en_us/codebuild/latest/userguide/sample-bitbucket-pull-request.html#sample-bitbucket-pull-request-filter-webhook-events-cfn

答案 2 :(得分:0)

在模板中设置源版本,云生成会自动选择分支

文档:https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-codebuild-project.html#cfn-codebuild-project-sourceversion

“main”是我的分支名称,所以

SourceVersion: refs/heads/main

enter image description here

enter image description here