gitlab cicd 初体验

文章目录

  • 背景
  • Hello world
    • 申请仓库
    • 创建.gitlab-ci.yml文件
    • 执行

背景

最近在调研devops这一块,看看业内的主流公司都是如何做一块的,看gitlab的ci/cd功能支持的挺完善,还支持pipeline,决定上手试一下。

Hello world

申请仓库

申请一个仓库hello world,在仓库中创建三个文件,分别为main.go,hello.go和hello_test.go。

  1. main.go
package main

import "fmt"

func main() {
	str := hello()
	fmt.Println(str)
}
  1. hello.go
package main

func hello() string {
	return "hello"
}
  1. 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
gitlab cicd 初体验_第1张图片
执行结束后,页面会显示对应的stages。
gitlab cicd 初体验_第2张图片
每个stage触发了一个Jobs,可以看到结果都是passed。
gitlab cicd 初体验_第3张图片


参考gitlab仓库地址。
https://gitlab.com/dlutzhangyi/hello-world

你可能感兴趣的:(gitlab)