在Gitlab CI中使用gomodules测试Go项目失败,并显示“软件包不在GOROOT中”

问题描述

我正在尝试使用以下.gitlab-ci.yml在Gitlab CI上构建和测试Go项目:

image: golang:latest

stages:
  - prepare
  - test
  - build
  - package

format:
  stage: prepare
  script:
    - make gofmt    

test:
  stage: test
  script:
    - make test

test race condition:
  stage: test
  script:
    - make race

coverage-html:
  stage: test
  script:
    - make cov

build win binary:
  stage: build
  script:    
    - make win

build linux binary:
  stage: build
  script:    
    - make linux
    
build mac binary:
  stage: build
  script:    
    - make mac

这是管道中使用的Makefile:

# Go parameters
GOCMD=go
GOBUILD=$(GOCMD) build
GOCLEAN=$(GOCMD) clean
GOTOOL=$(GOCMD) tool
GOTEST=$(GOCMD) test
GOTESTRACE=$(GOTEST) -race
GOGET=$(GOCMD) get
gofmt=$(GOCMD)fmt

BINARY_NAME=app
BINARY_DARWIN=$(BINARY_NAME)_darwin
BINARY_LINUX=$(BINARY_NAME)_linux
BINARY_WIN=$(BINARY_NAME)_win
    
all: build linux win

build: ## Download dependecies and Build the default binary      
        $(GOBUILD) -o $(BINARY_NAME) -v $(CURDIR)/cmd/app

linux: ## Build linux binary
        CGO_ENABLED=0 GOOS=linux GOARCH=amd64 $(GOBUILD) cmd/app -o $(BINARY_LINUX) -v $(CURDIR)/cmd/app

win: ## Build windows binary
        CGO_ENABLED=0 GOOS=windows GOARCH=amd64 $(GOBUILD) -o $(BINARY_WIN).exe -v  $(CURDIR)/cmd/app

mac: ## Build macOs binary
        CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 $(GOBUILD) -o $(BINARY_DARWIN) -v    $(CURDIR)/cmd/app

test: ## Run tests for the project
        $(GOTEST) -count=1 -coverprofile=cover.out -short -cover -failfast ./... -v

race: ## Run tests for the project (while detecting race conditions)
        $(GOTESTRACE) -coverprofile=cover.out -short -cover -failfast ./...

cov: test ## Run tests with HTML for the project
        $(GOTOOL) cover -html=cover.out

gofmt: ## gofmt code formating
    @echo Running go formating with the following command:
    $(gofmt) -e -s -w .

我正在使用gomodule来管理依赖项。我很难理解的是,如果我首先在build阶段运行作业,则管道能够下载依赖项并成功构建二进制文件。但是,在运行test阶段的作业时,它们全部失败,并显示以下错误

main.go:3:8: package app/command is not in GOROOT (/usr/local/go/src/app/command)
make: *** [Makefile:31: test] Error 1
Cleaning up file based variables
ERROR: Job Failed: command terminated with exit code 1

是否需要在我需要配置的Gitlab CI上测试基于gomodule的项目的其他设置?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)