在 Debian 上搭建 Golang 持续集成环境
一 准备与安装
sudo apt update && sudo apt install -y golang-gogo versionwget https://go.dev/dl/go1.22.5.linux-amd64.tar.gzsudo tar -C /usr/local -xzf go1.22.5.linux-amd64.tar.gz~/.bashrc 或 /etc/profile):
export GOROOT=/usr/local/goexport GOPATH=$HOME/goexport PATH=$PATH:$GOROOT/bin:$GOPATH/binsource ~/.bashrc 或 source /etc/profilego mod init your-project-namego mod tidyexport GO111MODULE=on。二 方案一 GitLab CI 最小可用流水线
.gitlab-ci.yml:
image: golang:1.22
stages:
- build
- test
variables:
GIN_MODE: release
before_script:
- go version
- go env
build:
stage: build
script:
- go build -o myapp .
test:
stage: test
script:
- go test -v ./...
git add .gitlab-ci.yml && git commit -m "Add GitLab CI" && git push三 方案二 GitHub Actions 最小可用流水线
.github/workflows/ci.yml:
name: CI
on: [push, pull_request]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v4
with:
go-version: '1.22'
- run: go mod tidy
- run: go build -v ./...
- run: go test -race -v ./...
四 质量与效率优化实践
~/go/pkg/mod,显著加速 go mod tidy 与构建。-race,提前发现并发问题。五 自托管 Runner 与替代方案
.gitlab-ci.yml 中使用 tags 指定自托管 Runner,执行与云端一致的构建与测试流程,适合内网、合规或特殊环境需求。.drone.yml 定义 clone → test → build → publish → deploy → notify 流程,部署简单、与容器生态契合。