如何在提交者属于指定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
相关产品推荐
相关产品推荐

