工作空间临时目录作用
jenkins在进行项目编译的时候会在工作空间下创建一些临时目录 这些临时目录在编译和部署期间不能删除 只能在流水线最后阶段删除
在流水线执行中间阶段删除的话 会导致访问不了编译后的文件
groovy脚本中引入jenkins内置变量 "${workspace}/register-center/target“
删除AppServiceProject@tmp目录后 然后再执行远程拷贝jar包的时候就会提示访问不到相关jar包
把清空临时目录的操作移动到流水线的最后步骤后就可以远程拷贝包
environment{…}, 大括号里面写一些键值对,也就是定义一些变量并赋值,这些变量就是环境变量。环境变量的作用范围,取决你environment{…}所写的位置,你可以写在顶层环境变量,让所有的stage下的step共享这些变量,也可以单独定义在某一个stage下,只能供这个stage去调用变量,其他的stage不能共享这些变量
dir()方法:就是改变当前的工作目录,在dir语句块里执行的其他路径或者相对路径
deleteDir()方法:默认递归删除WORKSPACE下的文件和文件夹,没有参数
Pipeline异常处理
stage("Build"){ steps { script { javahome = tool "Java1" //sh "echo '***************************************************'" //sh "${javahome}/bin/java -version" //sh "export JAVA_HOME=${javahome}" mvnhome = tool "M2" //sh "${mvnhome}/bin/mvn clean install -Dmaven.test.skip=true" sh "${mvnhome}/bin/mvn -Pdev clean -DskipTests=true install" try { sh "${srcPath}/mvn-sonar.sh" } catch (Exception err){ print("代码覆盖率检查失败") } } } post { success { print("Build success......") } } } stage("Build Image"){ steps { script { mytools.create_dockerimage("${workspace}/register-center/target/register-center-dev.jar","${clusterPath}","register-center-dev.jar","taishiganzhi","register-center") mytools.create_dockerimage("${workspace}/config-center/target/config-center-dev.jar","${clusterPath}","config-center-dev.jar","taishiganzhi","config-center") mytools.create_dockerimage("${workspace}/gateway-zuul/target/gateway-zuul-dev.jar","${clusterPath}","gateway-zuul-dev.jar","taishiganzhi","gateway-zuul") mytools.create_dockerimage("${workspace}/admin/target/admin-dev.jar","${clusterPath}","admin-dev.jar","taishiganzhi","admin") mytools.create_dockerimage("${workspace}/bizapp/sdc-collect-config/target/sdc-collect-config-dev.jar","${clusterPath}","sdc-collect-config-dev.jar","taishiganzhi","sdc-collect-config") mytools.create_dockerimage("${workspace}/bizapp/sdc-rule-config/target/sdc-rule-config-dev.jar","${clusterPath}","sdc-rule-config-dev.jar","taishiganzhi","sdc-rule-config") mytools.create_dockerimage("${workspace}/bizapp/sdc-es-service/target/sdc-es-service-dev.jar","${clusterPath}","sdc-es-service-dev.jar","taishiganzhi","sdc-es-service") } } post { success { print("Build image success......") } } }
单个stage出现异常不会影响后面stage的执行
Jenkins组合Job
1.安装插件Multijob plugin
2.创建任务 任务的类型为Multijob
3.编排组合job 按照顺序从上往下依次执行job
在任务的构建阶段设置job组合 直接填写已经存在的job name即可
4.查看任务执行结果
5.查看各个子job的详细日志
一个Job处理多个git仓库
1. 安装Multiple SCMs plugin插件
2.查看项目的工作空间目录
前台项目和后台项目分别被拉取到不同的子目录中 在编译的时候相互不会影响.
3.pipeline脚本实现多仓库拉取git
pipeline { agent any stages { stage('Get Code') { steps { checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: 'backend']], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'yxhgitlab', url: 'https://192.168.30.111:8090/tf-as/appservice.git']]]) checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: 'front']], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'yxhgitlab', url: 'https://192.168.30.111:8090/tf-as/h5-new.git']]]) } } } }
node {
stage('Checkout') {
checkout([$class: 'GitSCM', branches: [[name: '*/develop']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[url: 'git@192.168.1.248:mall/www-xxx-cms.git']]])
checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: 'www_cms_resources']], submoduleCfg: [], userRemoteConfigs: [[url: 'git@192.168.1.248:2019_lx_group/www_cms_resouces.git']]])
sh(script: "mv www_cms_resources/* www-cms-web/src/main/resources/")
}
}
这样就实现了在同一个Job拉取不同的项目或者不同的项目分支到指定的子目录中