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

迁移至GitHub Actions:PR提交.REAL_VERSION不触发构建的实现方案

GitHub Actions实现CircleCI迁移中的递归构建规避需求

完全可以实现你需要的需求,针对提交.REAL_VERSION触发递归构建的问题,GitHub Actions提供了多种灵活的解决方案,以下是具体实现方式:

方案1:直接忽略.REAL_VERSION文件变更触发构建

这是最直接的方式,通过paths-ignore配置限定触发buildMyStuff任务的文件范围,当仅.REAL_VERSION文件变更时,不会触发任务。

name: buildMyStuff
on:
  pull_request:
    # PR场景下,忽略仅.REAL_VERSION变更的情况
    paths-ignore:
      - '.REAL_VERSION'
  push:
    # 针对PR分支的push操作(CI提交.REAL_VERSION会触发push),忽略该文件变更
    branches-ignore:
      - 'main' # 排除main分支的push,避免影响部署流程
    paths-ignore:
      - '.REAL_VERSION'

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
      # ...你的构建、测试步骤
      - name: Update and commit .REAL_VERSION
        run: |
          git config --global user.name "github-actions[bot]"
          git config --global user.email "github-actions[bot]@users.noreply.github.com"
          echo "${{ github.run_number }}" > .REAL_VERSION
          git add .REAL_VERSION
          git commit -m "Update .REAL_VERSION with build number ${{ github.run_number }}"
          git push origin HEAD:${{ github.head_ref }}

方案2:根据提交者身份过滤

GitHub Actions执行git提交时,默认使用github-actions[bot]作为提交者。可以通过job的if条件判断提交者身份,跳过机器人提交触发的buildMyStuff任务。

name: buildMyStuff
on:
  pull_request:
  push:
    branches-ignore:
      - 'main'

jobs:
  build:
    runs-on: ubuntu-latest
    # 仅当提交者不是GitHub Actions机器人时才执行任务
    if: github.actor != 'github-actions[bot]'
    steps:
      # 构建步骤同方案1

方案3:通过提交信息关键词跳过任务

在CI提交.REAL_VERSION时,给提交信息添加特定关键词(比如[skip-buildMyStuff]),然后在workflow中判断该关键词,跳过任务触发。

name: buildMyStuff
on:
  pull_request:
  push:
    branches-ignore:
      - 'main'

jobs:
  build:
    runs-on: ubuntu-latest
    # 提交信息包含指定关键词时跳过任务
    if: "!contains(github.event.head_commit.message, '[skip-buildMyStuff]')"
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
      # ...构建步骤
      - name: Update and commit .REAL_VERSION
        run: |
          git config --global user.name "github-actions[bot]"
          git config --global user.email "github-actions[bot]@users.noreply.github.com"
          echo "${{ github.run_number }}" > .REAL_VERSION
          git add .REAL_VERSION
          # 提交信息加入跳过关键词
          git commit -m "Update .REAL_VERSION with build number ${{ github.run_number }} [skip-buildMyStuff]"
          git push origin HEAD:${{ github.head_ref }}

方案4:组合方案(双重保险)

可以将上述方案组合使用,比如同时用paths-ignore和提交者判断,进一步避免误触发:

name: buildMyStuff
on:
  pull_request:
    paths-ignore:
      - '.REAL_VERSION'
  push:
    branches-ignore:
      - 'main'
    paths-ignore:
      - '.REAL_VERSION'

jobs:
  build:
    runs-on: ubuntu-latest
    if: github.actor != 'github-actions[bot]'
    steps:
      # 构建步骤

部署任务deployMyStuff的兼容配置

确保合并到main分支后,部署任务能正常触发并复用构建产物:

name: deployMyStuff
on:
  push:
    branches:
      - main

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
        with:
          fetch-depth: 0 # 拉取完整历史以支持打标签
      - name: Create Git tag from .REAL_VERSION
        run: |
          TAG_VERSION=$(cat .REAL_VERSION)
          git tag $TAG_VERSION
          git push origin $TAG_VERSION
      - name: Restore cached build artifacts
        uses: actions/cache@v3
        with:
          path: ./build
          key: ${{ github.sha }}-build # 用PR构建的SHA作为缓存键,复用产物
      # ...部署到预发布环境的步骤

以上方案均可独立或组合使用,完美解决CircleCI中[ci skip]会跳过部署的问题,既避免了递归构建,又能保证后续部署流程正常执行。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.18 22:50:36