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

如何在GitHub Actions中持久化工作流输入变量以实现维护模式

实现GitHub工作流的持久化维护模式

方案一:使用GitHub仓库变量(推荐)

这是官方支持的持久化方式,可通过工作流自动更新变量,所有其他工作流能直接读取该值,无需手动修改。

  1. 初始配置
    先在仓库的Settings > Secrets and variables > Variables中创建MAINTENANCE变量,初始值设为false。

  2. 更新变量的手动工作流
    修改你的手动触发工作流,调用GitHub API更新仓库变量:

on:
  workflow_dispatch:
    inputs:
      maintenance:
        description: 'Set MAINTENANCE variable'
        required: true
        default: 'false'

permissions:
  administration: write  # 仅需此权限即可更新仓库变量

jobs:
  set-maintenance-mode:
    runs-on: ubuntu-latest
    steps:
      - name: Update MAINTENANCE repository variable
        uses: actions/github-script@v6
        with:
          script: |
            const { data: variables } = await github.rest.actions.getRepoVariables({
              owner: context.repo.owner,
              repo: context.repo.repo,
            });
            const existingVar = variables.variables.find(v => v.name === 'MAINTENANCE');
            
            if (existingVar) {
              await github.rest.actions.updateRepoVariable({
                owner: context.repo.owner,
                repo: context.repo.repo,
                name: 'MAINTENANCE',
                value: '${{ github.event.inputs.maintenance }}'
              });
            } else {
              await github.rest.actions.createRepoVariable({
                owner: context.repo.owner,
                repo: context.repo.repo,
                name: 'MAINTENANCE',
                value: '${{ github.event.inputs.maintenance }}'
              });
            }
  1. 其他工作流读取变量
    在代码提交触发的工作流中,直接通过${{ vars.MAINTENANCE }}读取状态并执行对应逻辑:
on: [push, pull_request]

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Skip build in maintenance mode
        if: ${{ vars.MAINTENANCE == 'true' }}
        run: echo "Maintenance mode active - skipping build process"
      - name: Execute normal build
        if: ${{ vars.MAINTENANCE == 'false' }}
        run: echo "Running standard build and deployment steps"

方案二:提交状态文件到仓库

通过在仓库中写入状态文件并提交的方式持久化维护状态,适合不想给工作流管理权限的场景。

  1. 更新状态文件的手动工作流
on:
  workflow_dispatch:
    inputs:
      maintenance:
        description: 'Set MAINTENANCE variable'
        required: true
        default: 'false'

jobs:
  set-maintenance-mode:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Write maintenance status to file
        run: echo "${{ github.event.inputs.maintenance }}" > .maintenance

      - name: Commit and push changes
        uses: ad-m/github-push-action@master
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          branch: ${{ github.ref }}
          message: "Update maintenance mode to ${{ github.event.inputs.maintenance }}"
  1. 其他工作流读取状态文件
on: [push, pull_request]

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Read maintenance status
        id: maintenance
        run: echo "status=$(cat .maintenance)" >> $GITHUB_OUTPUT

      - name: Maintenance mode logic
        if: ${{ steps.maintenance.outputs.status == 'true' }}
        run: echo "Skipping deployment - maintenance mode active"
      - name: Normal workflow logic
        if: ${{ steps.maintenance.outputs.status == 'false' }}
        run: echo "Executing full build and deployment pipeline"

方案对比与可行性说明

两种方案均无需手动修改变量,完全通过工作流自动完成状态持久化:

  • 仓库变量方案:无额外提交记录,状态管理简洁,适合大多数场景;
  • 文件提交方案:状态可回溯版本历史,无需特殊权限,但会产生额外的仓库提交。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.01 06:32:34