网站首页 返回列表 像“草根”一样,紧贴着地面,低调的存在,冬去春来,枯荣无恙。 jenkinsfile 构建流水线 24-10-31 16:56:34 曹大卫 50 ```shell // git分支 def gitBranch = env.gitBranch // git地址 def gitUrl = env.git_url // 编译命令 def buildShell = env.build_shell // 应用镜像 def image = env.image // 代码扫描的目录 def group = env.group // 指定发布环境 def custom_env = env.custom_env //镜像tag def imageTag = custom_env+Calendar.getInstance().getTime().format('YYYYMMdd-hhmmss',TimeZone.getTimeZone('CST')) // 代码子目录 def project_sub_dir = env.project_sub_dir // 指定发布环境 def specify_env = env.specify_env // 指定动作 def action = env.action pipeline { agent { kubernetes { yaml """ apiVersion: v1 kind: Pod metadata: labels: some-label: some-label-value spec: volumes: - name: docker-sock hostPath: path: /var/run/docker.sock type: '' - name: shared-dir emptyDir: {} - name: maven-cache hostPath: path: /data/cache/mvn-cache/repository type: Directory containers: - name: jnlp image: library/jenkins/inbound-agent:4.10-2 imagePullPolicy: "IfNotPresent" - name: maven image: library/maven:2025-1-23 imagePullPolicy: "IfNotPresent" command: - cat tty: true volumeMounts: - name: maven-cache mountPath: /usr/share/maven/ref/repository - name: docker image: library/docker:19.03.11 command: - cat tty: true volumeMounts: - name: docker-sock mountPath: /var/run/docker.sock - name: kustomize image: library/kustomize:v3.8.1 command: - cat tty: true volumeMounts: - name: shared-dir mountPath: /tmp/dir - name: kubedog image: library/kubedog:v0.6.1 command: - cat tty: true volumeMounts: - name: shared-dir mountPath: /tmp/dir """ } } environment{ BASEIMAGE = "alpine_open_jdk:2024-8u432" DEVOPS_GIT = "xxx.com/caozhongjiang/devops-argo.git" } stages { stage('checkLastBuildIsRun') { when { expression { return ( action == "update" ) } } steps { script { def jobname=env.JOB_NAME def jenkinsInstance = Jenkins.instance def previousBuildId = currentBuild.previousBuild.number println "上一次构建的id: ${previousBuildId}" def job = jenkinsInstance.getItemByFullName(jobname) def buildToStop = job.getBuildByNumber(previousBuildId) # def job = build(previousBuildId) if (buildToStop != null) { if (buildToStop.executor != null) { echo "停止构建id为${previousBuildId}的job" buildToStop.executor.interrupt() } } } } } // 拉取代码 stage('GetCode') { when { expression { return ( action == "update" ) } } steps { script { checkout([$class: 'GitSCM', branches: [[name: "$gitBranch"]], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'code', url: "${gitUrl}"]]]) } } } // 单元测试和编译打包 stage('Build&Test') { when { expression { return ( action == "update" ) } } steps { container('maven') { script{ sh """${buildShell}""" } } } } // 代码扫描 // 构建镜像 stage('BuildImage') { when { expression { return ( action == "update" ) } } steps { withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'harbor', usernameVariable: 'DOCKER_USER', passwordVariable: 'DOCKER_PASSWORD']]) { container('docker') { script{ dockerRegistryUrl="${image}".split("/")[0] sh """ if [[ ${project_sub_dir} != "null" ]];then sed -i "s+^FROM .*+FROM ${BASEIMAGE}+g" ${project_sub_dir}/Dockerfile docker build -t ${image}:${imageTag} $project_sub_dir else sed -i "s+^FROM .*+FROM ${BASEIMAGE}+g" ./Dockerfile docker build -t ${image}:${imageTag} . fi docker login ${dockerRegistryUrl} -u ${DOCKER_USER} -p ${DOCKER_PASSWORD} docker push ${image}:${imageTag} docker rmi ${image}:${imageTag} """ //currentBuild.description = "分支: $gitBranch \n 标签:$imageTag" } } } } } //修改gitlab中的yaml文件 stage('Deploy') { steps { withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'argo', usernameVariable: 'DEVOPS_USER', passwordVariable: 'DEVOPS_PASSWORD']]){ container('kustomize') { script{ TMP_DIR="${JOB_NAME}".split("_")[0] APP_DIR="${TMP_DIR}".split("/")[1] sh """ git clone http://${DEVOPS_USER}:${DEVOPS_PASSWORD}@${DEVOPS_GIT} /opt/devops-cd cd /opt/devops-cd git config --global user.name "gitops" git config --global user.email "" if [[ ${custom_env} != "null" ]];then cd /opt/devops-cd/${group}/irtp-trade/${custom_env} else cd /opt/devops-cd/${group}/${APP_DIR}/${gitBranch} fi case ${action} in "stop") kustomize edit set replicas ${APP_DIR}=0 ;; "start") kustomize edit set replicas ${APP_DIR}=1 ;; *) kustomize edit set image ${image}:${imageTag} ;; esac cd /opt/devops-cd git pull git commit -am 'image updates' git push origin main """ } } } } } } // 构建后的操作 post { success { script{ println("启动环境: $custom_env \n启动分支: $gitBranch \n镜像名称: $image:$imageTag") } } failure { script{ println("This branch $gitBranch pipeline build fails.") } } aborted { script{ println("This branch of the $gitBranch pipeline cancels the build.") } } } } ``` 关键字词[jenkins] 分享到: 上一篇:promethues监控爱快软路由 下一篇:harbor镜像扫描工具 如需留言,请 登录,没有账号?请 注册 0 条评论 0 人参与 最新文章 centos7 rsyslog v8.24.0-34.el7 版本bug efk中将宿主机ip地址采集到日志输出中 企业微信群机器人@成员的方式 promethues operator修改告警规则 centos7配置阿里云yum源 pt-duplicate-key-checker 工具检测mysql是否有重复索引 阿里云ecs扩容磁盘 es启用 tcp和http的ssl后报错 点击排行 alertmanager按标签分发告警到接收者 calico三种网络模式 promethues operator修改告警规则 企业微信群机器人@成员的方式 centos7配置阿里云yum源 最新评论 友情链接
0 条评论 0 人参与