关于Jenkins自动化pipeline模式(结合python+shell搞一波)
//两个变量对应Jenkins项目设置入参
def project_name = project_names.replaceAll('"','').split(',')
def project_host = project_hosts.replaceAll('"','').split(',')
pipeline{
agent any //全局必须带有agent表明此pipeline执行节点,这边采用默认master分支,当然分支可选选
parameters {
gitParameter branchFilter: 'origin/(.*)',
defaultValue: 'master',
selectedValue: 'DEFAULT',
name: 'BRANCH',
type: 'PT_BRANCH',
description: 'select you branch or tag.'
}
//指定jdk版本,此处对应Jenkins的全局变量
tools {
jdk 'java'
}
stages{
stage("git pull"){
// agent { label 'master' } //具体执行的步骤节点,非必须
steps{
checkout([$class: 'GitSCM',
branches: [[name: "${params.BRANCH}"]],
doGenerateSubmoduleConfigurations: false,
extensions: [],
gitTool: 'Default',
userRemoteConfigs: [[url: ' http://对应仓库地址 ',credentialsId: '对应Jenkins上的绑定账号id']]
])
}
}
//指定maven的配置文件
stage("mvn package"){
steps{
sh """
echo $project_name
mvn clean -U package -settings /maven/conf/test.xml -Dmaven.test.skip=true
"""
}
}
//对多服务入参选择遍历发布
stage("release"){
steps{
buildpb_for_loop(project_name,project_host)
}
}
}
//此处编写发布成功或失败的提示脚本
post {
success {
wrap([$class: 'BuildUser']) {
sh '''
sh ${JENKINS_HOME}/Success.sh
'''
}
}
failure {
wrap([$class: 'BuildUser']) {
sh '''
sh ${JENKINS_HOME}/Failure.sh
'''
}
}
}
}
def buildpb_for_loop(list,host) {
for (int j = 0; j < host.size(); j++) {
if ( "${host[j]}" == "127.0.0.1") {
for (int i = 0; i < list.size(); i++) {
if ( "${list[i]}" == "all") {
sh "echo 开始编译:${list[i]} ${host[j]}"
} else if ( "${list[i]}" == "test1" ){
sh """
set +x
cho 开始编译:${list[i]} ${host[j]}
scp 传包文件
ssh 执行各种脚步
"""
}
}
} else if ( "${host[j]}" == "127.0.0.2" ){
for (int i = 0; i < list.size(); i++) {
if ( "${list[i]}" == "all") {
sh "echo 开始编译:${list[i]} ${host[j]}"
} else if ( "${list[i]}" == "1" ){
sh """
set +x
cho 开始编译:${list[i]} ${host[j]}
scp 传包文件
ssh 执行各种脚步
"""
}
}
}
}
}
}