背景
最近在調研devops這一塊,看看業內的主流公司都是如何做一塊的,看gitlab的ci/cd功能支持的挺完善,還支持pipeline,決定上手試一下。
Hello world
申請倉庫
申請一個倉庫hello world,在倉庫中創建三個文件,分別爲main.go,hello.go和hello_test.go。
- main.go
package main
import "fmt"
func main() {
str := hello()
fmt.Println(str)
}
- hello.go
package main
func hello() string {
return "hello"
}
- hello_test.go
package main
import (
"testing"
)
func TestHello(t *testing.T) {
want := "hello"
if get := hello(); get != want {
t.Fatalf("want:%s get:%s", want, get)
}
}
創建.gitlab-ci.yml文件
官方給了一個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