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

如何在GitHub Actions中重置github run_number计数器?

解决GitHub Actions计数器重置问题

你尝试的YAML配置中,RELEASE_SUBVERSION2: ${{ 31 - github.run_number }}的问题在于运算符-前后多了冗余空格,修正为${{ 31 - github.run_number }}后表达式本身是合法的,但这种方式只是通过算术运算模拟递减,无法真正重置GitHub自带的github.run_number(该计数器是仓库级自动递增的,无法直接修改)。

要实现可控的计数器重置,推荐以下两种方案:

方案一:使用自定义仓库变量维护计数器

  1. 进入仓库的「Settings」→「Secrets and variables」→「Actions」,创建一个名为CUSTOM_RUN_NUMBER的变量,初始值设为31。
  2. 在工作流中使用actions/github-script更新变量并读取:
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: 更新自定义计数器
        uses: actions/github-script@v6
        with:
          script: |
            const { repo } = context;
            // 获取当前变量值
            const varsRes = await github.rest.actions.getRepoVariables({ ...repo });
            const targetVar = varsRes.data.variables.find(v => v.name === 'CUSTOM_RUN_NUMBER');
            let newCount = parseInt(targetVar.value) - 1;
            // 重置逻辑:当计数器减到0时,恢复为31
            if (newCount < 0) newCount = 31;
            // 更新变量
            await github.rest.actions.updateRepoVariable({
              ...repo,
              name: 'CUSTOM_RUN_NUMBER',
              value: newCount.toString()
            });
      - name: 使用计数器
        run: |
          echo "RELEASE_VERSION=1"
          echo "RELEASE_SUBVERSION1=0"
          echo "RELEASE_SUBVERSION2=${{ vars.CUSTOM_RUN_NUMBER }}"

方案二:基于模运算实现循环计数器

如果不需要严格重置,只是需要循环的数值范围,可以用模运算替代:

env:
  RELEASE_VERSION: 1
  RELEASE_SUBVERSION1: 0
  # 实现0-30循环的数值,当run_number超过31时自动循环
  RELEASE_SUBVERSION2: ${{ 30 - (github.run_number % 31) }}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.18 11:05:41