jenkins编译服务脚本

2023-05-19 17:45:09 浏览数 (1)

代码语言:javascript复制
#!/bin/bash

project_code=$product_code
build_path=/var/jenkins_home/workspace/$JOB_NAME
time=`date  '%Y-%m-%dT%H:%M:%SZ'`
git_tree_state="clean"
build_tag=""

function run_build(){
    name=${JOB_BASE_NAME##*.}
    if [[ $name = "service_cpp_quote" ]] || [[ $name = "service_new_trade" ]]  || [[ $name = "service_new_trade_gg" ]];then
        # C  需要先编译.
        mapping_path=/workspace
        docker_images=xxx/cpp/dev_base_tools:1.1.1
        build_command="cd /workspace && rm -rf build && mkdir build && cd build && cmake .. && make -j4 install"
        sudo docker run -v /opt/apps/jenkins/jenkins_home/workspace/$JOB_BASE_NAME:$mapping_path --rm $docker_images sh -c "$build_command"
        cd $build_path/build/dist
    elif [[ $JOB_BASE_NAME = "AIRM.db" ]]; then
        time=`date " %Y%m%d%H%M%S"`
        cd $build_path
        sudo mkdir -p $JOB_BASE_NAME
        versionlib_path=/data/Vsersion_Lib/$project_code
		sudo mkdir -p $versionlib_path
        sudo cp -rf `ls |grep $"sql"` $JOB_BASE_NAME/
        sudo tar -zcf  "$JOB_BASE_NAME"_"$time"_"$COMMIT_ID".tar.gz $JOB_BASE_NAME/
        sudo cp "$JOB_BASE_NAME"_"$time"_"$COMMIT_ID".tar.gz $versionlib_path/
        sudo rm -f "$JOB_BASE_NAME"_"$time"_"$COMMIT_ID".tar.gz
        sudo rm -rf $JOB_BASE_NAME/
        echo "$JOB_BASE_NAME"_"$time"_"$COMMIT_ID".tar.gz

        return 0
    elif [[ $JOB_BASE_NAME = "AIRM.gateway" ]]; then
        build_tag="--build-arg VERSION=$version_number --build-arg GIT_COMMIT=$COMMIT_ID --build-arg GIT_TREE_STATE=$git_tree_state --build-arg BUILD_DATE=$time"
    elif [[ $JOB_BASE_NAME = "AIRM.service_go_trade" ]]; then
        build_tag="--build-arg VERSION=$version_number --build-arg GIT_COMMIT=$COMMIT_ID --build-arg GIT_TREE_STATE=$git_tree_state --build-arg BUILD_DATE=$time"
        docker_names=("new_gateway" "query" "trade")
        for docker_name in ${docker_names[@]}
        do
            cp script/docker/$docker_name/Dockerfile .
            sudo docker tag xxx/$project_code/$docker_name:$version_number xxx/$project_code/$docker_name:$version_number.$BUILD_NUMBER
            sudo docker rmi xxx/$project_code/$docker_name:$version_number
            sudo docker build -t xxx/$project_code/$docker_name:$version_number $build_tag .
            rm -f Dockerfile
            sudo docker push xxx/$project_code/$docker_name:$version_number
        done

        return 0
    else
        cd $build_path
    fi

    # 打包.
    docker_name=${name}
    sudo docker tag xxx/$project_code/$docker_name:$version_number xxx/$project_code/$docker_name:$version_number.$BUILD_NUMBER
    sudo docker rmi xxx/$project_code/$docker_name:$version_number
    sudo docker build -t xxx/$project_code/$docker_name:$version_number $build_tag .
    sudo docker push xxx/$project_code/$docker_name:$version_number
}

function checkBuild(){
    product=$1
    version=$2
    if [[ -z $product ]] || [[ -z $version ]];then
        # 参数为空.
        return 1
    fi
    info_path=http://172.16.30.217:9110/version/status
    value=`curl -XGET "$info_path?product_code=$product_code&version_number=$version_number" |awk -F'lock_status":' '{print $2}'|awk -F, '{print $1}'`
    if [[ $value -eq 0 ]];then
        # 可以正常构建.
        return 0
    else
        # 版本已锁定.
        return 3
    fi
}

res1=$(checkBuild $product_code $version_number)
res2=`echo $?`
if [[ $res2 -eq 1 ]];then
    echo "版本号没有输入"
    exit $res2
elif [[ $res2 -eq 3 ]];then
    echo "对应的版本已锁定"
    exit $res2
else
    run_build
fi

0 人点赞