1. 准备Debian系统环境在Debian系统上,首先需要安装Golang并配置基础环境。通过以下命令安装最新版Golang:

sudo apt update && sudo apt install golang-go -y验证安装是否成功:
go version配置环境变量(可选但推荐),将以下内容添加到~/.bashrc或~/.profile中:
export GOPATH=$HOME/goexport PATH=$PATH:$GOPATH/bin执行source ~/.bashrc使配置生效。
2. 选择并安装CI工具主流CI工具均可用于Debian环境,常见选项包括:
以GitLab Runner为例,安装步骤如下:
sudo apt-get install -y curl openssh-server ca-certificates tzdata perlcurl -s https://packages.gitlab.com/install/repositories/gitlab/gitlab-runner/script.deb.sh | sudo bashsudo apt-get install gitlab-runner -y注册Runner(替换为你的GitLab实例URL和注册令牌):
sudo gitlab-runner register3. 配置CI流程文件在项目根目录创建CI配置文件(如GitLab的.gitlab-ci.yml、Jenkins的Jenkinsfile),定义构建、测试、部署等阶段。
示例1:GitLab CI/CD配置(.gitlab-ci.yml)
image: golang:latest# 使用官方Golang镜像stages:- build- test- deployvariables:GIN_MODE: release# 设置Go环境变量(如Web框架模式)before_script:- go version # 打印Go版本- go env # 打印Go环境变量build:stage: buildscript:- go build -o myapp .# 编译项目test:stage: testscript:- go test ./...# 运行所有测试deploy:stage: deployscript:- scp myapp user@yourserver:/path/to/deploy# 部署到目标服务器only:- main# 仅main分支触发部署示例2:Jenkins Pipeline配置(Jenkinsfile)
pipeline {agent anystages {stage('Build') {steps {sh 'go build -o myapp'// 编译项目}}stage('Test') {steps {sh 'go test ./...'// 运行测试}}stage('Deploy') {steps {sh 'scp myapp user@server:/path/to/deploy'// 部署}}}}4. 触发与监控CI流程将配置文件提交到Git仓库并推送,CI工具会自动触发流程:
git add .gitlab-ci.yml# 或Jenkinsfilegit commit -m "Add CI configuration"git push origin main5. 扩展流程(可选)根据需求扩展CI流程,提升项目质量:
golint、staticcheck等工具,例如在before_script中加入go vet ./...;build-image:stage: buildscript:- docker build -t my-golang-app:latest .