You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让AWS CodeCommit触发所有分支的AWS CodePipeline?

How to Trigger AWS CodePipeline on All Branch Commits & Add Conditional Stages (Master vs Others)

Alright, let's walk through how to replicate that GitLab CI/CD behavior in AWS CodePipeline—since the two systems handle branch triggers and conditional stages a bit differently. Here's step-by-step what you need to do:

1. First: Let All Branch Commits Trigger Your Pipeline

By default, CodeStar's initial setup only triggers pipelines on master branch changes. To get every branch's commits to trigger runs, you'll need to replace the default source trigger with a CloudWatch Event Rule:

Step 1.1: Remove the Branch-Specific Trigger in CodePipeline

Go to your CodePipeline's Source stage configuration. If you're using CodeCommit as the source, delete the explicit BranchName setting (this lets the pipeline accept events for any branch).

Step 1.2: Create a CloudWatch Event Rule for All Branch Pushes

Head to the CloudWatch console, create a new Event Rule with this event pattern (replace your-repo-name with your CodeCommit repo name):

{
  "source": ["aws.codecommit"],
  "detail-type": ["CodeCommit Repository State Change"],
  "detail": {
    "event": ["referenceCreated", "referenceUpdated"],
    "referenceType": ["branch"],
    "repositoryName": ["your-repo-name"]
  }
}

Set the rule's target to your CodePipeline, and make sure you attach an IAM role that allows CloudWatch Events to trigger the pipeline.

2. Add Conditional Stages (Auto for Master, Manual for Others)

Now that all branches trigger the pipeline, you need to define different behavior for master vs non-master branches. There are two straightforward ways to do this:

Option 1: Use CodePipeline's Built-in Conditional Actions

CodePipeline lets you set conditions on individual actions to control when they run. Here's how to set this up:

  1. Capture the branch name: If you're using CodeBuild in your pipeline, the CODEBUILD_SOURCE_BRANCH environment variable will automatically hold the current branch name. You can use this variable in your pipeline conditions.
  2. Add a manual approval for non-master branches: In your pipeline's deploy (or any downstream) stage, add a Manual Approval action. Set its condition to:
    NotEquals($CODEBUILD_SOURCE_BRANCH, master)
    
    This means the approval step only shows up when the pipeline is triggered from a non-master branch.
  3. Auto-run actions for master: For actions that should run automatically on master (like production deployment), set their condition to:
    Equals($CODEBUILD_SOURCE_BRANCH, master)
    
    These actions will skip entirely for non-master branches unless you approve them first (or adjust the conditions to fit your workflow).

Option 2: Handle Branch Logic in CodeBuild (Like GitLab's only/except)

If you prefer to manage branch-specific logic in your build script (similar to GitLab's only/except), you can add conditional commands to your buildspec.yml:

phases:
  build:
    commands:
      - |
        if [ "$CODEBUILD_SOURCE_BRANCH" == "master" ]; then
          echo "Running production build steps..."
          # Add your master-specific build/deploy prep commands here
        else
          echo "Running dev branch build steps..."
          # Add commands for non-master branches (e.g., run tests only)
        fi

Then, pair this with a manual approval stage in CodePipeline for non-master branches (using the conditional action method from Option 1) before any deployment steps.

Example CloudFormation Snippet (For CodeStar Projects)

Since CodeStar projects are defined with CloudFormation, here's a quick snippet to update your pipeline configuration:

# CloudWatch Event Rule to trigger all branches
AllBranchTriggerRule:
  Type: AWS::Events::Rule
  Properties:
    EventPattern:
      source: ["aws.codecommit"]
      detail-type: ["CodeCommit Repository State Change"]
      detail:
        event: ["referenceCreated", "referenceUpdated"]
        referenceType: ["branch"]
        repositoryName: [!Ref CodeCommitRepository]
    Targets:
      - Arn: [!Ref Pipeline]
        Id: PipelineTarget
        RoleArn: [!GetAtt CloudWatchEventsExecutionRole.Arn]

# Updated Pipeline with conditional actions
Pipeline:
  Type: AWS::CodePipeline::Pipeline
  Properties:
    Stages:
      - Name: Source
        Actions:
          - Name: SourceAction
            ActionTypeId:
              Category: Source
              Owner: AWS
              Provider: CodeCommit
              Version: '1'
            Configuration:
              RepositoryName: [!Ref CodeCommitRepository]
              # No BranchName specified to accept all branches
            OutputArtifacts:
              - Name: SourceArtifact
      - Name: Build
        Actions:
          - Name: BuildAction
            ActionTypeId:
              Category: Build
              Owner: AWS
              Provider: CodeBuild
              Version: '1'
            Configuration:
              ProjectName: [!Ref CodeBuildProject]
            InputArtifacts:
              - Name: SourceArtifact
      - Name: Deploy
        Actions:
          # Manual approval only for non-master branches
          - Name: DevApproval
            ActionTypeId:
              Category: Approval
              Owner: AWS
              Provider: Manual
              Version: '1'
            RunOrder: 1
            Conditions:
              - Type: StringEquals
                Variable: CODEBUILD_SOURCE_BRANCH
                Value: "master"
                Negate: true
          # Auto-deploy to production only for master
          - Name: DeployProduction
            ActionTypeId:
              Category: Deploy
              Owner: AWS
              Provider: ElasticBeanstalk
              Version: '1'
            RunOrder: 2
            Configuration:
              ApplicationName: [!Ref ElasticBeanstalkApplication]
              EnvironmentName: [!Ref ElasticBeanstalkEnvironment]
            InputArtifacts:
              - Name: BuildArtifact
            Conditions:
              - Type: StringEquals
                Variable: CODEBUILD_SOURCE_BRANCH
                Value: "master"

This setup will trigger your pipeline on every branch commit, run auto-deploys for master, and require manual approval before deploying any non-master branches.

内容的提问来源于stack exchange,提问作者Anum Sheraz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 07:38:52