Linux ·

使用linux的shell脚本自动触发jenkins构建并爬取结果

现在越来越多的东西都在使用jenkins进行自动构建,也极大的方便了测试与运维人员进行打包部署,但唯一美中不足的是,不能进行自动部署,昨天写了一个脚本来做这项工作,以达到一键自动打包部署.

#!/bin/sh

# jenkins url
export jenkins_job_url=http://192.16.30.83:8080/job

# user and password
user=leo.du:123456

# jenkins name for project
jenkins_name=$1

result=''

function check_result() {
    # clean tmp.txt, tmp.html
    rm -f tmp.txt tmp.html

    echo -n .

    # get the build result
    wget -q ${jenkins_job_url}/${jenkins_name}/ -O tmp.html
    grep "job/${jenkins_name}/.*/console" tmp.html | head -n 1 > tmp.txt

    while [ "$result" == "" ]; do
        # check the build result
        result=`grep -E 'Success|Failed' tmp.txt`

        sleep 5

        check_result
    done

    result=`grep 'Success' tmp.txt`
    
    # clean tmp.txt, tmp.html
    rm -f tmp.txt tmp.html

    if [ "$result" == "" ]; then
        echo -e "\nbuilding failed...pls check the programe"
	exit 1
    fi
}

function send_build_post() {
    # send build command
    curl -X POST ${jenkins_job_url}/${jenkins_name}/build?delay=0sec --user ${user}

    echo 'checking....pls wait moment'
   
    # sleep 5 seconds wait the page refresh
    sleep 5
}

send_build_post
echo -n building.
check_result

调用方法如下:

function deploy_account() {
    name=CE-AUTH
    location=/tmp/fpx-auth.war

    # 此处将需要打包的工程当参数传进去
    ./jenkins.sh ${name}
    
    rm -f ${location}

    # 构建完成之后就可以下载了
    wget -q ${jenkins_job_url}/${name}/ws/auth/target/fpx-auth.war -O ${location}

}

参与评论