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

如何让GitHub Action任务一天内随机运行1到50次?

GitHub Action每日随机运行1-50次配置调整方案

调整逻辑

  • 原配置仅每日触发1次,无法满足随机次数需求,因此拆分两步实现目标:
    1. 调高cron触发频率,保证每日至少有50次触发机会,作为任务执行的基础入口
    2. 每次触发时新增随机判断+当日执行次数校验逻辑,控制当日实际执行次数落在1~50区间内

具体修改步骤

1. 调整schedule的cron配置

将原0 0 * * *替换为*/28 * * * *,该配置每28分钟触发一次,单日总触发次数约51次,刚好覆盖50次的上限需求。

2. 新增执行控制逻辑

在原有步骤前新增随机判断、次数校验的步骤,同时修改提交逻辑避免重复提交冲突。

3. 完整修改后的配置文件

#1. Repository Fork
# 2. Modify the files A and B according to the procedure
# 3. After committing the modifications, push & Enjoy!

name: planting-grass

# A. Comment lines 8-11
# on:
# push:
# branches:
# - unknown

# B. Uncomment lines 14-16
on:
   schedule:
     - cron: '*/28 * * * *'

jobs:
  task:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Set current date and random seed
        id: vars
        run: |
          echo "::set-output name=date::$(date +'%Y-%m-%d')"
          echo "::set-output name=random_num::$(shuf -i 1-100 -n 1)"
          # 读取当日已执行次数,没有则默认为0
          if [ -f ./run_count_$(date +'%Y-%m-%d').log ]; then
            echo "::set-output name=run_count::$(cat ./run_count_$(date +'%Y-%m-%d').log)"
          else
            echo "::set-output name=run_count::0"
          fi
      - name: Random execution check
        id: check
        run: |
          # 执行规则:当日次数未满50次时,30%概率触发执行,当日0次且当前是23点后强制执行1次保证下限
          if [ ${{ steps.vars.outputs.run_count }} -ge 50 ]; then
            echo "::set-output name=run::false"
          elif [ ${{ steps.vars.outputs.run_count }} -eq 0 ] && [ $(date +'%H') -ge 23 ]; then
            echo "::set-output name=run::true"
          elif [ ${{ steps.vars.outputs.random_num }} -le 30 ]; then
            echo "::set-output name=run::true"
          else
            echo "::set-output name=run::false"
          fi
      - name: Execute commands
        if: steps.check.outputs.run == 'true'
        run: bash ./task.sh ${{ steps.vars.outputs.date }}
      - name: Update execution count
        if: steps.check.outputs.run == 'true'
        run: |
          # 更新当日执行次数
          echo $(( ${{ steps.vars.outputs.run_count }} + 1 )) > ./run_count_${{ steps.vars.outputs.date }}.log
          git add run_count_*.log
      - name: Commit files
        if: steps.check.outputs.run == 'true'
        run: |
          git config --global user.name "$(git --no-pager log --format=format:'%an' -n 1)"
          git config --global user.email "$(git --no-pager log --format=format:'%ae' -n 1)"
          git add date.txt
          git commit -m ${{ steps.vars.outputs.date }}
      - name: Push changes
        if: steps.check.outputs.run == 'true'
        uses: ad-m/github-push-action@master
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          branch: ${{ github.ref }}

注意事项

  • 可以通过调整shuf -i 1-100 -n 1的判断阈值,修改单次触发的执行概率,进而调整每日平均执行次数
  • GitHub Action的schedule触发存在10~15分钟的延迟属于正常现象,不会影响最终执行次数范围
  • 旧的执行次数日志文件可以定期清理,不影响日常运行

内容的提问来源于stack exchange,提问作者emarwa.mouma.35t

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.25 20:15:04