[Up]常用資訊

[重點文章] 重點文章 [重點文章] 重點文章

2020年4月22日 星期三

[Jenkins] Jenkins 執行過程中檢查 slack 未成功後 重新傳送 script

[Jenkins] Jenkins 執行過程中檢查 slack 未成功後 重新傳送 script

在執行 jenkins 過程中有些資訊會利用 slack webhook 方式將資訊傳送出去
 
但是如果當下異常就無法傳送,所以就改一下檢查方式讓 slack webhook 不會漏送
 
下面為執行的Script

#!/bin/bash
#$CN_KU_HK_StatusCheck
Jname=`echo $JOB_NAME | tr [:upper:] [:lower:] | cut -c1-2`
echo $Jname
timedir=$(date +%F)
echo $timedir
/usr/bin/mkdir -p /home/F5/jenkins_log/$Jname-$timedir
jbnamevab=$XX_XX_XX_PoolMember_Status_Check5_vab
#
#
for i in $(echo $jbnamevab | sed "s/,/\n/g" ); 
do 
 echo $i
    curl -X POST http://127.0.0.1:8080/jenkins/job/$i/build?delay=0sec --user $USERNAME:$PASSWORD

    sleep 5
    
done
echo Build finished
/usr/bin/touch /home/F5/jenkins_log/$JOB_NAME-$BUILD_NUMBER-log-file
#
for i in $(echo $jbnamevab | sed "s/,/\n/g" ); 
do
    JOB_URL=http://127.0.0.1:8080/jenkins/job/$i
    JOB_STATUS_URL=${JOB_URL}/lastBuild/api/json
    GREP_RETURN_CODE=0
    
    # Start the build
    #curl --user "$USERNAME:$PASSWORD" $JOB_URL/build?delay=0sec
    
    # Poll every 10 second  until the build is finished
    while [ $GREP_RETURN_CODE -eq 0 ]
    do
        sleep 3
        # Grep will return 0 while the build is running:
        curl --user "$USERNAME:$PASSWORD" $JOB_STATUS_URL | grep result\":null > /dev/null
        GREP_RETURN_CODE=$?
    done
    echo "Build Check finished"
    echo -e "$i" | cat >> /home/F5/jenkins_log/$JOB_NAME-$BUILD_NUMBER-log-file
    /usr/bin/find /home/F5/jenkins_log/$i-* -type f -amin -10 -exec cat {} \;| grep "offline\|disabled" | awk '{ print $1 " " $4 " 連線數: " $9 }' >> /home/F5/jenkins_log/$JOB_NAME-$BUILD_NUMBER-log-file
    echo "$i-Out-put-log"
#
    mv /home/F5/jenkins_log/$i-* /home/F5/jenkins_log/$Jname-$timedir
    sleep 5
done
#
#
# 顯示執行清單
sleep 10
# 請設定 60 分秒後在撈取執行清單
#
Sname=`echo $JOB_NAME | tr [:upper:] [:lower:] | cut -c1-2`
#cat /home/F5/jenkins_log/$JOB_NAME-$BUILD_NUMBER-log-file | /home/F5/slackweb.sh $JOB_NAME-$BUILD_NUMBER
SLACKREP_RETURN_CODE=7
Countslack=1;
while [ $SLACKREP_RETURN_CODE -eq 7 ]
do
    sleep 3
    echo "Check-num = $Countslack"
    Countslack=$((Countslack+1))
    # Grep will return 0 while the build is running:
    cat /home/F5/jenkins_log/$JOB_NAME-$BUILD_NUMBER-log-file | /home/F5/slackweb_Test.sh $JOB_NAME-$BUILD_NUMBER $Sname
    SLACKREP_RETURN_CODE=$?
done
echo "chek-send-slack-OK"
#
#for i in $(echo $jbnamevab | sed "s/,/\n/g" ); 
#do 
# echo $i
#    curl -s http://127.0.0.1:8080/jenkins/job/$i/lastBuild/consoleText --user $USERNAME:$PASSWORD
#    
#    sleep 2
#done

mv /home/F5/jenkins_log/$JOB_NAME-* /home/F5/jenkins_log/$Jname-$timedir

沒有留言:

張貼留言