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

Github Actions+GoReleaser+ssh-agent报错:无法读取Username,终端提示禁用

解决GitHub Actions + GoReleaser + 私有Go仓库构建失败问题

问题背景

我有一个托管在私有Go仓库的CLI应用,依赖团队内部另一个私有Go仓库,本地构建完全正常。但在GitHub Action中使用GoReleaser执行发布构建时,始终卡在go mod tidy环节失败,具体报错:

"release failed after 6serror=hook failed: go mod tidy: exit status 1; output: go: downloading..."
"fatal: could not read Username for 'https://github.com': terminal prompts disabled"

已通过webfactory/ssh-agent加载了SSH私钥,但GoReleaser仍无法访问私有依赖仓库。

核心原因

Go默认使用HTTPS协议拉取GitHub仓库的依赖,即使SSH Agent已配置密钥,Go不会自动切换到SSH协议。在GitHub Action环境中,终端提示被禁用,导致HTTPS方式拉取私有仓库时需要用户名密码的步骤直接失败。

解决方案

1. 强制Git用SSH替代HTTPS访问GitHub仓库

在Action中添加Git配置步骤,让所有指向https://github.com/的请求自动切换为git@github.com:协议,这样Go mod会通过SSH Agent使用已配置的私钥拉取私有依赖。

2. 调整Action步骤顺序

确保Git配置步骤在代码拉取和GoReleaser执行之前完成,保证所有涉及仓库的操作都走SSH协议。

3. 验证部署密钥权限

确保SSH私钥对应的公钥已添加为所有私有依赖仓库的部署密钥(Deploy Key),如果需要写入权限,需勾选"Allow write access"选项。

修改后的GitHub Action脚本

name: Release

on:
  push:
    tags:
      - "v*.*.*"

jobs:
  build:
    name: Build Release Binaries
    runs-on: ubuntu-latest
    permissions:
      contents: write

    steps:
      - name: Setup SSH Agent
        uses: webfactory/ssh-agent@v0.5.4
        with:
          ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }}

      - name: Configure Git to use SSH for GitHub repos
        run: |
          git config --global url."git@github.com:".insteadOf "https://github.com/"

      - name: Setup Go 1.18
        uses: actions/setup-go@v3
        with:
          go-version: 1.18

      - name: Checkout code
        uses: actions/checkout@v3
        with:
          repository: 'myorg/myrepo'
          fetch-depth: 0

      - name: Run GoReleaser
        uses: goreleaser/goreleaser-action@v3
        with:
          distribution: goreleaser
          version: latest
          args: release --rm-dist
        env:
          GITHUB_TOKEN: ${{ secrets.TOKEN }}

修改说明

  • 新增Configure Git to use SSH for GitHub repos步骤,强制Git协议转换
  • 移除手动指定的checkout路径,避免GOPATH路径混乱,依赖actions/setup-go自动处理模块路径
  • 调整步骤顺序,确保SSH和Git配置在代码拉取前完成

内容的提问来源于stack exchange,提问作者A Bit of Help

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.26 04:45:15