Jenkins Pipeline实现Golang项目的CI/CD

Jenkins Pipeline实现Golang项目的CI/CD

背景

最近新增了一个Golang实现的项目,需要接入到现有的流水线架构中。

流程图

这边流程和之前我写过的一篇《基于Jenkins实现的CI/CD方案》差不多,不一样的是构建现在是手动触发的,没有配置webhook。因为不是业务主干代码,部署的频率比较低。

项目目录结构

Dockerfile文件

dockerfile 复制代码
FROM alpine:3.19.1
WORKDIR /data
COPY go-pipline /data/go-pipline

# 设置阿里云仓库
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories

# 设置时区和安装bash
RUN apk add -U tzdata && \
    apk add -U bash && \
    ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \
    echo "Asia/Shanghai" > /etc/timezone

EXPOSE 8080
ENTRYPOINT ["/data/go-pipline"]

Jenkinsfile文件

groovy 复制代码
import java.text.SimpleDateFormat
import java.util.TimeZone

// 构建版本
def createVersion() {
    def simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss")
    simpleDateFormat.timeZone = TimeZone.getTimeZone("Asia/Shanghai")
    return simpleDateFormat.format(new Date()) + "_${env.branch}"
}

// 获取ssh连接
def GetRemoteServer(ip, username, password) {
    def remote = [:]
    remote.name = ip
    remote.host = ip
    remote.user = username
    remote.password = password
    remote.allowAnyHosts = true
    return remote
}

pipeline {
    agent none
    environment {
        _version = createVersion()
    }
    stages {
        stage('Git Checkout') {
            agent any
            steps {
                checkout([
	                $class: 'GitSCM',
	                branches: [[name: "${branch}"]],
	                doGenerateSubmoduleConfigurations: false,
	                extensions: [],
	                gitTool: 'Default',
	                submoduleCfg: [],
	                userRemoteConfigs: [[url: 'xxx.git',credentialsId: 'xxx',]]
                ])
            }
        }
        stage('Source Package') {
            agent {
                docker {
                    image 'golang:1.21.0'
                    args '-v go-cache:/go/pkg/mod'
                }
            }
            steps {
                sh 'go env -w GO111MODULE=on'
                sh 'go env -w GOPROXY=https://goproxy.cn,direct'
                sh 'go mod download'
                sh 'CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-s -w" -o go-pipeline'
            }
        }

        stage('Image Build') {
            agent any
            steps {
                sh 'docker build -f $WORKSPACE/Dockerfile -t {{harbor}}/common/go-pipeline:${_version} $WORKSPACE'
                sh 'rm -f $WORKSPACE/go-pipeline'
                sh 'docker push {{harbor}}/common/go-pipeline:${_version}'
                sh 'docker rmi {{harbor}}/common/go-pipeline:${_version}'
            }
        }

        stage('Publish To Env') {
            agent any
            steps {
                script {
                    def remote = GetRemoteServer('127.0.0.1', 'root', 'root')
                    sshCommand remote: remote, command: "docker stop go-pipeline; docker rm go-pipeline"
                    sshCommand remote: remote, command: "docker run -it  --name go-pipeline -d -p 8080:8080  {{harbor}}/common/go-pipeline:${_version}"
                }
            }
        }
    }
}

有一个需要注意的地方,Source Package 源码编译阶段,除了指定构建的容器以外,我还挂载了一个go-cache的volume,这边是为了缓存go的依赖。这边是为了提高构建的速度,或者也可以解决构建机器连接不了外网的情况。具体volume创建命令:

shell 复制代码
docker volume create go-cache

main方法中只提供了一个接口

复制代码
package main

import (
	"github.com/gin-gonic/gin"
	"net/http"
)

func main() {
	r := gin.Default()
	r.GET("/ping", func(context *gin.Context) {
		context.JSON(http.StatusOK, gin.H{
			"message": "pong",
		})
	})
	r.Run()
}

流水线配置

新增一个pipeline类型的流水线

新增一个参数branch ,将构建的分支参数开出来

选择Pipeline script from SCM ,配置如下图

这边需要注意,把最后Lightweight checkout 去掉勾选,不然branch参数会不生效

选择构建,结果验证ok

结语

代码地址:https://github.com/yzh19961031/blogDemo/tree/master/go-pipeline

相关推荐
FungLeo17 小时前
成为全栈·Go 后端篇·契约一致性与差分测试:两套实现如何互相校验
golang·node.js·go·合约测试·差分测试·成为全栈·api兼容
老王IT17 小时前
Go开发环境搭建(window操作系统)
开发语言·后端·golang
利威尔xu1 天前
Go IM 源码实战(7):断线三天重连,水位算错就漏消息
redis·后端·微服务·面试·golang
7177771 天前
2026年CI/CD工具选哪家比较好:主流方案对比与Gitee选型解析
ci/cd·gitee
biaov1 天前
Jenkins + Docker 搭建前端 CI:前端一键打包部署
前端·docker·jenkins
行思理1 天前
go语言学习难度及适合学习的人群
开发语言·学习·golang
@大迁世界2 天前
告别 Go,拥抱 Rust
开发语言·golang·rust
喵了几个咪2 天前
GoWind ShortPlay 拆解:不用模型的推荐闭环——埋点回流、三路召回与已看去重
开发语言·后端·微服务·golang·gowind
喵了几个咪2 天前
GoWind UBA 拆解:把 Doris UNIQUE KEY 翻译成 ClickHouse——读数正确性对齐实录
开发语言·后端·clickhouse·微服务·golang·gowind
FungLeo2 天前
成为全栈·Go 后端篇·用 Go 重写分层架构:领域包、装配根与最小抽象
golang·go·领域驱动设计·gorm·分层架构·成为全栈