如何配置GitHub Action在新PR自动评论时排除指定用户与团队
实现方案
该需求完全可实现,你可以通过GitHub Actions内置条件判断 + GitHub CLI的团队成员查询能力完成配置,具体如下:
方案1:仅排除指定用户(userA、userB)
直接通过GitHub Actions的上下文变量判断PR提交者是否属于排除列表,修改后的配置如下:
on: [pull_request] jobs: build: name: Comment a pull_request runs-on: ubuntu-latest # 此处添加if判断,仅当PR提交者不在排除列表时才执行整个Job if: ${{ github.event.pull_request.user.login != 'userA' && github.event.pull_request.user.login != 'userB' }} steps: - name: Checkout uses: actions/checkout@v4 - name: Comment a pull_request uses: mb2dev/github-action-comment-pull-request@1.0.0 with: message: "Hello, Thank you for this PR!" GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
方案2:同时支持排除指定用户 + 指定团队所有成员
需要新增一个步骤查询提交者是否属于待排除的团队,再根据查询结果判断是否执行评论步骤,完整配置如下:
on: [pull_request] jobs: build: name: Comment a pull_request runs-on: ubuntu-latest # 先过滤排除的指定用户 if: ${{ github.event.pull_request.user.login != 'userA' && github.event.pull_request.user.login != 'userB' }} steps: - name: Checkout uses: actions/checkout@v4 # 新增步骤:校验提交者是否属于待排除的团队 - name: Check if PR author is in excluded team id: check_team env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} PR_AUTHOR: ${{ github.event.pull_request.user.login }} # 替换为你的组织名和待排除的团队slug ORG_NAME: "你的组织名" EXCLUDED_TEAM: "要排除的团队slug" run: | # 查询用户是否在团队中,存在则输出is_excluded为true,否则为false if gh api orgs/$ORG_NAME/teams/$EXCLUDED_TEAM/memberships/$PR_AUTHOR > /dev/null 2>&1; then echo "is_excluded=true" >> $GITHUB_OUTPUT else echo "is_excluded=false" >> $GITHUB_OUTPUT fi - name: Comment a pull_request # 仅当用户不在排除团队中时执行评论 if: ${{ steps.check_team.outputs.is_excluded == 'false' }} uses: mb2dev/github-action-comment-pull-request@1.0.0 with: message: "Hello, Thank you for this PR!" GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
注意:使用团队排除功能时,需要在仓库的Action权限设置中,给默认的GITHUB_TOKEN添加
read:org权限,否则无法查询组织团队的成员信息。
内容的提问来源于stack exchange,提问作者Slava
相关产品推荐
相关产品推荐

