如何解决在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 举报,一经查实,本站将立刻删除。