简体   繁体   English

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

[英]How to set Branch Filter option in AWS CodeBuild cloudformation template?

If using a Github repository as a source in a CodeBuild project, the Branch Filter option allows to run builds only for branches, whose name is matching a certain regular expression.如果在 CodeBuild 项目中使用 Github 存储库作为源,则Branch Filter选项允许仅对名称与某个正则表达式匹配的分支运行构建。

  1. AWS Management Console AWS 管理控制台

In the AWS Management Console you can configure the branch filter upon creating or editing a CodeBuild project:在 AWS 管理控制台中,您可以在创建或编辑 CodeBuild 项目时配置分支过滤器:

AWS 控制台

  1. AWS CLI命令行界面

For awscli exists an option --update-webhook (documented here )对于 awscli 存在一个选项--update-webhook (记录在此处

    $ aws codebuild update-webhook --project-name myproject --branch-filter ^master$
  1. CloudFormation云形成

In CodeBuild cloudformation template exists an option Triggers > Webhook (documented here ), but this option is just a boolean for simple enabling/disabling the github webhook.在 CodeBuild cloudformation 模板中存在一个选项Triggers > Webhook在此处记录),但该选项只是一个布尔值,用于简单地启用/禁用 github webhook。

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

So my question is, how to directly define a branch filter in a cloudformation template, without subsequently having to execute an awscli command or use the AWS Management Console?所以我的问题是,如何直接在 cloudformation 模板中定义分支过滤器,而无需随后执行 awscli 命令或使用 AWS 管理控制台?

You can try using AWS CodePipeline您可以尝试使用 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"

Then you just need to define your CodeBuild project with CodePipeline integration:然后您只需要使用 CodePipeline 集成定义您的 CodeBuild 项目:

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

Here is a minimal example using triggers and webhook filters, filter group pattern can also be something like ^refs/heads/.* :这是一个使用触发器和 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

See also: https://docs.amazonaws.cn/en_us/codebuild/latest/userguide/sample-bitbucket-pull-request.html#sample-bitbucket-pull-request-filter-webhook-events-cfn另见: https : //docs.amazonaws.cn/en_us/codebuild/latest/userguide/sample-bitbucket-pull-request.html#sample-bitbucket-pull-request-filter-webhook-events-cfn

Set source version in your template and branch will be selected automatically by cloud formation在您的模板中设置源版本,云形成将自动选择分支

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

"main" is the name of my branch, so “main”是我的分支的名称,所以

SourceVersion: refs/heads/main

在此处输入图片说明

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AWS CodeBuild分支过滤器选项已删除 - AWS CodeBuild Branch filter option removed 如何自定义AWS Codestar / Cloudformation模板以创建特定的代码构建项目? - How to customize AWS Codestar / Cloudformation template to create specific codebuild project? 如何允许 AWS CodeBuild 修改 CloudFormation 模板中的指定角色? - How to allow AWS CodeBuild to modify specified role in CloudFormation template? AWS CodeBuild Webhook - CloudFormation - AWS CodeBuild Webhook - CloudFormation 在存储库的cloudformation模板中使用AWS :: CodeBuild :: Project环境变量 - Using AWS::CodeBuild::Project Environment variable in cloudformation template of repository 设置AWS Kinesis cloudformation模板 - Set AWS Kinesis cloudformation template 如何配置 AWS CodeBuild 构建规范以使用由 CloudFormation 创建的动态 ReportGroup - How to configure an AWS CodeBuild buildspec to use a dynamic ReportGroup created by CloudFormation AWS CodePipeline:如何将CloudFormation操作的输出传递给CodeBuild操作 - AWS CodePipeline: How to pass output from CloudFormation action to a CodeBuild action 如何在 Cloudformation 模板上为 AWS EC2 实例设置 MemorySize? - How to set MemorySize for AWS EC2 Instance on Cloudformation Template? 如何过滤cloudformation中的aws资源 - How to filter aws resource in cloudformation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM