Golang中Gitlab-ci.yaml的故障排查指南
Golang中Gitlab-ci.yaml的故障排查指南 我的一个GitLab CI作业失败了,我想了解在.gitlab-ci.yaml中注入Go路径的正确语法。我遇到了未知别名错误:* Unknown alias: inject-gopath
PACKAGE_PATH: /Users/rodneybizzell/go/src/github.com/rbizzell40/gitlab-ci
stages:
- dep
- test
- build
.anchors:
- &inject-gopath
mkdir -p $(dirname ${PACKAGE_PATH})
&& ln -s ${CI_PROJECT_DIR} ${PACKAGE_PATH}
&& cd ${PACKAGE_PATH}
dep:
stage: dep
image: golang:1.10-alpjine3.7
before_script:
- apk add --no-cache curl git
- curl -sSL https://github.com/golang/dep/releases/download/v0.5.0/dep-linux-amd64 -o /go/bin/dep
- chmod +x /go/bin/dep
- *inject-gopath
script:
- dep ensure -v -vendor-only
artifacts:
name: "vendor=$CI_PIPELINE_ID"
paths:
- vendor/
expire_in: 1 hour
test:
stage:
dependencies:
- dep
image: golang:1.10-alpine3.7
before_script:
- *inject-gopath
script:
- go test ./...
build:
stage: build
dependencies:
- dep
image: docker:17
services:
- docker:dind
script:
- docker build -t app .
更多关于Golang中Gitlab-ci.yaml的故障排查指南的实战教程也可以访问 https://www.itying.com/category-94-b0.html
好的,非常感谢您的帮助。
更多关于Golang中Gitlab-ci.yaml的故障排查指南的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html
我不太确定您想在这里实现什么。
您能详细说明一下吗?
你能指出我该如何理解这个问题吗?这就是我寻求帮助的原因。告诉我用错了并不能帮助我。如果你能帮忙,能否尝试用更专业的方式交流?你不必表现得那么傲慢。
不,问题出在前一行的语法上。& 符号用于引入锚点,但你不能那样使用它,所以需要正确使用或者完全不用。
我尝试使用 Golang 构建这个 Docker 镜像。需要传递包含 Go 代码的包目录路径,但似乎不支持 mkdir -p $(dirname ${PACKAGE_PATH}) 这种语法。
func main() {
fmt.Println("hello world")
}
抱歉,我本意并非如此。在手机上写文本确实很困难。
从您展示的代码片段来看,inject-gopath 并没有在任何地方使用,最简单的办法可能是删除它。
但这样的话,.anchors 就会变成无效操作。
所以我的推测是,您实际想要的是这样的:
.anchors: &inject-gopath
-|
mkdir -p $(dirname ${PACKAGE_PATH})
&& ln -s ${CI_PROJECT_DIR} ${PACKAGE_PATH}
&& cd ${PACKAGE_PATH}
然后您只需要在适当的地方使用这个锚点,比如在 before 脚本中像这样:
before_script:
<<: *Inject-gopath
附注:语法上可能还存在一些小错误。我仍在用手机操作,手边没有 YAML 参考文档。
在您的.gitlab-ci.yaml文件中,问题出现在使用YAML锚点(anchor)的语法上。GitLab CI不支持在before_script中直接使用锚点别名*inject-gopath作为命令执行。您需要将锚点定义为脚本命令列表,然后在作业中通过别名引用它。
以下是修正后的配置示例:
PACKAGE_PATH: /Users/rodneybizzell/go/src/github.com/rbizzell40/gitlab-ci
stages:
- dep
- test
- build
.inject-gopath: &inject-gopath
- mkdir -p $(dirname ${PACKAGE_PATH})
- ln -s ${CI_PROJECT_DIR} ${PACKAGE_PATH}
- cd ${PACKAGE_PATH}
dep:
stage: dep
image: golang:1.10-alpine3.7
before_script:
- apk add --no-cache curl git
- curl -sSL https://github.com/golang/dep/releases/download/v0.5.0/dep-linux-amd64 -o /go/bin/dep
- chmod +x /go/bin/dep
- *inject-gopath
script:
- dep ensure -v -vendor-only
artifacts:
name: "vendor=$CI_PIPELINE_ID"
paths:
- vendor/
expire_in: 1 hour
test:
stage: test
dependencies:
- dep
image: golang:1.10-alpine3.7
before_script:
- *inject-gopath
script:
- go test ./...
build:
stage: build
dependencies:
- dep
image: docker:17
services:
- docker:dind
before_script:
- *inject-gopath
script:
- docker build -t app .
主要修改点:
- 将锚点
.inject-gopath定义为YAML列表(使用-前缀的多个命令) - 修正了
test作业的stage定义(从stage:改为stage: test) - 在
build作业中也添加了before_script来注入GOPATH
这样配置后,*inject-gopath会在各个作业的before_script中正确展开为三个独立的shell命令执行。

