GitHub Actions 自动关闭 issue 为啥不生效?
我写了个 GitHub Actions 工作流,想在 push 到 main 分支时自动关闭带特定标签的 issue,但一直没反应。
试过用 github.event.label.name 判断标签,也检查了 token 权限,还是不行。这是我的 workflow 配置:
name: Close Issues
on:
push:
branches: [main]
jobs:
close-issues:
runs-on: ubuntu-latest
steps:
- uses: actions/github-script@v6
with:
script: |
const issues = await github.rest.issues.listForRepo({
owner: context.repo.owner,
repo: context.repo.repo,
labels: 'stale'
});
for (const issue of issues.data) {
await github.rest.issues.update({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issue.number,
state: 'closed'
});
}
github-token: ${{ secrets.GITHUB_TOKEN }}
你是 push 到 main 时触发,但
github.event.label这个东西只有在 issue 相关事件(比如 labeled)里才有,push 事件根本拿不到 label 信息。你实际想要的效果应该是:当有人给 issue 打上 'stale' 标签时自动关闭,对吧?那触发器应该用
issues事件:这样当标签被添加时才会触发,而且用
context.issue.number就能拿到当前 issue 编号,不需要再查询一圈。另外你原来的写法用 push 每次都查全库,效率低不说,逻辑也不对。