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

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知識

相關文章