工作中随着业务越来越大,微服务的项目也越来越多,最开始的时候是一个服务一个流水线,然后还分了三个环境,也就是一个服务三个流水线,后面就越来越不利于管理维护了,因此,进行了一个改造整合
golang这边的微服务基本分成三类,service、schedule、mq,所以将所有流水线整合为三条通用流水线,再通过参数传递的形式,去构建每个服务
这是service里面的dockerfile和jenkinsfile,schedule、mq大同小异
dockerfile
tyun/chens/golang:1.17.13
ENV ZBE_PATH /biz-code
ARG SERVICENAME=message
COPY $SERVICENAME /biz-code/$SERVICENAME
WORKDIR /biz-code
RUN chmod 777 $(echo `ls -l` |awk -F ' ' '{print $NF}')
USER root
RUN ls -l
CMD ./$(echo `ls -l` |awk -F ' ' '{print $NF}')
jenkinsfile
pipeline {agent {kubernetes {cloud 'kubernetes'cloud 'kubernetes-test'slaveConnectTimeout 1200workspaceVolume hostPathWorkspaceVolume(hostPath:"/opt/workspace", readOnly: false)yaml '''
apiVersion: v1
kind: Pod
spec:containers:- args: ['$(JENKINS_SECRET)', '$(JENKINS_NAME)']image: 'jenkins/jnlp-slave:latest-jdk11'name: jnlpimagePullPolicy: IfNotPresentvolumeMounts:- mountPath: "/etc/localtime"name: "localtime"readOnly: false - command:- "cat"env:- name: "LANGUAGE"value: "en_US:en"- name: "LC_ALL"value: "en_US.UTF-8"- name: "LANG"value: "en_US.UTF-8"image: tyun/chens/golang:1.17.13"imagePullPolicy: "IfNotPresent"name: "build"tty: truevolumeMounts:- mountPath: "/etc/localtime"name: "localtime"- mountPath: "/go/pkg/"name: "cachedir"readOnly: false- command:- "cat"env:- name: "LANGUAGE"value: "en_US:en"- name: "LC_ALL"value: "en_US.UTF-8"- name: "LANG"value: "en_US.UTF-8"image: "registry-beijing.aliyuncs/citools/kubectl:self-1.17"imagePullPolicy: "IfNotPresent"name: "kubectl"tty: truevolumeMounts:- mountPath: "/etc/localtime"name: "localtime"readOnly: false- command:- "cat"env:- name: "LANGUAGE"value: "en_US:en"- name: "LC_ALL"value: "en_US.UTF-8"- name: "LANG"value: "en_US.UTF-8"image: "registry-beijing.aliyuncs/citools/docker:19.03.9-git"imagePullPolicy: "IfNotPresent"name: "docker"tty: truevolumeMounts:- mountPath: "/etc/localtime"name: "localtime"readOnly: false- mountPath: "/var/run/docker.sock"name: "dockersock"readOnly: falserestartPolicy: "Never"imagePullSecrets:- name: qcloudregistrykeynodeSelector:build: "true"securityContext: {}volumes:- hostPath:path: "/var/run/docker.sock"name: "dockersock"- hostPath:path: "/usr/share/zoneinfo/Asia/Shanghai"name: "localtime"- name: "cachedir"hostPath:path: "/opt/gopkg"- name: "phpdir"hostPath:path: "/opt/phppkg"
'''}
}
stages {stage('Pulling Code') {parallel {stage('Pulling Code by Jenkins') {when {expression {env.giteeBranch == null}}steps {git(changelog: true, poll: true, url:'.git', branch:"${BRANCH}", credentialsId: 'gitee-mima')script {COMMIT_ID = sh(returnStdout: true, script: "git log -n 1 --pretty=format:'%h'").trim()TAG = BUILD_TAG + '-' + COMMIT_IDprintln "Current branch is ${BRANCH}, Commit ID is ${COMMIT_ID}, Image TAG is ${TAG}"}}}stage('Pulling Code by trigger') {when {expression {env.giteeBranch != null}}steps {git(url: '.git', branch: env.giteeBranch, changelog: true, poll: true, credentialsId: 'gitee-mima')script {COMMIT_ID = sh(returnStdout: true, script: "git log -n 1 --pretty=format:'%h'").trim()TAG = BUILD_TAG + '-' + COMMIT_IDprintln "Current branch is ${env.giteeBranch}, Commit ID is ${COMMIT_ID}, Image TAG is ${TAG}"}}}}
}stage('Building') {steps {container(name: 'build') {sh """ go env -w GO111MODULE=ongo env -w GOPROXY=,directgo env -w GOPRIVATE=gitee/chens-go/jordanexport GIT_TERMINAL_PROMPT=1go build -o ${SERVICENAME} services/${SERVICENAME}/${SERVICENAME}-service/cmd/${SERVICENAME}/hmod +x ${SERVICENAME}cp -p ${SERVICENAME} build-work/k8s2/service"""}}}stage('Docker build for creating image') {environment {HARBOR_USER = credentials('registry-secret')}steps {container(name: 'docker') {sh """cd build-work/k8s2/serviceecho ${HARBOR_USER_USR} ${HARBOR_USER_PSW} ${TAG}docker login -u ${HARBOR_USER_USR} -p ${HARBOR_USER_PSW} ${HARBOR_ADDRESS}docker build -t ${HARBOR_ADDRESS}/${REGISTRY_DIR}/${IMAGE_NAME}:${TAG} --build-arg SERVICENAME=${SERVICENAME} .docker push ${HARBOR_ADDRESS}/${REGISTRY_DIR}/${IMAGE_NAME}:${TAG}"""}}}stage('Deploying to K8s') {environment {MY_KUBECONFIG = credentials('k8s-config')}steps {container(name: 'kubectl'){sh """/usr/local/bin/kubectl --kubeconfig $MY_KUBECONFIG set image deploy -l app=${KOBE_NAME} ${KOBE_NAME}=${HARBOR_ADDRESS}/${REGISTRY_DIR}/${IMAGE_NAME}:${TAG} -n $NAMESPACE"""}}}}environment {COMMIT_ID = ""HARBOR_ADDRESS = tyun"REGISTRY_DIR = "chens"IMAGE_NAME = "kobe"NAMESPACE = "${ENV}"TAG = ""KOBE_NAME = "${SERVICENAME}"
}parameters {gitParameter(branch: '', branchFilter: 'origin/(.*)', defaultValue: "${BRANCH}", description: 'Branch for build and deploy', name:'BRANCH', quickFilterEnabled: false, selectedValue: 'NONE', sortMode: 'NONE', tagFilter: '*', type: 'PT_BRANCH')}
}
流水线里面需要传递三个参数
具体配置
最后的这个build-work/k8s2/service/Jenkinsfile
,是jenkinsfile在项目代码中的路径
所有参数名称和路径皆可根据自己项目来定义
这里面有些语法看着很离谱,其实是我在做的过程中踩的一些坑,你们可以试试不那样写会发生什么,当然也有可能还有别的解决方法,可多尝试
记录一下,因为这个流水线里面针对服务名称的格式化做了一些特殊处理
我们的mq服务都是以下划线命名,比如goods_stock_merchant
,但是在k8s里面使用命令更新deploy的时候,命名不能使用下划线,所以只能在流水线里面将下划线改为中划线
Dockerfile
tyun/zhubaoe/golang:1.17.13
ENV ZBE_PATH /biz-code
ARG SERVICENAME=message
COPY mq_${SERVICENAME}_server /biz-code/mq_${SERVICENAME}_server
WORKDIR /biz-code
RUN chmod 777 $(echo `ls -l` |awk -F ' ' '{print $NF}')
USER root
RUN ls -l
CMD ./$(echo `ls -l` |awk -F ' ' '{print $NF}')
Jenkinsfile
pipeline {agent {kubernetes {cloud 'kubernetes'cloud 'kubernetes-test'slaveConnectTimeout 1200workspaceVolume hostPathWorkspaceVolume(hostPath:"/opt/workspace", readOnly: false)yaml '''
apiVersion: v1
kind: Pod
spec:containers:- args: ['$(JENKINS_SECRET)', '$(JENKINS_NAME)']image: 'jenkins/jnlp-slave:latest-jdk11'name: jnlpimagePullPolicy: IfNotPresentvolumeMounts:- mountPath: "/etc/localtime"name: "localtime"readOnly: false - command:- "cat"env:- name: "LANGUAGE"value: "en_US:en"- name: "LC_ALL"value: "en_US.UTF-8"- name: "LANG"value: "en_US.UTF-8"image: tyun/zhubaoe/golang:1.17.13"imagePullPolicy: "IfNotPresent"name: "build"tty: truevolumeMounts:- mountPath: "/etc/localtime"name: "localtime"- mountPath: "/go/pkg/"name: "cachedir"readOnly: false- command:- "cat"env:- name: "LANGUAGE"value: "en_US:en"- name: "LC_ALL"value: "en_US.UTF-8"- name: "LANG"value: "en_US.UTF-8"image: "registry-beijing.aliyuncs/citools/kubectl:self-1.17"imagePullPolicy: "IfNotPresent"name: "kubectl"tty: truevolumeMounts:- mountPath: "/etc/localtime"name: "localtime"readOnly: false- command:- "cat"env:- name: "LANGUAGE"value: "en_US:en"- name: "LC_ALL"value: "en_US.UTF-8"- name: "LANG"value: "en_US.UTF-8"image: "registry-beijing.aliyuncs/citools/docker:19.03.9-git"imagePullPolicy: "IfNotPresent"name: "docker"tty: truevolumeMounts:- mountPath: "/etc/localtime"name: "localtime"readOnly: false- mountPath: "/var/run/docker.sock"name: "dockersock"readOnly: falserestartPolicy: "Never"imagePullSecrets:- name: qcloudregistrykeynodeSelector:build: "true"securityContext: {}volumes:- hostPath:path: "/var/run/docker.sock"name: "dockersock"- hostPath:path: "/usr/share/zoneinfo/Asia/Shanghai"name: "localtime"- name: "cachedir"hostPath:path: "/opt/gopkg"- name: "phpdir"hostPath:path: "/opt/phppkg"
'''}
}
stages {stage('Pulling Code') {parallel {stage('Pulling Code by Jenkins') {when {expression {env.giteeBranch == null}}steps {git(changelog: true, poll: true, url:'.git', branch:"${BRANCH}", credentialsId: 'gitee-mima')script {COMMIT_ID = sh(returnStdout: true, script: "git log -n 1 --pretty=format:'%h'").trim()TMP = sh(returnStdout: true, script: """echo ${SERVICENAME}|awk '{ gsub(/_/,"-"); print $0 }'""").trim()KOBE_NAME = 'mq-' + TMP + '-server'TAG = BUILD_TAG + '-' + COMMIT_IDprintln "Current branch is ${BRANCH}, Commit ID is ${COMMIT_ID}, Image TAG is ${TAG}"}}}stage('Pulling Code by trigger') {when {expression {env.giteeBranch != null}}steps {git(url: '.git', branch: env.giteeBranch, changelog: true, poll: true, credentialsId: 'gitee-mima')script {COMMIT_ID = sh(returnStdout: true, script: "git log -n 1 --pretty=format:'%h'").trim()TMP = sh(returnStdout: true, script: """echo ${SERVICENAME}|awk '{ gsub(/_/,"-"); print $0 }'""").trim()KOBE_NAME = 'mq-' + TMP + '-server'TAG = BUILD_TAG + '-' + COMMIT_IDprintln "Current branch is ${env.giteeBranch}, Commit ID is ${COMMIT_ID}, Image TAG is ${TAG}"}}}}
}stage('Building') {steps {container(name: 'build') {sh """ go env -w GO111MODULE=ongo env -w GOPROXY=,directgo env -w GOPRIVATE=gitee/zhubaoe-go/jordanexport GIT_TERMINAL_PROMPT=1go build -o mq_${SERVICENAME}_server mq/${SERVICENAME}_hmod +x mq_${SERVICENAME}_servercp -p mq_${SERVICENAME}_server build-work/k8s2/mq/"""}}}stage('Docker build for creating image') {environment {HARBOR_USER = credentials('registry-secret')}steps {container(name: 'docker') {sh """cd build-work/k8s2/mq/echo ${HARBOR_USER_USR} ${HARBOR_USER_PSW} ${TAG}docker login -u ${HARBOR_USER_USR} -p ${HARBOR_USER_PSW} ${HARBOR_ADDRESS}docker build -t ${HARBOR_ADDRESS}/${REGISTRY_DIR}/${IMAGE_NAME}:${TAG} --build-arg SERVICENAME=${SERVICENAME} .docker push ${HARBOR_ADDRESS}/${REGISTRY_DIR}/${IMAGE_NAME}:${TAG}"""}}}stage('Deploying to K8s') {environment {MY_KUBECONFIG = credentials('k8s-config')}steps {container(name: 'kubectl'){sh """/usr/local/bin/kubectl --kubeconfig $MY_KUBECONFIG set image deploy -l app="${KOBE_NAME}" "${KOBE_NAME}"=${HARBOR_ADDRESS}/${REGISTRY_DIR}/${IMAGE_NAME}:${TAG} -n $NAMESPACE"""}}}}environment {COMMIT_ID = ""HARBOR_ADDRESS = tyun"REGISTRY_DIR = "zhubaoe"IMAGE_NAME = "kobe"NAMESPACE = "${ENV}"TAG = ""KOBE_NAME = ""
}parameters {gitParameter(branch: '', branchFilter: 'origin/(.*)', defaultValue: 'feature/test', description: 'Branch for build and deploy', name:'BRANCH', quickFilterEnabled: false, selectedValue: 'NONE', sortMode: 'NONE', tagFilter: '*', type: 'PT_BRANCH')}
}
特殊处理主要是这个地方
TMP = sh(returnStdout: true, script: """echo ${SERVICENAME}|awk '{ gsub(/_/,"-"); print $0 }'""").trim()
使用awk的gsub函数,将服务名里面的所有下划线改为中划线,最开始是用的sub函数,而sub函数默认只会匹配修改遇到的第一个下划线,所以使用gsub,全局匹配,做个记录,免得忘了!!
本文发布于:2024-02-04 05:59:11,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170700536452891.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |