我有以下.gitlab-ci.yml
stages:
- test
- build
- art
image: golang:1.9.2
variables:
BIN_NAME: example
ARTIFACTS_DIR: artifacts
GO_PROJECT: example
GOPATH: /go
before_script:
- mkdir -p ${GOPATH}/src/${GO_PROJECT}
- mkdir -p ${CI_PROJECT_DIR}/${ARTIFACTS_DIR}
- go get -u github.com/golang/dep/cmd/dep
- cp -r ${CI_PROJECT_DIR}/* ${GOPATH}/src/${GO_PROJECT}/
- cd ${GOPATH}/src/${GO_PROJECT}
test:
stage: test
script:
# Run all tests
go test -run ''
build:
stage: build
script:
# Compile and name the binary as `hello`
- go build -o hello
- pwd
- ls -l hello
# Execute the binary
- ./hello
# Move to gitlab build directory
- mv ./hello ${CI_PROJECT_DIR} artifacts:
paths:
- ./hello问题是我的程序依赖于Go和Mysql。
我知道我可以为每个阶段提供不同的docker镜像,但我的测试阶段需要这两个镜像。
go test & MySql我所调查的内容:
我已经学会了如何使用docker commit创建自己的docker镜像,以及如何使用docker文件来构建和镜像。
然而,我听说有一种方法可以使用docker compose将docker容器链接在一起,这似乎是一个更好的方法……
我不知道如何在GitLab中做到这一点,我知道我需要一个compose.yml文件,但不确定它放在哪里,它需要什么,它创建了一个图像,然后我链接到我的.gitlab-ci.yml文件?
也许这已经结束了,还有一种更简单的方法?
发布于 2019-06-27 19:56:41
我知道您的测试需要MySQL服务器才能工作,而且您在Go测试中使用了某种MySQL客户端或驱动程序。
您可以使用在test作业期间提供的Gitlab CI service。GitlabCI将在Go容器旁边运行一个MySQL容器,可以通过Go容器中的名称访问该容器。例如:
test:
stage: test
services:
- mysql:5.7
variables:
# Configure mysql environment variables (https://hub.docker.com/_/mysql/)
MYSQL_DATABASE: mydb
MYSQL_ROOT_PASSWORD: password
script:
# Run all tests
go test -run ''将启动一个MySQL容器,并且可以通过主机名mysql从Go容器访问它。注意:您需要为MySQL启动定义Environment variables section of image documentation (如要创建的根密码或数据库)的variables。
您还可以全局定义服务(将使其可用于构建中的每个作业)并使用alias,以便可以从另一个主机名访问MySQL服务器。
https://stackoverflow.com/questions/56788001
复制相似问题