-
1.通过chang-id获取所有number信息
ssh -p 29418 xxx@$GERRIT_HOST gerrit query status:open change:${GERRIT_CHANGE_ID}|grep -E '^ number: '|awk -F \" number: \" '{print \$2}'|tr '\\n' ','
-
2.开始循环number信息
ChNumList = numbers.split(',')
-
3.分别获取MR信息
for (number in ChNumList){
.....
}
ssh -p 29418 xxx@$GERRIT_HOST gerrit query status:open change:%s|grep -E '^ project: '|awk -F \" project: \" '{print \$2}' #获取project信息
ssh -p 29418 xxx@$GERRIT_HOST gerrit query status:open --current-patch-set change:%s|grep -E '^ number: '|awk -F \" number: \" '{print \$2}' #获取patchset
def curbranch = sh(script: String.format("ssh -p 29418 xxx@$GERRIT_HOST gerrit query status:open change:%s|grep -E '^ branch: '|awk '{print \$NF}'", env.GERRIT_HOST, number), returnStdout: true).trim() #获取当前分支
LocalMainUrl = sh(script: "git config --get remote.origin.url", returnStdout:true).trim() #获取代码分支
- 4.判断分支
def Count = sh(script:String.format("grep -r \'name=\"%s\"\' %s", project, Manifest_path), returnStdout: true).trim() #获取manifest文件里面当前project的数量
if (Count != ""){
def RevCheck = sh(script: "grep -r \"name=\\\"" + project + "\\\"\" " + Manifest_path + "|grep \"revision=\"|xargs echo", returnStdout: true).trim()
if(RevCheck != ""){
ManiBranch = sh(script: "grep -r \"name=\\\"" + project + "\\\"\" " + Manifest_path + "|awk -F \"revision=\\\"\" '{print \$2}'|awk -F \"\\\"\" '{print \$1}'", returnStdout: true).trim()
}else{
ManiBranch = sh(script: "grep -r \"<default\" " + Manifest_path + "|awk -F \"revision=\\\"\" '{print \$2}'|awk -F \"\\\"\" '{print \$1}'", returnStdout: true).trim()
}
if(ManiBranch == curbranch){
judge_branch += 1
sh String.format("ssh -p 29418 xxx@%s gerrit review %s,%s --message \\\"Build Started in %s\\\"", env.GERRIT_HOST, number, patchset, env.BUILD_URL)
}
} #判断project是否存在
if (CHANGES == ''){
CHANGES = "$project,$number,$patchset"
}else{
CHANGES = CHANGES + ";$project,$number,$patchset"
}
- 5.切割字符串,循环修改仓
change_list = CHANGES.split(';')
for(int i = 0;i<change_list.length;i++){
def change = change_list[i]
println change
def data = change.split(',')
def project = data[0]
def number = data[1]
def patchset = data[2]
def path_List = [project]
print("++++++++")
println path_List
project_num = sh(script:String.format("grep -r \'name=\"%s\"\' %s|wc -l", project, Manifest_path), returnStdout: true).trim()
if (project_num != '1'){
path_List = []
paths = sh(script:String.format("grep -r \'name=\"%s\"\' %s|awk -F ' ' '{print \$3}'|awk -F '\"' '{print \$2}'|tr '\\n' ','", project, Manifest_path), returnStdout: true).trim()
println paths
path_List = paths.split(',')
print("++++++++")
println path_List
}
for(num in path_List){
println num
dir(ManifestDir){
def currentpath = pwd()
println currentpath
def checkPrj = sh(script: "repo status " + num + " 2>&1|grep -E 'not found'|wc -l", returnStdout: true).trim()
println checkPrj
if ( checkPrj != '0' ){
print("prj " + num + " not found in current build")
currentBuild.result = 'UNSTABLE'
sh "exit 1"
}
print(String.format("downloading %s %s %s", num, number, patchset))
sh String.format('repo download %s %s/%s -c', num, number, patchset)
}
}
}
- 6.repo下载代码
while(times < 0 || ++retries <= times){
println(codepath)
try{
if (retries >= 1){
dir(codepath){
sh(script:String.format('rm -rf %s/* .repo;ls -al',codepath))
}
}
dir(codepath){
if (!fileExists('.repo')){
sh(script:String.format('rm -rf %s/*;ls -al',codepath))
sh(script:String.format('repo init --repo-url %s --repo-branch=%s -u %s -b %s -m %s -g all --no-repo-verify --depth 25;if [ \$? -ne "0" ];then echo ">> Error:downcode failed,repo init failed!!";exit 1;fi',repo_url,repo_branch,manifest_url,manifest_branch,manifest_xml))
}
else if (!fileExists('.repo/manifest.xml') || !fileExists('.repo/repo') || !fileExists('.repo/manifests')){
sh(script:String.format('rm -rf %s/* %s/.repo;ls -al',codepath,codepath))
sh(script:String.format('repo init --repo-url %s --repo-branch=%s -u %s -b %s -m %s -g all --no-repo-verify --depth 25;if [ \$? -ne "0" ];then echo ">> Error:downcode failed,repo init failed!!";exit 1;fi',repo_url,repo_branch,manifest_url,manifest_branch,manifest_xml))
}
else{
dir(codepath+'/.repo/manifests'){
sh(script:String.format('git checkout %s', manifest_xml))
LocalMainUrl = sh(script: "git config --get remote.origin.url", returnStdout:true).trim()
println("LocalMainUrl="+LocalMainUrl)
LocalMainBranch = sh(script: "git config --get branch.default.merge|awk -F '/' '{print \$NF}'", returnStdout:true).trim()
println("LocalMainBranch="+LocalMainBranch)
}
if (LocalMainBranch != manifest_branch || LocalMainUrl != manifest_url){
println("LocalMainUrl="+LocalMainUrl+","+"LocalMainBranch="+LocalMainBranch)
sh(script:String.format('rm -rf %s/* %s/.repo;ls -al',codepath,codepath))
sh(script:String.format('repo init --repo-url %s --repo-branch=%s -u %s -b %s -m %s -g all --no-repo-verify --depth 25;if [ \$? -ne "0" ];then echo ">> Error:downcode failed,repo init failed!!";exit 1;fi',repo_url,repo_branch,manifest_url,manifest_branch,manifest_xml))
}
else{
dir(codepath){
println("start: repo forall git reset m/"+manifest_branch)
if (fileExists('reset_log.txt')){
sh 'rm -rf reset_log.txt'
}
if (isGate != "YES"){
sh(script:String.format('repo forall -c \'git clean -dxf\''))
}
sh(script:String.format('repo forall -c \'git reset m/%s --hard;result=\$?;echo \${result};if [ \${result} != 0 ];then echo %s failnum \${result} |tee %s/reset_log.txt;fi\';if [ \$? -ne "0" ];then echo ">> Error:downcode failed,repo forall failed!!";exit 1;elif [[ -e reset_log.txt ]];then cat reset_log.txt;echo ">> Error:downcode failed,repo forall failed";exit 1;fi',manifest_branch,codepath,codepath))
println("end: repo forall git reset m/"+manifest_branch)
sh(script:String.format('repo init --repo-url %s --repo-branch=%s -u %s -b %s -m %s -g all --no-repo-verify --depth 25;if [ \$? -ne "0" ];then echo ">> Error:downcode failed,repo init failed!!";exit 1;fi',repo_url,repo_branch,manifest_url,manifest_branch,manifest_xml))
}
}
}
def repo_result = sh(script:'repo sync -d -j8 --no-tags 2>&1| tee git_pull.log;if [ \${PIPESTATUS[0]} != 0 ];then echo ">> Error:downcode failed";exit 1;fi', returnStatus: true)
//println "xxxxxxxxxxxxxxxxx"
//repo_result = sh(script:"history", returnStatus: true)
println(repo_result)
if (repo_result == 0){
println("repo sync successful !!")
}
else{
println("repo sync error !!")
sh "exit 1"
}
}