0

我有这个 go.yml 用于 github 操作

name: Test

on: [push, pull_request]

jobs:

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

    - name: Set up Go 1.15
      uses: actions/setup-go@v2
      with:
        go-version: 1.15
      id: go

    - name: Check out code
      uses: actions/checkout@v2

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

    - name: Build
      run: go build -v ./...

    - name: Test
      run: go test -v ./...

它构建时出现错误:home/runner/work/project/project 不在已知的 GOPATH/src 中错误:进程已完成,退出代码为 1。

如何解决它的问题?

4

1 回答 1

0

GOPATH的默认值为$HOME/go

您的项目文件夹不在此范围内,GOPATH因此出现错误。

您有两种方法可以解决此问题。

  1. (首选)更新您的项目以使用 go.mod。它是 go 中更新、更好的依赖管理解决方案,并且不需要您的项目在GOPATH.

假设您使用的 Go 版本高于 1.12,请删除Gopkg.tomland Gopkg.lock(如果有的话)。

跑,

一个。go mod init <project-name>替换<project-name>为您的项目名称。

湾。运行go mod tidy,它将添加您在项目中使用的所有依赖项。

C。运行go build一次以确保您的项目仍然可以构建。如果没有,您可以在go.mod.

提交go.modgo.sum(如果您需要确定性构建)。

从您的 CI 配置中删除它,

 if [ -f Gopkg.toml ]; then
              curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh
              dep ensure
          fi

并构建项目。它应该工作。

  1. GOPATH在调用dep ensure. 我认为 GOPATH=/home/runner/work/project/project应该可以,但我不知道与之相关的确切细节,GOPATH因此您只需要尝试即可。
于 2020-11-26T04:17:52.783 回答