-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbuild.sh
executable file
·127 lines (110 loc) · 2.98 KB
/
build.sh
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/bin/bash -xi
#GITHUB_WORKSPACE=
OS=${INPUT_OS-''}
ARCH=${INPUT_ARCH-''}
RELEASE_TAG=$(basename "${GITHUB_REF:-'master'}")
export VERSION=${RELEASE_TAG:-"master"}
function add_pkg() {
pkg=$1
command -v apk && apk add ${pkg} && return 0
command -v yum && yum makecache fast && yum install -y ${pkg} && return 0
command -v apt && apt-get update && apt-get install -y ${pkg} && return 0
}
if [[ $(uname) != 'Darwin' ]];then
command -v bash || add_pkg bash
command -v curl || add_pkg curl
command -v jq || add_pkg jq
command -v git || add_pkg git
command -v tar || add_pkg tar
command -v unzip || add_pkg unzip
fi
#INPUT_UPLOAD_URL='https://uploads.github.com/repos/ibuler/koko/releases/27862783/assets'
if [[ -n "${INPUT_UPLOAD_URL=''}" ]];then
RELEASE_ASSETS_UPLOAD_URL=${INPUT_UPLOAD_URL}
else
RELEASE_ASSETS_UPLOAD_URL=$(jq -r .release.upload_url < "${GITHUB_EVENT_PATH}")
fi
RELEASE_ASSETS_UPLOAD_URL=${RELEASE_ASSETS_UPLOAD_URL%\{?name,label\}}
#INPUT_GITHUB_TOKEN=
function get_md5() {
file=$1
if [[ "$(uname)" == "Darwin" ]];then
echo $(md5 ${file} | awk '{ print $NF }')
else
echo $(md5sum ${file} | cut -d ' ' -f 1)
fi
}
# First to build it
workspace=${GITHUB_WORKSPACE}
build_dir=''
git config --global --add safe.directory /github/workspace
git config --global --add safe.directory ${workspace}
if [[ -f ${workspace}/build.sh ]];then
build_dir=${workspace}
fi
if [[ -z ${build_dir} && -f ${workspace}/utils/build.sh ]];then
build_dir=${workspace}/utils
fi
if [[ -z ${build_dir} ]];then
echo "No build script found at: PROJECT/build.sh"
exit 10
fi
cd ${build_dir} && bash -xieu build.sh || exit 3
# 准备打包
cd ${workspace}/release || exit 5
for i in *;do
if [[ ! -d $i ]];then
continue
fi
if [[ "${OS}" && "${ARCH}" ]];then
tar_dirname=$i-${VERSION}-${OS}-${ARCH}
else
tar_dirname=$i-${VERSION}
fi
mv ${i} ${tar_dirname}
tar_filename=${tar_dirname}.tar.gz
tar czvf ${tar_filename} ${tar_dirname}
md5sum=$(get_md5 ${tar_filename})
echo ${md5sum} > ${tar_filename}.md5 && rm -rf ${tar_dirname}
done
function upload_zip() {
file=$1
curl \
--fail \
-X POST \
--data-binary @${file} \
-H 'Content-Type: application/gzip' \
-H "Authorization: Bearer ${GITHUB_TOKEN}" \
"${RELEASE_ASSETS_UPLOAD_URL}?name=${file}"
return $?
}
function upload_octet() {
file=$1
curl \
--fail \
-X POST \
--data @${file} \
-H 'Content-Type: application/octet-stream' \
-H "Authorization: Bearer ${GITHUB_TOKEN}" \
"${RELEASE_ASSETS_UPLOAD_URL}?name=${file}"
return $?
}
if [[ -n "${ASSETS_UPLOAD_DISABLED-}" ]];then
echo "禁用了上传,pass"
exit 0
fi
# 打包完上传
for i in *;do
# 因为可能是md5已被上传了
if [[ ! -f $i || "$i" == *.md5 ]];then
continue
fi
if [[ $i == *.tar.gz ]];then
upload_zip $i || exit 3
if [[ -f $i.md5 ]];then
upload_octet $i.md5 || exit 4
fi
else
upload_octet $i || echo 'Ignore file upload error';
fi
done