开发技巧

GitHub 仓库维护自动化:Issue 模板、Dependabot 与自动标签

给开源仓库配一套维护自动化:结构化的问题反馈表单、PR 模板、Dependabot 每周一早上更新依赖和 Action 版本、按改动文件自动打标签、清理 30 天无人回复的 issue。

BINBHO · 图文教程

给开源仓库配一套维护自动化:结构化的问题反馈表单、PR 模板、Dependabot 每周一早上更新依赖和 Action 版本、按改动文件自动打标签、清理 30 天无人回复的 issue。

先看清成品

全部配置文件已在本机用 check-jsonschema 的内置 schema 校验通过(issue 表单、issue 配置、dependabot、workflow 各一种),两个 workflow 也通过了 actionlint 1.7.12。没有放到真实仓库里运行,Dependabot 和 Actions 的实际效果需要推送后观察。

注意:遵守 GitHub 服务条款。不要在仓库、日志或 issue 里提交密钥和个人数据;不要用 Actions 挖矿或做与仓库无关的大量计算;不要共享账号。

做完之后你会得到什么?

成果 说明
Bug 表单 版本、复现步骤、系统必填
PR 模板 自查清单含“不提交密钥”
Dependabot pip 和 Actions 每周一 09:00(北京时间)检查,按组合并 PR
自动标签 文档 / CI / 依赖
过期清理 issue 30 天无动态标记,再 14 天关闭;PR 不处理

需要准备哪些环境?

项目 要求 说明
GitHub 仓库 有管理员权限 需要改 Settings
标签 预先创建 文档、CI、依赖、等待回复、已确认、安全、bug、待分类
校验工具 pip install check-jsonschema,actionlint 本地校验

文件结构

19-github-C-src/.github/
├── ISSUE_TEMPLATE/
│   ├── bug_report.yml
│   └── config.yml
├── pull_request_template.md
├── dependabot.yml
├── labeler.yml
└── workflows/
    ├── labeler.yml
    └── stale.yml

第 1 步:问题反馈表单

.github/ISSUE_TEMPLATE/bug_report.yml:

name: 问题反馈
description: 报告一个可以复现的错误
title: "[Bug] "
labels: ["bug", "待分类"]
body:
  - type: markdown
    attributes:
      value: 提交前请先搜索已有 issue。不要贴出密码、令牌或个人隐私信息。
  - type: input
    id: version
    attributes:
      label: 版本号
      placeholder: 例如 0.3.1
    validations:
      required: true
  - type: textarea
    id: steps
    attributes:
      label: 复现步骤
      value: |
        1.
        2.
        3.
    validations:
      required: true
  - type: textarea
    id: expected
    attributes:
      label: 期望结果与实际结果
    validations:
      required: true
  - type: dropdown
    id: os
    attributes:
      label: 操作系统
      options:
        - Windows
        - macOS
        - Linux
        - 其他
    validations:
      required: true
  - type: textarea
    id: logs
    attributes:
      label: 日志
      description: 粘贴相关日志,会自动按代码格式显示
      render: shell

.github/ISSUE_TEMPLATE/config.yml(把 OWNER/REPO 换成你的仓库):

blank_issues_enabled: false
contact_links:
  - name: 使用问题请到讨论区
    url: https://github.com/OWNER/REPO/discussions
    about: 怎么用、要不要加功能,先在讨论区聊

关闭空白 issue 后,用户只能选表单或去讨论区。讨论区需要先在 Settings 里开启 Discussions。

第 2 步:PR 模板

.github/pull_request_template.md:

## 改了什么

## 为什么改

关联 issue:Closes #

## 自查

- [ ] 本地测试通过
- [ ] 更新了文档(如需要)
- [ ] 没有提交密钥、令牌或个人数据

第 3 步:Dependabot

.github/dependabot.yml:

version: 2
updates:
  - package-ecosystem: "pip"
    directory: "/"
    schedule:
      interval: "weekly"
      day: "monday"
      time: "09:00"
      timezone: "Asia/Shanghai"
    open-pull-requests-limit: 5
    groups:
      dev-tools:
        patterns: ["pytest*", "ruff"]
  - package-ecosystem: "github-actions"
    directory: "/"
    schedule:
      interval: "weekly"
      day: "monday"
      time: "09:00"
      timezone: "Asia/Shanghai"
    groups:
      actions:
        patterns: ["*"]

groups 把同组更新合成一个 PR,避免一周十几个 PR。github-actions 会更新 workflow 里的 actions/checkout@v7 这类版本号。

第 4 步:自动打标签

.github/labeler.yml(labeler v5 起的格式):

文档:
  - changed-files:
      - any-glob-to-any-file: ["docs/**", "**/*.md"]
