环境
- 变量:env(环境变量:sit/dev/simulation/prod/all),job(job-name/all)
- 目录:/var/lib/jenkins/jenkinsfile
- environment.json:
[root@test-01 jenkinsfile]# cat environment.json | jq .
{
"environment": [
{
"sit": "http://xxxx.xx.xx.com/|test:xxxxxxxxxxxxxxxx",
"dev": "http://xxxx.xx.xx.com/|test:xxxxxxxxxxxxxxxx",
"simulation": "http://xxxx.xx.xx.com/|test:xxxxxxxxxxxxxxxx",
"prod": "http://xxxx.xx.xx.com/|test:xxxxxxxxxxxxxxxx",
}
]
}
设计思路
- 以sit环境的url和token作为基准baseUrl/baseToken
- 若env=all则表示同步至所有环境
-
- 循环获取env的环境变量,获取url和token
- 若job=all则表示同步所有job
-
-
- 获取当前基准环境下的job_list,循环获取jobname
- 判断当前的job是否有config.xml的文件
-
-
-
-
- 若第一行=<html>则无xml文件,在目标环境创建job
- 若存在xml文件,则更新job
-
-
-
- 若job=jobname则同步单个job
- 判断当前的job是否有config.xml的文件
-
-
-
- 若第一行=<html>则无xml文件,在目标环境创建job
- 若存在xml文件,则更新job
-
-
- 若env=env则表示同步至单个环境
-
- 获取目标环境的url和token
- 若job=all则表示同步所有job
-
-
- 获取当前基准环境下的job_list,循环获取jobname
- 判断当前的job是否有config.xml的文件
-
-
-
-
- 若第一行=<html>则无xml文件,在目标环境创建job
- 若存在xml文件,则更新job
-
-
-
- 若job=jobname则同步单个job
- 判断当前的job是否有config.xml的文件
-
-
-
- 若第一行=<html>则无xml文件,在目标环境创建job
- 若存在xml文件,则更新job
-
-
完整代码
pipeline {
agent any
options {
disableConcurrentBuilds()
}
parameters {
string(name: "env", defaultValue: '', description: '')
string(name: "job", defaultValue: '', description: '')
}
stages {
stage("Sync job") {
steps {
script {
dir("/var/lib/jenkins/jenkinsfile") {
baseUrl = "http://xxx.xx.xx.com/"
baseToken = "test:xxxxxxxxxxxxxxxxxxxxxxxxxx"
def env = "${params.env}"
if ( env == "all" ) {
getEnvNumber = "cat environment.json | jq -r .environment[][] | wc -l"
def envNumber = sh(script: "$getEnvNumber", returnStdout:true).trim()
for ( i=2; i<=jobNumber.toInteger(); i++ ) {
getAllUrlCommand = "cat environment.json | jq -r .environment[][] | sed -n $i'p' | awk -F \"|\" '{print\$1}'"
getAllTokenCommand = "cat environment.json | jq -r .environment[][] | sed -n $i'p' | awk -F \"|\" '{print\$2}'"
def url = sh(script: "$getAllUrlCommand", returnStdout:true).trim()
def token = sh(script: "$getAllTokenCommand", returnStdout:true).trim()
def job = "${params.job}"
if ( job== "all" ) {
sh "sudo curl -X GET $baseUrl/api/json?pretty=true -u $baseToken > job_list.txt"
getJobNumber = "cat job_list.txt | jq -r .jobs[].name | wc -l"
def jobNumber = sh(script: "$getJobNumber", returnStdout:true).trim()
for (j=1; j<=jobNumber.toInteger(); j++) {
def jobname = sh(script: "cat job_list.txt | jq -r .jobs[].name | sed -n $j'p'", returnStdout:true).trim()
sh "sudo curl -X GET $baseUrl/job/$jobname/config.xml -u $baseToken -o config.xml"
getCRUMBCommand= "sudo curl -s '$url/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,\":\",//crumb)' -u $token"
def CRUMB = sh(script: "$getCRUMBCommand", returnStdout:true).trim()
def str = sh(script: "curl -sX GET $url/job/$jobname/config.xml -u $token | sed -n 1p", returnStdout:true).trim()
if( str == "<html>") {
sh "sudo curl -s -XPOST '$url/createItem?name=$jobname' -u $token --data-binary @config.xml -H \"$CRUMB\" -H \"Content-Type:text/xml\""
}
else {
sh "sudo curl -s -XPOST '$url/job/$jobname/config.xml' -u $token --data-binary @config.xml -H \"$CRUMB\" -H \"Content-Type:text/xml\""
}
}
}
else {
sh "sudo curl -X GET $baseUrl/api/json?pretty=true -u $baseToken > job_list.txt"
sh "sudo curl -X GET $baseUrl/job/$job/config.xml -u $baseToken -o config.xml"
getCRUMBCommand= "sudo curl -s '$url/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,\":\",//crumb)' -u $token"
def CRUMB = sh(script: "$getCRUMBCommand", returnStdout:true).trim()
def str = sh(script: "curl -sX GET $url/job/$job/config.xml -u $token | sed -n 1p", returnStdout:true).trim()
if( str == "<html>") {
sh "sudo curl -s -XPOST '$url/createItem?name=$job' -u $token --data-binary @config.xml -H \"$CRUMB\" -H \"Content-Type:text/xml\""
}
else {
sh "sudo curl -s -XPOST '$url/job/$job/config.xml' -u $token --data-binary @config.xml -H \"$CRUMB\" -H \"Content-Type:text/xml\""
}
}
}
}
else {
getUrlCommand = "cat environment.json | jq -r .environment[].$env | awk -F \"|\" '{print\$1}'"
getTokenCommand = "cat environment.json | jq -r .environment[].$env | awk -F \"|\" '{print\$2}'"
def url = sh(script: "$getUrlCommand", returnStdout:true).trim()
def token = sh(script: "$getTokenCommand", returnStdout:true).trim()
def job = "${params.job}"
if ( job== "all" ) {
sh "sudo curl -X GET $baseUrl/api/json?pretty=true -u $baseToken > job_list.txt"
getJobNumber = "cat job_list.txt | jq -r .jobs[].name | wc -l"
def jobNumber = sh(script: "$getJobNumber", returnStdout:true).trim()
for (j=1; j<=jobNumber.toInteger(); j++) {
def jobname = sh(script: "cat job_list.txt | jq -r .jobs[].name | sed -n $j'p'", returnStdout:true).trim()
sh "sudo curl -X GET $baseUrl/job/$jobname/config.xml -u $baseToken -o config.xml"
getCRUMBCommand= "sudo curl -s '$url/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,\":\",//crumb)' -u $token"
def CRUMB = sh(script: "$getCRUMBCommand", returnStdout:true).trim()
def str = sh(script: "curl -sX GET $url/job/$jobname/config.xml -u $token | sed -n 1p", returnStdout:true).trim()
if( str == "<html>") {
sh "sudo curl -s -XPOST '$url/createItem?name=$jobname' -u $token --data-binary @config.xml -H \"$CRUMB\" -H \"Content-Type:text/xml\""
}
else {
sh "sudo curl -s -XPOST '$url/job/$jobname/config.xml' -u $token --data-binary @config.xml -H \"$CRUMB\" -H \"Content-Type:text/xml\""
}
}
}
else {
sh "sudo curl -X GET $baseUrl/api/json?pretty=true -u $baseToken > job_list.txt"
sh "sudo curl -X GET $baseUrl/job/$job/config.xml -u $baseToken -o config.xml"
getCRUMBCommand= "sudo curl -s '$url/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,\":\",//crumb)' -u $token"
def CRUMB = sh(script: "$getCRUMBCommand", returnStdout:true).trim()
def str = sh(script: "curl -sX GET $url/job/$job/config.xml -u $token | sed -n 1p", returnStdout:true).trim()
if( str == "<html>") {
sh "sudo curl -s -XPOST '$url/createItem?name=$job' -u $token --data-binary @config.xml -H \"$CRUMB\" -H \"Content-Type:text/xml\""
}
else {
sh "sudo curl -s -XPOST '$url/job/$job/config.xml' -u $token --data-binary @config.xml -H \"$CRUMB\" -H \"Content-Type:text/xml\""
}
}
}
}
}
}
}
}
}
ide思路:
每个环境不同
- 进入code目录中,进入jenkins-pipeline/目录
- ls -l获取目录下所有的子目录及文件,count所有子目录及文件的数量
- for循环整个Jenkins-pipeline下的子目录和文件
-
- 获取子目录或文件的name
- 判断是否为目录
-
-
- 若为目录则cd $name
-
-
-
-
- ls -l获取目录下的所有文件,dircount所有文件的数量
-
-
-
-
-
-
- for循环整个name下的所有文件
-
-
-
-
-
-
-
-
- 获取jobname
- 获取jobname所对应job的config.xml文件
- 判断当前的job是否有config.xml的文件
-
-
-
-
-
-
-
-
-
-
- 若第一行=<html>则无xml文件,在目标环境创建job
- 若存在xml文件,则更新job
-
-
-
-
-
-
-
- 若为文件则
-
-
-
-
- 获取jobname
- 获取jobname所对应job的config.xml文件
- 判断当前的job是否有config.xml的文件
-
-
-
-
-
-
- 若第一行=<html>则无xml文件,在目标环境创建job
- 若存在xml文件,则更新job
-
-
-