简述
由于流水线被组织中越来越多的项目所使用,常用的模块功能可能多个项目都会使用。在多个项目之间共享流水线有助于减少冗余和代码的可读性。
共享库结构
(root)
+- src # Groovy source files
| +- org
| +- foo
| +- Bar.groovy # for org.foo.Bar class
+- vars
| +- foo.groovy # for global ‘foo‘ variable
| +- foo.txt # help for ‘foo‘ variable
+- resources # resource files (external libraries only)
| +- org
| +- foo
| +- bar.json # static helper data for org.foo.Bar
模板库定义
vim var/pipelineCall.groovy
#!groovy
pipeline {
//环境变量
environment {
//git认证
__ROPE_GIT_AUTH = "xxxxx"
//应用名
APPLICATION_NAME = "${map.APPLICATION_NAME}"
//应用端口
SERVER_PORT = "${map.SERVER_PORT}"
// git地址
__ROPE_GIT_URL = "${map.GIT_URL}"
}
options{
buildDiscarder(logRotator(numToKeepStr: ‘10‘))
disableConcurrentBuilds()
skipDefaultCheckout()
timeout(time: 1, unit: ‘HOURS‘)
}
stages {
stage(‘Git阶段‘) {
checkout([
$class: ‘GitSCM‘,
branches: [[name: "${BRANCH_NAME}"]],
doGenerateSubmoduleConfigurations: false,
extensions: [],
submoduleCfg: [],
userRemoteConfigs: [[credentialsId: "$__ROPE_GIT_AUTH",url: "${__ROPE_GIT_URL}"]]
])
}
stage(‘Maven阶段‘){
steps{
configFileProvider([configFile(fileId: ‘maven-global-settings‘, variable: ‘MAVEN_GLOBAL_ENV‘)]) {
sh ‘mvn -s $MAVEN_GLOBAL_ENV clean package -DskipTests -DskipDocker -U‘
}
}
}
}
}
客户端调用
vim Jenkinsfile
#!groovy
library ‘test-pipeline-library‘
def map = [:]
map.put(‘DEPARTMENT‘,‘test‘)
// 应用名
map.put(‘APPLICATION_NAME‘,‘demo‘)
// 应用端口
map.put(‘SERVER_PORT‘,8080)
// 项目git地址
map.put(‘GIT_URL‘,‘项目git地址‘)
//调用流水线共享库
pipelineCall(map)
jenkins配置共享库
## 系统管理 => 系统配置 => Global Pipeline Libraries
# 新增 test-pipeline-library共享库
创建多分支流水线任务
原文:https://www.cnblogs.com/qms19/p/14338513.html