首页  >  文章  >  后端开发  >  go语言项目是怎么配置Gitlab CI的

go语言项目是怎么配置Gitlab CI的

藏色散人
藏色散人转载
2021-12-08 14:52:102734浏览

本文由golang教程栏目给大家介绍gol语言项目是怎么配置Gitlab CI的,希望对需要的朋友有所帮助!

pipeline流程

  • 使用golangci-lint 检查代码
  • 编译代码
  • 部署二进制

before_script 设置环境变量

主要 GOPROXY设置

before_script:
  - echo "before_script"
  - git version
  - go env -w GOPRIVATE=code.haiziwang.com
  - mkdir -p .go
  - go version
  - go env -w GO111MODULE=on
  - go env -w GOPROXY="https://goproxy.io,direct"

golangci-lint

默认集成了很多开箱即用的linter

54f315b332ec1527dafd2f6af77906e.png

https://golangci-lint.run/

golangci-lint:
    image: golangci/golangci-lint:v1.27.0
    stage: lint
    extends: .go-cache
    allow_failure: true
    script:
      - golangci-lint run -v

allow_failure 表示失败了可以继续跑后续的job

编译

compile:
    stage: build
    extends: .go-cache
    script:
      - go mod download
      - go build -race -o $OUTPUT_NAME
    artifacts:
      paths:
        - $OUTPUT_NAME

缓存 go mod

.go-cache:
    variables:
        GOPATH: $CI_PROJECT_DIR/.go
    cache:
      paths:
        - .go/pkg/mod/

full example

# This file is a template, and might need editing before it works on your project.
image: hub-mirror.c.163.com/library/golang:latest

.go-cache:
    variables:
        GOPATH: $CI_PROJECT_DIR/.go
    cache:
      paths:
        - .go/pkg/mod/

variables:
  OUTPUT_NAME: helloworld-app

stages:
    - lint
    - build
    - deploy

before_script:
  - echo "before_script"
  - git version
  - go env -w GOPRIVATE=code.haiziwang.com
  - mkdir -p .go
  - go version
  - go env -w GO111MODULE=on
  - go env -w GOPROXY="https://goproxy.io,direct"

golangci-lint:
    image: golangci/golangci-lint:v1.27.0
    stage: lint
    extends: .go-cache
    allow_failure: true
    script:
      - golangci-lint run -v

compile:
    stage: build
    extends: .go-cache
    script:
      - go mod download
      - go build -race -o $OUTPUT_NAME
    artifacts:
      paths:
        - $OUTPUT_NAME

deploy-dev:
    stage: deploy
    script:
      - echo "deploy dev environment"

以上是go语言项目是怎么配置Gitlab CI的的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文转载于:segmentfault.com。如有侵权,请联系admin@php.cn删除