1.宿主机安装jenkins 不要用docker

为什么:docker jenkins你只有jenkins, 你想做golang编译的情况,它的镜像里面缺少go环境。
而宿主机安装的情况,jenkins是可以通过环境变量修改来访问宿主机里面安装的内容。

2.插件

// docker镜像构建
Docker Pipeline
// 推送k8s
Kubernetes CLI Plugin
stage 时间可视化
Pipeline: Stage View Plugin
飞书通知
https://github.com/721806280/lark-notice-plugin

3.agent

没用到高级功能,系统默认即可

vi CentOS-Base.repo
sudo yum clean all
sudo yum makecache
sudo yum update -y
yum -y intall git jenkins
yum -y install jenkins
yum -y install docker
yum install -y yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum list docker-ce --showduplicates
yum list docker-ce --showduplicates|sort -r
yum install docker-ce
systemctl start docker
systemctl status docker
systemctl enable docker

yum install -y yum-utils device-mapper-persistent-data lvm2
sudo yum install epel-release
yum -y install git vim wget nginx
sudo yum install java-11-openjdk-devel
sudo update-alternatives --config java
curl -LO “https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl”
sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl
kubectl version --client
kubectl version --client --output=yaml
wget https://golang.google.cn/dl/go1.22.3.darwin-amd64.tar.gz # 解压后,放到/usr/local/ 目录后,记得修改 /etc/bashrc, 修改PATH

repo

epel.repo
jenkins.repo
docker-ce.repo
CentOS-Base.repo

[base]
name=CentOS-$releasever - Base
baseurl=http://mirrors.aliyun.com/centos/$releasever/os/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

[updates]
name=CentOS-$releasever - Updates
baseurl=http://mirrors.aliyun.com/centos/$releasever/updates/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

[extras]
name=CentOS-$releasever - Extras
baseurl=http://mirrors.aliyun.com/centos/$releasever/extras/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

[centosplus]
name=CentOS-$releasever - Plus
baseurl=http://mirrors.aliyun.com/centos/$releasever/centosplus/$basearch/
gpgcheck=1
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

url 后缀会导致报错。需要systemctl edit jenkins
[Service]
Environment=“JENKINS_PREFIX=/jenkins”
保存退出

权限:
GOPATH 放置在jenkins 用户有权限访问的地方
sudo usermod -aG docker jenkins # 给jenkins 开放docker执行权限
记得重启jenkins

docker login registry.jiaxianghudong.com 在宿主机配置
go 环境在宿主机配置
accessToken 可以配置在creditials里面,username,password

参考链接

https://blog.csdn.net/qq_33371766/article/details/137413858

https://baijiahao.baidu.com/s?id=1796281020927178690&wfr=spider&for=pc

docker ce
https://baijiahao.baidu.com/s?id=1796281020927178690&wfr=spider&for=pc

jenkins
https://blog.csdn.net/qq_33371766/article/details/137413858

送上模板一份


def job_name = env.JOB_NAME.split('test-')
def job = job_name[1]

pipeline {
    agent any
    environment {
        GOPATH = '/data/go'
        GOROOT = '/usr/local/go' // 如果需要的话
        PATH = "$GOROOT/bin:$PATH"
        GOPRIVATE='gitee.com/xxxx/*'
        GOPROXY='https://goproxy.cn,direct'
        CGO_ENABLED='0'
        GOOS='linux'
        GOARCH='amd64'
        GIT_TERMINAL_PROMPT='1'
		job="$job"
    }
    stages {
        stage('Checkout') {
        	steps {
				checkout([$class: 'GitSCM', 
					branches: [[name: '*/Develop']], 
					doGenerateSubmoduleConfigurations: false, 
					extensions: [], 
					userRemoteConfigs: [[
						url: "https://gitee.com/xxxx/${job}.git",
						credentialsId: 'card-trade2'
					]]
				])
			}
		}
		stage('Build') {
			steps {
				script {
					withCredentials([usernamePassword(credentialsId: 'card-trade2', passwordVariable: 'GIT_PASSWORD', usernameVariable: 'GIT_USERNAME')]) {
						println "开始编译"
						sh '''
						set +x
						go mod tidy
						echo "运行go build"
						echo $(pwd)
						go build -o ${job} -buildvcs=false -v
						'''
					}	
				}
			}
		}
		stage('构建镜像') {
			steps {
				script {
					sh '''
                set +x
                tee Dockerfile <<-EOF
FROM registry.xxxx.com/jiaxiang/alpine-cst:1.35

WORKDIR /go/bin
ADD ${job} /go/bin
ADD config.yaml /go/bin
ADD configs /go/bin/configs
RUN chmod +x /go/bin/${job}
CMD ["/go/bin/${job}","--config", "config.yaml"]
EOF
            '''
					docker.withRegistry("https://registry.xxxx.com", "registry-jiaxiang") {
						def app = docker.build "registry.xxxx.com/jiaxiang/card-trade-v3.${job}:${env.BUILD_ID}"
						app.push "${env.BUILD_ID}"
					}
				}
			}
		}
		stage('更新镜像') {
			steps {
				script {
					def parameter = "registry.xxxx.com/jiaxiang/card-trade-v3.${job}:${env.BUILD_ID}"
					withCredentials([sshUserPrivateKey(credentialsId: 'sshpass', keyFileVariable: 'SSH_KEY')]) {
						// 构建SSH命令,包括传递的参数和凭据
						sh "ssh -i $SSH_KEY -o StrictHostKeyChecking=no xxxr@47.11.52.1 'sudo /bin/bash dockerupdatemallapi.sh \"$parameter\"'"
					}
				}
			}
		}
		stage('更新k8s') {
            steps {
                script {
                    println "更新镜像为 ${image} job is ${job}"
                    withKubeConfig([credentialsId: "k8s_config"]) {
                        sh "kubectl set image deploy ${job} -n card-trade ${job}=${image}"
                    }
                }
            }
        }
	}
}
Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