jenkins参数化构建:条件触发构建 - k8s系列(三)

网友投稿 370 2022-09-11

jenkins参数化构建:条件触发构建 - k8s系列(三)

环境背景

已实现 k8s系列(一) - 使用kubeadm安装kubernetes(k8s) 已实现 k8s系列(二) - jenkins+kubernetes(k8s)+docker持续集成与部署(CI/CD) km - 2cpu - 4g内存 - ip - 192.168.23.39 node1 - 2cpu - 2G内存 - ip - 192.168.23.40 node1 - 2cpu - 2G内存 - ip - 192.168.23.41 基于 k8s系列(二) 已有可正常运行的jenkins工作任务jenkins 已按文章安装好 kubernetes 与 Git Parameter 扩展 git 仓库地址(CI/CD所需文件及代码)master tag为:master-20220401-00 dev tag为:dev-20220401-00 所有操作基于官方文档说明

增加jenkins构建参数

勾选 This project is parameterized

1.增加 docker_registry 构建参数

2.增加 banch_name 构建参数

3.增加 tag_name 和 dev_tag_name 构建参数

4.构建显示

若首次构建无法出现分支与标签,需要先构建一次才会显示

修改Pipeline脚本

1.条件触发

script{ if(branch_name == "origin/master"){ #master构建 } else { #dev构建 } }

2.相关 Deployment与Service配置在git仓库内go_app.yaml文件

apiVersion: apps/v1 kind: Deployment metadata: name: go-app-${branch_name} labels: app: goweb${branch_name} spec: selector: matchLabels: app: goweb${branch_name} replicas: 10 strategy: type: RollingUpdate template: metadata: labels: app: goweb${branch_name} spec: containers: - name: go-app-${branch_name} image: ${docker_registry}/k8s:${tag_name} imagePullPolicy: Always ports: - name: http containerPort: 80 livenessProbe: httpGet: port: 80 path: /ping initialDelaySeconds: 2 periodSeconds: 60 timeoutSeconds: 3 --- apiVersion: v1 kind: Service metadata: name: go-app-${branch_name} spec: type: ClusterIP selector: app: goweb${branch_name} ports: - port: 80 protocol: TCP targetPort: 80

3.最终脚本

基于 k8s系列(二) 修改

pipeline { agent any stages { stage('clone') { steps { echo 'clone' // 拉取代码 git credentialsId: 'gitee_userid', url: 'https://gitee.com/lzcit/k8s.git' sh "git checkout ${branch_name}" sh "sed -i 's/\${docker_registry}/${docker_registry}/g' go_app.yaml" script{ //替换环境变量 if(branch_name == "origin/master"){ sh "sed -i 's/\${branch_name}/master/g' go_app.yaml" sh "sed -i 's/\${branch_name}/master/g' run.go" sh "sed -i 's/\${tag_name}/${tag_name}/g' go_app.yaml" sh "sed -i 's/\${tag_name}/${tag_name}/g' run.go" } else { sh "sed -i 's/\${branch_name}/dev/g' go_app.yaml" sh "sed -i 's/\${branch_name}/dev/g' run.go" sh "sed -i 's/\${tag_name}/${dev_tag_name}/g' go_app.yaml" sh "sed -i 's/\${tag_name}/${dev_tag_name}/g' run.go" } } } } stage('build go') { steps { echo 'build go' // 编译可执行文件 sh "export GOPROXY='&& go mod tidy && CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o ./run run.go" sh "mkdir -p work && cp -a run ./work/ && cp -a static ./work/" } } stage('make image') { steps { echo 'make image' // 制作代码镜像 script{ if(branch_name == "origin/master"){ sh "docker build -f Dockerfile -t ${docker_registry}/k8s:${tag_name} ." } else { sh "docker build -f Dockerfile -t ${docker_registry}/k8s:${dev_tag_name} ." } } } } stage('push image') { steps { echo 'push image' // 推送到私有仓库 script{ if(branch_name == "origin/master"){ sh "docker push ${docker_registry}/k8s:${tag_name}" } else { sh "docker push ${docker_registry}/k8s:${dev_tag_name}" } } } } stage('deploy') { steps { echo 'deploy' // 部署代码 kubeconfig(caCertificate: '''-----BEGIN CERTIFICATE----- MIIC/jCCAeagAwIBAgIBADANBgkqhkiG9w0BAQsFADAVMRMwEQYDVQQDEwprdWJl cm5ldGVzMB4XDTIyMDQwMTAxNDU0NVoXDTMyMDMyOTAxNDU0NVowFTETMBEGA1UE AxMKa3ViZXJuZXRlczCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAJ6u P4IG/t/uorMEiVgseW1llz/XyO2BCvl7Hu1dtS1NNu7360H/fTs+HqP7Ss8Qf5Yh di3DbClKfERUHD8nKLztocSrrJNbKhniQOpI9tUJIbMVkCj5iBWthHrrQshdPB90 xP5skLM+aMoraIJqWdvzzcDKdjfi0pV6XM37YOovYT5e8IzTJ5a24dWCCu5z7J1+ rEQPxOp9AOyqctpUyneA5LK0nxxNs3Vqv16t97IzTPttAiRfUfNEVDswUqF/pspd aBM3u3saQ/08BhKn45lGNog3huC5kViT0U61Z84EuX/a+YOWxPAIWnSnRrwl7oFR fBpIKNJq59SlYtanVskCAwEAAaNZMFcwDgYDVR0PAQH/BAQDAgKkMA8GA1UdEwEB /wQFMAMBAf8wHQYDVR0OBBYEFJ3uWtxk0fbtEy5E+bodzUpPojErMBUGA1UdEQQO MAyCCmt1YmVybmV0ZXMwDQYJKoZIhvcNAQELBQADggEBAGgJH5enPPtMnQwyjtJm PH13dV/CU/R+8i3w4Mz567otNNTlWNZ5bHb6ddKUKQ6iaSMJ3y/O/Oi1RAVFAGA2 6ffL5BWiQyywNdzjUdd5nPFz97BfDqm5yIEVdIecSNnzbe47IDyXl0vHegp31DRf YHGOQ0PTOJz1rJ7qKNehsM3j5p2Hg22o4IT6EqaXNRS/tlaCb5cG5OLil+vAebl/ FNfII8/ORpMN6KFGH6iC1TBsXPzn1Y6vzLv96758L8DpLmMBZ/z9f+s/ZyPESrxo tkCZ89YwTMchrxHg0uluRnh8MQaUPgm1itTIdJNrwet1zBrQOogkOfFZdfunSkdy 0yE= -----END CERTIFICATE-----''', credentialsId: 'kubernetes-key', serverUrl: '{ sh "kubectl apply -f go_app.yaml" } } } } }

查看结果

1.查看 kubernetes(k8s) pods 发布结果

2.校验 kubernetes(k8s) 最终发布

3.所需shell命令

kubectl get pod kubectl get svc curl svc_ip/ping

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:线上k8s集群TTL 机制排毒,Job已经通过API 增加了Job的TTL 时长,且成功响应,为什么系统还是清理了Job?
下一篇:PR人:何加盐对谈剽悍一只猫:如何打造个人影响力!
相关文章

 发表评论

暂时没有评论,来抢沙发吧~