在使用 jenkins 過程中, 有時候需要使用命令修改 代碼,或者配置文件,并提交
典型的應(yīng)用為 把策劃方案 excel 轉(zhuǎn)換為json 文件 供程序來使用.
下面我實(shí)現(xiàn)了一種
pipeline {
agent {
label 'mac170'
}
stages {
stage('Git clone repo') {
steps {
//cleanWs()
git credentialsId: 'xxxx-xxxx-xxxx-xxxx-xxxx', url: 'http://xxxx/xxxx.git'
sh '''
tempBranch=${branch}
IFS='/'
read -ra strarr <<< "${tempBranch}"
IFS=''
#git branch --delete --force ${strarr[1]}
git checkout --track -B ${strarr[1]} ${branch}
git clean -df
#git checkout --force ${strarr[1]}
git switch ${strarr[1]}
git reset --hard ${branch}
#git pull origin ${strarr[1]}:${strarr[1]}
printenv
'''
}
}
stage('Change code stage'){
steps {
sh '''
tempBranch=${branch}
IFS='/'
read -ra strarr <<< "${tempBranch}"
IFS=''
echo "導(dǎo)出config"
#balabala 做事情,或者調(diào)用其他腳本做事情
git add xxxxx.json
git add xxxxx.json
#獲取修改的文件數(shù)量
lineCnt=`git status -s | wc -l`
lineCnt=`eval echo $lineCnt`
if [[ $lineCnt != 0 ]] ; then
echo "有$lineCnt個文件需要提交"
git commit -m "update by jenkins"
git push origin ${strarr[1]}
else
echo "沒有任何更新,不需要提交!"
fi
'''
}
}
}
}
其中 branch 變量為git paramter 插件的選擇結(jié)果

git paramter 插件

git paramter 設(shè)置