【问题标题】:Github Action : golang cannot find packageGithub Action : golang 找不到包
【发布时间】:2020-11-06 18:15:45
【问题描述】:

我将示例 github 操作设置到我的存储库。 sn-p 来了。

jobs:

  build:
    name: Build
    runs-on: ubuntu-latest
    steps:

    - name: Set up Go 1.x
      uses: actions/setup-go@v2
      with:
        go-version: ^1.13
      id: go

    - name: Check out code into the Go module directory
      uses: actions/checkout@v2

    - name: Get dependencies
      run: |
        go get -v -t -d ./...
        if [ -f Gopkg.toml ]; then
            curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh
            dep ensure
        fi

但工作在Get dependencies 处失败。错误就在这里。

package github.com/<organization-account>/<repo-name>/api/domain/repo: cannot find package "github.com/<organization-account>/<repo-name>/api/domain/repo" in any of:
    /opt/hostedtoolcache/go/1.14.4/x64/src/github.com/<organization-account>/<repo-name>/api/domain/repo (from $GOROOT)
    /home/runner/go/src/github.com/<organization-account>/<repo-name>/api/domain/repo (from $GOPATH)

当然。当go run main.go 时,我的代码在本地工作。我有go.modgo.sum

【问题讨论】:

  • go run 可能有效,但问题在于go get -v -t -d ./...。你的&lt;repo-name&gt; 是私人的吗?
  • 感谢您的回复。是的。我的存储库是私有的。
  • 看起来 actions/checkout@v2 没有将你的代码输出到 GOPATH 中,并且名称表明它期望克隆一个 module。如果您可以转换为模块,这将简单得多。否则,您必须确保您在 GOPATH 中签出到正确的位置。
  • 谢谢您的回复。如何将私有仓库转换为模块?

标签: go github-actions


【解决方案1】:

这不是 OP 的正确答案,但可能有助于通过搜索到达这里的人。

在你的 go 项目的根目录中:

go mod init
go mod tidy

提交并推送到 github,它现在应该可以工作了。

【讨论】:

  • 然后删除两个文件``` Gopkg.toml Gopkg.lock ```
  • 这个解决方案对我有用。应该是公认的答案。
  • 这甚至还没有接近答案,他得到 404 试图下载私人回购@DavidPi
  • @ChadGrant 你是绝对正确的。我检查了我的答案和 OP 的日期,似乎我错过了那个重要信息。我更新了我的回复,因为它可能与来自搜索引擎的人有关。
【解决方案2】:

您需要为 go get 或 go mod 设置一个令牌以下载私人存储库,这就是您获得 404 的原因

然后你需要配置 git 以使用该令牌。

- name: Setup Git
  run: git config --global url."https://${{ secrets.TOKEN }}:@github.com/".insteadOf "https://github.com"

我不使用 dep,但你应该使用 go mod download 而不是 go get 或 dep,因为你有一个 mod 文件。

【讨论】:

    猜你喜欢
    • 2020-01-02
    • 2021-04-25
    • 2018-11-17
    • 1970-01-01
    • 1970-01-01
    • 2014-04-21
    • 2023-04-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多