CI:
  - changed-files:
      - any-glob-to-any-file: ".github/workflows/**"
依赖:
  - changed-files:
      - any-glob-to-any-file: ["pyproject.toml", "requirements*.txt"]

.github/workflows/labeler.yml:

name: 自动打标签

on:
  pull_request_target:
    types: [opened, synchronize, reopened]

permissions:
  contents: read
  pull-requests: write

jobs:
  label:
    runs-on: ubuntu-latest
    steps:
      # 只读取 .github/labeler.yml 和变更文件列表,不检出也不运行 PR 里的代码
      - uses: actions/labeler@v7

安全提示:pull_request_target 以目标仓库的权限运行,能写 PR。这里只用 labeler 读取变更文件列表,不要在这个 workflow 里 checkout 并运行 PR 分支的代码,否则外部贡献者的代码会拿到写权限。

labeler 不给 issues: write 时不会自动创建标签,所以要先在仓库里建好“文档、CI、依赖”三个标签。

第 5 步:过期 issue 清理

.github/workflows/stale.yml:

name: 清理长期无响应的 issue

on:
  schedule:
    - cron: "30 1 * * *"   # UTC 01:30,即北京时间 09:30
  workflow_dispatch:

permissions:
  issues: write
  pull-requests: write

jobs:
  stale:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/stale@v11
        with:
          days-before-stale: 30
          days-before-close: 14
          stale-issue-label: 等待回复
          exempt-issue-labels: 已确认,安全
          stale-issue-message: 这个 issue 已 30 天没有新动态,14 天内仍无回复将自动关闭。如果问题还在,留言即可保持打开。
          days-before-pr-stale: -1
          days-before-pr-close: -1

days-before-pr-stale: -1 和 days-before-pr-close: -1 表示 PR 不标记也不关闭;带“已确认”或“安全”标签的 issue 不处理。

第 6 步:本地校验

pip install check-jsonschema
check-jsonschema --builtin-schema vendor.github-issue-forms .github/ISSUE_TEMPLATE/bug_report.yml
check-jsonschema --builtin-schema vendor.github-issue-config .github/ISSUE_TEMPLATE/config.yml
check-jsonschema --builtin-schema vendor.dependabot .github/dependabot.yml
check-jsonschema --builtin-schema vendor.github-workflows .github/workflows/*.yml
actionlint .github/workflows/*.yml

检查结果:每条 check-jsonschema 输出 ok -- validation done,actionlint 无输出。

第 7 步:推送后检查

  • 新建 issue:只能看到“问题反馈”表单和讨论区链接。
  • 开一个只改 README 的 PR:自动加上“文档”标签。
  • Insights → Dependency graph → Dependabot:能看到两个生态的检查记录。
  • Actions 里手动运行一次 stale(workflow_dispatch),看日志里处理了哪些 issue。

调试示例

把 dependabot.yml 里的 interval: "weekly" 写成 interval: "every-week",本机实际输出(节选):

Schema validation errors were encountered.
  dependabot.yml::$.updates[0].schedule.interval: 'every-week' is not one of ['daily', 'weekly', 'monthly', 'quarterly', 'semiannually', 'yearly', 'cron']

报错直接列出合法值。先本地校验,不用推送后再等 Dependabot 报错。

出错了怎么办?

现象 原因 解决
新建 issue 时看不到表单 文件不在默认分支或 YAML 有错 合并到默认分支,跑第 6 步校验
PR 没有自动标签 标签不存在 先在仓库里创建标签
labeler 报 Resource not accessible 缺 pull-requests: write 按本文 permissions 写
Dependabot 一次开很多 PR 没设 groups 加 groups 或调 open-pull-requests-limit
stale 关掉了重要 issue 没加豁免标签 给它们加“已确认”

模板规则

  • 所有自动化配置放在 .github/,改动前跑第 6 步校验。
  • pull_request_target 里不运行 PR 代码。
  • Dependabot 时间写明时区。

项目资源

用途 地址
Issue 表单语法 https://docs.github.com/en/communities/using-templates-to-encourage-useful-issues-and-pull-requests/syntax-for-issue-forms
Dependabot 配置参考 https://docs.github.com/en/code-security/dependabot/working-with-dependabot/dependabot-options-reference
actions/labeler https://github.com/actions/labeler
actions/stale https://github.com/actions/stale
Actions 安全使用指南 https://docs.github.com/en/actions/reference/security/secure-use
check-jsonschema https://github.com/python-jsonschema/check-jsonschema

这些配置单个看都很小,合在一起能让维护者少做大量重复劳动:问题反馈有固定格式,依赖更新按周来,标签自动打,没人回复的 issue 也不会一直堆着。