jenkins编译服务脚本
#!/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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
上次更新: 2023/01/31, 21:32:57