jenkins pipeline java
来源:原创
时间:2021-02-27
作者:脚本小站
分类:Jenkins
给jenkins添加到docker用户组:以jenkins身份运行docker的话会出现一些问题,需要进行如下操作,需要重启节点。或者以root身份运行jenkins也可。
usermod -aG docker jenkins
pipeline:
#!groovy @Library('jenkinslib@master') _ def tools = new org.devops.tools(); def k8s = new org.devops.kubernetes(); def gitlab = new org.devops.gitlab(); String dockerTag = "${env.dockerTag}" pipeline{ agent{ node{ label "master" } } stages{ stage("Checkout"){ steps{ script{ tools.PrintMes("获取代码","green") checkout([ $class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [ [ credentialsId: '398170a9-72a6-410b-8293-334687f152da', url: 'http://192.168.0.26/qiyang/solo.git' ] ] ]) } } } stage("build"){ steps{ script{ tools.PrintMes("编译","green") mvnHome = tool "M2" sh "${mvnHome}/bin/mvn clean package -Dmaven.test.skip=true" } } } stage("dockerbuild"){ steps{ script{ workspace1 = pwd() node{ workspace2 = pwd() script{ sh "sudo cp ${workspace1}/target/solo.war ${workspace2}" } stage("build docker image"){ // 打镜像并上传 docker.withRegistry('http://myharbor.com', '2cc97692-d306-4e6d-b78e-95b46a4faf28') { // 需要创建凭据 app = docker.build("myharbor.com/solo/solo:${env.dockerTag}") app.push() } } script{ sh "docker image rm myharbor.com/solo/solo:${env.dockerTag}" } } } } } stage("deployment"){ steps{ script{ response = k8s.GetDeployment("kube-system", "coredns") response = response.content println(response) // print deployment content fileData = readYaml text: """${response}""" println(fileData) // 替换镜像名称 oldImage = fileData["spec"]["template"]["spec"]["containers"][0]["image"] response = response.replace(oldImage, "myharbor.com/solo/solo:${env.dockerTag}") // 从gitlab上获取文件 content = gitlab.GetRepoFile("5","Dockerfile") println(content) // 上传文件 base64Content = response.bytes.encodeBase64().toString() content = gitlab.CreateRepoFile(2,"yaml%2fcoredns-${env.dockerTag}.yaml",base64Content) } } } } } def createVersion(String BUILD_NUMBER){ return new Date().format('yymm')+"-${BUILD_NUMBER}" }