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

实现自动标记blocked Issue的GitHub Actions工作流,测试时CLI认证失败

GitHub Actions工作流测试时GitHub CLI认证失败问题

需求背景

我正尝试实现一个GitHub Actions工作流:当Issue被打开、编辑或重新打开时,若其依赖的任务仍处于进行状态,自动将该Issue标记为blocked。技术方案包括在仓库的.github/workflows目录创建YAML文件、监听对应Issue事件、用GitHub CLI获取依赖Issue状态、用jq解析数据等。

已完成操作

  • 创建了.github/workflows/label-blocked-issues.yml工作流文件,内容如下:
on:
  issues:
    types:
      - opened
      - edited
      - reopened

jobs:
  label-blocked-issues:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Repository
        uses: actions/checkout@v3

      - name: Install GitHub CLI
        run: |
          sudo apt-get update
          sudo apt-get install gh

      - name: Authenticate with GitHub
        run: |
          echo -e "machine github.com\nlogin $GITHUB_ACTOR\npassword ${{ secrets.GITHUB_TOKEN }}" > ~/.netrc
          chmod 600 ~/.netrc

      - name: Find and Label Blocked Issues
        id: find-blocked-issues
        run: |
          body=$(jq -r .issue.body $GITHUB_EVENT_PATH)
          dependencies=$(echo "$body" | grep -oE "#[0-9]+" | sed 's/#//g' || true)

          found_blocked=false
          for issue in $dependencies; do
            status=$(curl -s -H "Authorization: Bearer $GITHUB_TOKEN" \
              -H "Accept: application/vnd.github.v3+json" \
              "https://api.github.com/repos/${{ github.repository }}/issues/$issue" | jq -r .state)

            if [ "$status" != "closed" ]; then
              echo "Issue #$issue is still open"
              found_blocked=true
            fi
          done

          if [[ "$found_blocked" == true ]]; then
            echo "::set-output name=blocked::true"
          fi

      - name: Label Issue as Blocked if
        if: steps.find-blocked-issues.outputs.blocked == 'true'
        run: |
          echo "Labeling issue as blocked..."
          echo "GitHub Repository: ${{ github.repository }}"
          echo "Issue Number: ${{ github.event.issue.number }}}"

          # Use the GitHub token directly in the command
          gh api --method POST \
            -H "Accept: application/vnd.github+json" \
            -H "X-GitHub-Api-Version: 2022-11-28" \
            -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
            /repos/${{ github.repository }}/issues/${{ github.event.issue.number }}/labels \
            -f "labels[]=blocked"
  • 创建了同目录下的test-event.json测试文件:
{
  "action": "opened",
  "issue": {
    "number": 1,
    "title": "Test Issue",
    "body": "Depends on #123",
    "state": "open"
  }
}
  • 使用Nektos/Act工具执行测试命令:
    act -v -e .github/workflows/test-event.json -W . -j label-blocked-issues
    
    或简化版:
    act -e .github/workflows/test-event.json -W . -j label-blocked-issues
    

问题现象

测试时所有步骤均通过,但最后一步"Label Issue as Blocked if"执行失败,输出如下:

[label-blocked-issues.yml/label-blocked-issues] ⭐ Run Main Label Issue as Blocked if
[label-blocked-issues.yml/label-blocked-issues]   🐳  docker exec cmd=[bash --noprofile --norc -e -o pipefail /var/run/act/workflow/4] user= workdir=
| Labeling issue as blocked...
| GitHub Repository: treasure-house-of-projects/skarb-ngo
| Issue Number: 1
| Welcome to GitHub CLI!
| 
| To authenticate, please run `gh auth login`.
[label-blocked-issues.yml/label-blocked-issues]   ❌  Failure - Main Label Issue as Blocked if
[label-blocked-issues.yml/label-blocked-issues] exitcode '4': failure
[label-blocked-issues.yml/label-blocked-issues] 🏁  Job failed
Error: Job 'label-blocked-issues' failed

核心问题是无法通过GitHub CLI认证,工具提示需要执行gh auth login。

已尝试的解决方法

  • 调整GITHUB_TOKEN、secrets.GITHUB_TOKEN的多种使用方式
  • 在需要认证的步骤中直接执行:
    gh auth login --with-token ${{ secrets.GITHUB_TOKEN }}
    

但所有尝试都无法在Nektos/Act测试工具中完成GitHub CLI认证。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.05 19:20:22