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

如何在提交者属于指定Github团队时跳过Github Actions步骤?

实现GitHub CI步骤的条件化配置

核心思路

通过GitHub Actions的github-script工具检查提交者是否属于指定团队,动态调整publish-unit-test-result-action的comment_mode参数:属于目标团队时设为off,否则保持failures。

修改后的CI配置

# 新增步骤:检查提交者团队身份
- name: 检查提交者团队身份
  id: check_team
  uses: actions/github-script@v6
  with:
    script: |
      const targetTeam = "Don't run the summary step";
      // 区分PR提交和直接提交的情况,获取提交者用户名
      const committer = context.payload.pull_request ? context.payload.pull_request.user.login : context.actor;
      
      try {
        // 获取目标团队信息
        const team = await github.rest.teams.getByName({
          org: context.repo.owner,
          team_slug: targetTeam.replace(/\W+/g, '-').toLowerCase()
        });
        
        // 检查提交者是否在团队中
        await github.rest.teams.getMembershipForUser({
          team_id: team.data.id,
          username: committer
        });
        
        console.log(`${committer} 属于目标团队`);
        return true;
      } catch (err) {
        if (err.status === 404) {
          console.log(`${committer} 不属于目标团队`);
          return false;
        }
        throw err;
      }

# 修改原有测试结果发布步骤
- name: Publish test results
  uses: EnricoMi/publish-unit-test-result-action@v2
  if: always()
  with:
    junit_files: "tmp/jest*.xml"
    # 根据团队检查结果动态设置comment_mode
    comment_mode: ${{ steps.check_team.outputs.result == 'true' && 'off' || 'failures' }}
    check_name: "Javascript jest tests"

注意事项

  • 权限配置:需确保GitHub Actions的运行令牌(GITHUB_TOKEN)拥有read:org权限。在仓库的「Settings → Actions → General → Workflow permissions」中,勾选「Read and write permissions」即可满足需求。
  • 团队Slug处理:脚本会自动将团队名称转换为GitHub识别的slug格式(小写、非字母数字字符替换为连字符),无需手动修改。
  • 场景覆盖:同时支持PR提交和直接推送代码的场景,确保所有提交者的身份检查都生效。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 20:42:43