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

基于GitHub Actions的Git分支策略CI/CD工作流异常排查

问题分析与CI/CD工作流修复方案

分支策略

分支名称对应环境
mainprod
release/*uat
feature/*dev

预期执行流程

  • 推送代码到feature/developer-1分支时,自动触发构建+部署任务,部署到dev环境
  • 发起PR合并到release/test-release分支后,再次触发构建+部署任务,且基于合并后的release分支执行

当前存在的问题

  1. PR合并到release分支时,工作流直接跳过任务,完全不执行
  2. 即使触发任务,也是在feature分支上运行,而非合并后的release分支

问题根源

  1. 事件触发逻辑不对:GitHub的pull_request事件默认不会在PR合并完成后触发,原工作流里的if条件试图匹配合并状态,但事件本身没触发,自然不会执行
  2. 代码分支检出错误:PR事件中默认拉取的是PR的源分支(feature分支),不是合并后的目标分支(release分支)

修复后的完整工作流代码

name: mock CI/CD

on:
  push:
    branches:
      - 'feature/*'
      - 'release/*'  # 新增:监听release分支的推送(包括PR合并后的自动推送)
  pull_request:
    branches:
      - 'release/*'
    types: [closed]  # 新增:只监听PR关闭事件(合并PR属于关闭的一种情况)

jobs:
  build:
    runs-on: ubuntu-latest
    if: |
      # 匹配feature分支的推送事件
      (github.event_name == 'push' && startsWith(github.ref, 'refs/heads/feature/')) ||
      # 匹配release分支的推送事件(含PR合并后的推送)
      (github.event_name == 'push' && startsWith(github.ref, 'refs/heads/release/')) ||
      # 匹配合并到release分支的PR关闭事件
      (github.event_name == 'pull_request' && github.event.pull_request.merged == true && startsWith(github.event.pull_request.base.ref, 'release/'))
    steps:
      - name: 检出代码
        uses: actions/checkout@v3
        with:
          # 动态指定检出分支:PR合并时拉取目标release分支,其他情况拉取当前推送分支
          ref: ${{ github.event_name == 'pull_request' && github.event.pull_request.base.ref || github.ref }}

      - name: 获取提交信息
        run: |
          echo "Commit message: $(git log -1 --pretty=%B)" > commit_message.txt

      - name: 显示提交信息
        run: cat commit_message.txt

      - name: 创建构建产物
        run: |
          echo "Mock build complete."
          mv commit_message.txt file_build.txt

      - name: 上传构建产物
        uses: actions/upload-artifact@v3
        with:
          name: build
          path: file_build.txt

  deploy:
    runs-on: ubuntu-latest
    needs: build
    steps:
      - name: 检出代码
        uses: actions/checkout@v3
        with:
          ref: ${{ github.event_name == 'pull_request' && github.event.pull_request.base.ref || github.ref }}

      - name: 下载构建产物
        uses: actions/download-artifact@v3
        with:
          name: build
          path: .

      - name: 重命名构建产物
        run: mv file_build.txt file_deploy.txt

      - name: 上传部署产物
        uses: actions/upload-artifact@v3
        with:
          name: deploy
          path: file_deploy.txt

修复点说明

  1. 事件监听调整
    • 给push事件加上release/*分支监听:PR合并到release分支后,GitHub会自动向release分支推送合并后的代码,这时候push事件会触发任务
    • 给pull_request事件添加types: [closed]:确保PR合并(属于关闭场景)时触发事件,作为补充触发条件
  2. 条件判断优化
    • 拆分了三个独立的条件逻辑,分别对应三种需要触发任务的场景,逻辑更清晰,避免条件组合错误
  3. 分支检出修正
    • 在actions/checkout步骤里新增ref参数,根据事件类型动态选择要检出的分支:PR合并时拉取目标release分支,保证任务基于合并后的代码运行;其他场景拉取当前推送的分支

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.19 16:54:59