[Jenkins] Shell Script 觸發多個 工作
使用 shell script 觸發 多個job 並檢查 執行工作狀態後進行下個動作
### Start Script
#!/bin/bash
funcprepare(){
i=0
while [ $i -le 2 ]
do
read -r -p "是否執行過發佈前腳本? [Y/n] " input
case $input in
[yY][eE][sS]|[yY])
echo "Yes"
let i++
;;
[nN][oO]|[nN])
echo "No"
exit
;;
*)
echo "請確認是否執行過發佈前腳本..."
;;
esac
done
}
funcomplie(){
echo "[tags]進入funcomplie函數"
for i in `cat complie.txt | tr -d '[0-9]' |tr -d ' ' |tr -d '.' |grep -v '^$'`
do
if [[ "new-test-base" =~ "$i" ]] || [[ "new-test-sdk" =~ "$i" ]];then
#if [ "new-ac-parent" == *$i* ] || [ "new-ac-sdk" == *$i* ];then
echo "[tags]即將部署專案 $i"
curl -s -u jiaminxu:1161350de54c880d5b08c2616a1b6c7243 -X POST "http://192.168.10.204:8080/job/new-$i/buildWithParameters?token=123456 " -d 'env=master'
sleep 10
funcbase
else
echo "[tags]部署非基礎模組: $i"
curl -s -u jiaminxu:1161350de54c880d5b08c2616a1b6c7243 -X POST "http://192.168.10.204:8080/job/new-$i/buildWithParameters?token=123456 " -d 'env=master&deploy_step=Deploy'
fi
done
}
funcbase(){
echo "[tags]進入funcbase函數"
echo i:$i
complie_result=`curl -s -u jiaminxu:1161350de54c880d5b08c2616a1b6c7243 -X POST "http://192.168.10.204:8080/job/new-$i/lastBuild/api/json?token=123456 "|grep -Po '"result":".*?"'|awk -F : '{print $2}'|sed 's/\"/ /'g`
echo 項目名 $complie_result
time=0
while [ "$complie_result" == "" ]
do
#echo "部署編譯完成 $complie_result "
echo "編譯還在進行中"
sleep 5
let time+=5
echo 項目編譯時間 $time
complie_result=`curl -s -u jiaminxu:1161350de54c880d5b08c2616a1b6c7243 -X POST "http://192.168.10.204:8080/job/new-$i/lastBuild/api/json?token=123456 "|grep -Po '"result":".*?"'|awk -F : '{print $2}'|sed 's/\"/ /'g`
echo 項目名 $complie_result
done
#echo "編譯還在進行中"
echo "部署編譯完成 $complie_result "
result=$(echo $complie_result |grep "SUCCESS")
echo 項目名 $complie_result
if [[ "$result" != "" ]]
then
echo "部署結果包含success 編譯完成 "
else
echo "編譯失敗"
exit 7
fi
}
echo "[tags] 開始執行編譯 complie"
echo "[tags] 開始執行編譯 complie"
echo "[tags] 開始執行編譯 complie"
for i in `cat complie.txt | tr -d '[0-9]' |tr -d ' ' |tr -d '.' |grep -v '^$' `
#for i in `cat 2.txt | tr -d '[0-9]' |tr -d ' ' |tr -d '.' |grep -v '^$'|grep 'core' `
#for i in `cat 2.txt | tr -d '[0-9]' |tr -d ' ' |tr -d '.' |grep -v '^$'|grep -v 'core' `
do
#echo "[tags]原發佈項目: $i"
echo "[tags]處理過後發佈的專案: ${i%*-}"
done
funcprepare
funcomplie
### END script###
complie.txt如下 裡面寫需要發佈的專案
test-base
test-sdk
test-api
使用指令方式
##
# To 儲存 job config.xml
curl -X GET 'http://127.0.0.1:8080/jenkins/job//config.xml' -u username:API_TOKEN -o .xml
# 使用 存儲的 config to 建立 a new job
curl -s -XPOST 'http://127.0.0.1:8080/jenkins/createItem?name=' -u username:API_TOKEN --data-binary @.xml -H "Content-Type:text/xml"
# get all jenkins jobs
curl -X GET 'http://127.0.0.1:8080/jenkins/api/json?pretty=true' -u username:API_TOKEN -o jobs.json
# get jenkins view
curl -X GET 'http://127.0.0.1:8080/jenkins/view//api/json' -u username:API_TOKEN -o view.json
#
java -jar jenkins-cli.jar -s http://127.0.0.1:8080/jenkins -auth username:API_TOKEN list-jobs
##
Script 自身參數化配置
echo $args[0]
java -jar jenkins-cli.jar -s http://127.0.0.1:8080/jenkins -auth username:API_TOKEN list-jobs
新的標示方式 以這樣設定
Script 自身參數化配置
Script 自身參數化配置
#Script 自身參數化配置
##
echo $args[0]
java -jar jenkins-cli.jar -s http://127.0.0.1:8080/jenkins -auth username:API_TOKEN list-jobs
# code 使用
echo $args[0]
java -jar jenkins-cli.jar -s http://127.0.0.1:8080/jenkins -auth username:API_TOKEN list-jobs
[09:10:26 root@Debian10-2-Jenkins-Ansible-02 ~]# apt update
[09:10:26 root@Debian10-2-Jenkins-Ansible-02 ~]# apt install default-jdk
OR
[09:10:26 root@Debian10-2-Jenkins-Ansible-02 ~] $ sudo apt update
[09:10:26 root@Debian10-2-Jenkins-Ansible-02 ~] $ sudo apt install default-jdk
##
# code 使用 二
echo $args[0]
java -jar jenkins-cli.jar -s http://127.0.0.1:8080/jenkins -auth username:API_TOKEN list-jobs
[09:10:26 root@Debian10-2-Jenkins-Ansible-02 ~]# apt update
[09:10:26 root@Debian10-2-Jenkins-Ansible-02 ~]# apt install default-jdk
OR
[09:10:26 root@Debian10-2-Jenkins-Ansible-02 ~] $ sudo apt update
[09:10:26 root@Debian10-2-Jenkins-Ansible-02 ~] $ sudo apt install default-jdk
沒有留言:
張貼留言