最近在调研devops这一块,看看业内的主流公司都是如何做一块的,看gitlab的ci/cd功能支持的挺完善,还支持pipeline,决定上手试一下。
申请一个仓库hello world,在仓库中创建三个文件,分别为main.go,hello.go和hello_test.go。
package main
import "fmt"
func main() {
str := hello()
fmt.Println(str)
}
package main
func hello() string {
return "hello"
}
package main
import (
"testing"
)
func TestHello(t *testing.T) {
want := "hello"
if get := hello(); get != want {
t.Fatalf("want:%s get:%s", want, get)
}
}
官方给了一个go语言的template参考着改一下
# This file is a template, and might need editing before it works on your project.
image: golang:latest
variables:
# Please edit to your GitLab project
REPO_NAME: gitlab.com/dlutzhangyi/hello-world
# The problem is that to be able to use go get, one needs to put
# the repository in the $GOPATH. So for example if your gitlab domain
# is gitlab.com, and that your repository is namespace/project, and
# the default GOPATH being /go, then you'd need to have your
# repository in /go/src/gitlab.com/namespace/project
# Thus, making a symbolic link corrects this.
before_script:
- mkdir -p $GOPATH/src/$(dirname $REPO_NAME)
- ln -svf $CI_PROJECT_DIR $GOPATH/src/$REPO_NAME
- cd $GOPATH/src/$REPO_NAME
stages:
- test
- build
- deploy
format:
stage: test
script:
- go fmt $(go list ./... | grep -v /vendor/)
- go vet $(go list ./... | grep -v /vendor/)
- go test -race $(go list ./... | grep -v /vendor/)
compile:
stage: build
script:
- go build -race -ldflags "-extldflags '-static'" -o $CI_PROJECT_DIR/mybinary
artifacts:
paths:
- mybinary
在左侧栏的CI/CD中点击Pipelines,点击Run Pipeline
执行结束后,页面会显示对应的stages。
每个stage触发了一个Jobs,可以看到结果都是passed。
参考gitlab仓库地址。
https://gitlab.com/dlutzhangyi/hello-world