流水线流程
流程说明
流水线编写思路:
(1)将流程中所需要的各个参数,在初始化阶段全部封装到对象中
(2)后续操作为在指定位置执行对应的命令
对象定义
工程名称(文件夹名称)
代码下载地址
资源部署位置
分支标签
class GitObject {
//对象名称,同时为下载目录
def name
//gitlab克隆链接
def url
//代码分支
def branche
//deploy的根目录
def deployPath
}
参数解释
节点信息
agent{
node{
label 'Node10.202.xx.xxx'
}
}
可选参数
parameters{
choice(description: '代码分支选择',name: 'k_tag',choices: ['master','test','develop'])
booleanParam (defaultValue: true, description: 'Deploy 模块', name: 'deploy_modules')
booleanParam (defaultValue: true, description: '全选,默认选中,选中后以下模块可不选择', name: 'all_tag')
booleanParam (defaultValue: false, description: 'koca-base代码', name: 'kb_tag')
booleanParam (defaultValue: false, description: 'koca-boot代码', name: 'kbo_tag')
booleanParam (defaultValue: false, description: 'koca-cloud代码', name: 'kc_tag')
booleanParam (defaultValue: false, description: 'koca-admin代码', name: 'ka_tag')
booleanParam (defaultValue: false, description: 'koca-studio代码', name: 'ks_tag')
booleanParam (defaultValue: false, description: 'koca-amo代码', name: 'kd_tag')
booleanParam (defaultValue: false, description: 'koca-lcp-runtime代码', name: 'klr_tag')
booleanParam (defaultValue: false, description: 'koca-parent代码', name: 'kp_tag')
}
1.选择分支
选择不同分支master,test,develop
2.Deploy 模块
常用选择,选中后会进行deploy,默认选中
3.全选
全选,默认选中,选中后其下各个分模块可不选择
4各选择模块
如果取消勾选全选按钮,系统会编译并deploy选中模块
初始化map----将各代码地址及部署位置存放在map中
注意map虽然为全局定义,但需要在此处初始化,并以参数形式传入初始化函数
downloadMap = [
'koca-base':'xxx/xxx/xx/xx.git',
'koca-boot':'xxx/xxx/xx/xx.git',
xxx: xxx,
]
分支转换 选项一般为master,test等选项,一般需要根据实际情况进行转换
def branchSwitch(String branch) {
switch (branch.startsWith('V')) {
case true:
return "refs/tags/" + branch
break
case false:
return "refs/heads/" + branch
break
default:
return "refs/tags/" + branch
break
}
}
初始化并添加到list中
def initGitObject(String name, def downloadMap, def deployMap, String branche) {
GitObject gitObject = new GitObject()
gitObject.name = name
gitObject.url = downloadMap.get(name)
gitObject.branche = branche
gitObject.deployPath = deployMap.get(name)
return gitObject
}
拉取代码
//遍历实体类下载代码
def gitCloneModulesList(ArrayList gitObjectList) {
for(gitObject in gitObjectList) {
dir(gitObject.name) {
gitClone(gitObject.url, gitObject.branche, "$CREDENTIALS_ID", gitObject.name)
}
}
}
def gitClone(String remoteAddr, String branch, String credentialsId,String dirName) {
checkout([$class: 'GitSCM',
branches: [[name: (branch)]], doGenerateSubmoduleConfigurations: false,
extensions: [], submoduleCfg: [],
userRemoteConfigs: [[credentialsId: (credentialsId), url: (remoteAddr)]]])
}
遍历实体类执行部署函数
//遍历实体类执行部署函数
def deployExecutionList(ArrayList gitObjectList) {
for(gitObject in gitObjectList) {
deployExecution(gitObject.deployPath)
}
}
//部署命令
def deployExecution(String path) {
sh "cd $path && mvn clean deploy -Dmaven.test.skip=true"
}
全部代码
import hudson.model.*;
def gitClone(String remoteAddr, String branch, String credentialsId,String dirName) {
checkout([$class: 'GitSCM',
branches: [[name: (branch)]], doGenerateSubmoduleConfigurations: false,
extensions: [], submoduleCfg: [],
userRemoteConfigs: [[credentialsId: (credentialsId), url: (remoteAddr)]]])
}
//git初始化对象
class GitObject {
//对象名称,同时为下载目录
def name
//gitlab克隆链接
def url
//代码分支
def branche
//deploy的根目录
def deployPath
}
//克隆map,在初始化时添加所有分支链接
def downloadMap
//部署目录map,在初始化时添加所有分支部署目录
def deployMap
//git初始化对象列表
def gitObjectList
//根据选择标签,初始化所选各个分支实体类
def initGitObject(String name, def downloadMap, def deployMap, String branche) {
GitObject gitObject = new GitObject()
gitObject.name = name
gitObject.url = downloadMap.get(name)
gitObject.branche = branche
gitObject.deployPath = deployMap.get(name)
return gitObject
}
//分支转换函数
def branchSwitch(String branch) {
switch (branch.startsWith('V')) {
case true:
return "refs/tags/" + branch
break
case false:
return "refs/heads/" + branch
break
default:
return "refs/tags/" + branch
break
}
}
//遍历实体类下载代码
def gitCloneModulesList(ArrayList gitObjectList) {
for(gitObject in gitObjectList) {
dir(gitObject.name) {
gitClone(gitObject.url, gitObject.branche, "$CREDENTIALS_ID", gitObject.name)
}
}
}
//部署命令
def deployExecution(String path) {
sh "cd $path && mvn clean deploy -Dmaven.test.skip=true"
}
//遍历实体类执行部署函数
def deployExecutionList(ArrayList gitObjectList) {
for(gitObject in gitObjectList) {
deployExecution(gitObject.deployPath)
}
}
pipeline{
agent{
node{
label 'Nodexx.xxx.xx.xxx'
}
}
parameters{
choice(description: '代码分支选择',name: 'k_tag',choices: ['master','test','develop'])
booleanParam (defaultValue: true, description: 'Deploy 模块', name: 'deploy_modules')
booleanParam (defaultValue: true, description: '全选,默认选中,选中后以下模块可不选择', name: 'all_tag')
booleanParam (defaultValue: false, description: 'koca-base代码', name: 'kb_tag')
booleanParam (defaultValue: false, description: 'koca-boot代码', name: 'kbo_tag')
booleanParam (defaultValue: false, description: 'koca-cloud代码', name: 'kc_tag')
booleanParam (defaultValue: false, description: 'koca-admin代码', name: 'ka_tag')
booleanParam (defaultValue: false, description: 'koca-studio代码', name: 'ks_tag')
booleanParam (defaultValue: false, description: 'koca-amo代码', name: 'kd_tag')
booleanParam (defaultValue: false, description: 'koca-lcp-runtime代码', name: 'klr_tag')
booleanParam (defaultValue: false, description: 'koca-parent代码', name: 'kp_tag')
}
environment{
CREDENTIALS_ID = 'git-koca'
DEFAULT_BRANCH = 'refs/heads/master'
KOCA_CLOUD_LOAD_TAG = 'koca-cloud-loadbalancer'
KOCA_DASHBOARD_TAG = 'koca-dashboard'
}
stages{
stage('初始化'){
steps{
script{
//初始化克隆map
downloadMap = [
'koca-base':'http://xx.xxx.x.x/KOCA/koca-base.git',
'koca-boot':'http://xx.xxx.x.x/KOCA/koca-boot.git',
'koca-cloud':'http://xx.xxx.x.x/KOCA/koca-cloud.git',
'koca-cloud-gateway':'http://xx.xxx.x.x/KOCA/koca-cloud-loadbalancer.git',
'koca-admin':'http://xx.xxx.x.x/KOCA/koca-admin.git',
'koca-studio':'http://xx.xxx.x.x/KOCA/koca-studio.git',
'koca-amo':'http://xx.xxx.x.x/KOCA/koca-amo.git',
'koca-dashboard':'http://xx.xxx.x.x/KOCA/koca-dashboard.git',
'koca-lcp-runtime':'http://xx.xxx.x.x/KOCA/koca-lcp.git',
'koca-parent':'http://xx.xxx.x.x/KOCA/koca-base.git',
]
//初始化部署目录map
deployMap = [
'koca-base':'koca-base',
'koca-boot':'koca-boot',
'koca-cloud-gateway':'koca-cloud-gateway',
'koca-cloud':'koca-cloud',
'koca-cloud-loadbalancer':'koca-cloud-loadbalancer',
'koca-admin':'koca-admin',
'koca-studio':'koca-studio/src',
'koca-amo':'koca-amo/src',
'koca-dashboard':'koca-dashboard/src',
'koca-lcp-runtime':'koca-lcp-runtime',
'koca-parent':'koca-parent/parent/koca-parent',
]
//分支标签
def branchTag = branchSwitch("$k_tag")
//将各个模块添加到list中
gitObjectList = new ArrayList()
if(params.kb_tag || params.all_tag){
gitObjectList.add(initGitObject('koca-base', downloadMap, deployMap, branchTag))
}
if(params.kbo_tag || params.all_tag){
gitObjectList.add(initGitObject('koca-boot', downloadMap, deployMap, branchTag))
}
if(params.kc_tag || params.all_tag){
gitObjectList.add(initGitObject('koca-cloud', downloadMap, deployMap, branchTag))
gitObjectList.add(initGitObject('koca-cloud-gateway', downloadMap, deployMap, branchTag))
// gitObjectList.add(initGitObject('koca-cloud-loadbalancer', downloadMap, deployMap, branchTag))
}
if(params.ka_tag || params.all_tag){
gitObjectList.add(initGitObject('koca-admin', downloadMap, deployMap, branchTag))
}
if(params.ks_tag || params.all_tag){
gitObjectList.add(initGitObject('koca-studio', downloadMap, deployMap, branchTag))
}
if(params.kd_tag || params.all_tag){
gitObjectList.add(initGitObject('koca-amo', downloadMap, deployMap, branchTag))
// map.put("${KOCA_DASHBOARD_TAG}", branchSwitch("$k_tag"))
}
if(params.klr_tag || params.all_tag){
gitObjectList.add(initGitObject('koca-lcp-runtime', downloadMap, deployMap, branchTag))
}
if(params.kp_tag || params.all_tag){
gitObjectList.add(initGitObject('koca-parent', downloadMap, deployMap, branchTag))
}
}
}
}
stage('拉取代码'){
steps{
println("拉取代码")
script{
gitCloneModulesList(gitObjectList)
}
}
}
stage('发布到maven制品库'){
when{
expression{
return params.deploy_modules
}
}
steps{
println("发布到maven制品库")
script{
deployExecutionList(gitObjectList)
}
}
}
}
}