-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgithub-add-asset
executable file
·106 lines (93 loc) · 2.37 KB
/
github-add-asset
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
#!/usr/bin/env bash
set -euo pipefail
YP_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." >/dev/null && pwd)"
source ${YP_DIR}/sh/common.inc.sh
#- github-add-asset 1.0
## Usage: github-add-asset [OPTION]
## Upload a github release asset (aka binary).
##
## --repo-slug The github repository slug: account/repo
## --tag The release tag
## --asset The path to the asset
## --token The OAuth Token
##
## -h, --help Display this help and exit
## -v, --version Output version information and exit
TMP_CURL=$(mktemp -t yplatform.XXXXXXXXXX)
function on_exit() {
rm -rf ${TMP_CURL}
}
trap on_exit EXIT
REPO_SLUG=
TAG=
ASSET=
GH_TOKEN=
if { getopt --test >/dev/null 2>&1 && false; } || [[ "$?" = "4" ]] || false; then
ARGS=$(getopt -o hv -l help,version,asset:,repo-slug:,tag:,token: -n $(basename ${BASH_SOURCE[0]}) -- "$@") || sh_script_usage # editorconfig-checker-disable-line
eval set -- "${ARGS}"
fi
while [[ $# -gt 0 ]]; do
case "$1" in
--asset)
ASSET=$2
shift 2
;;
--repo-slug)
REPO_SLUG=$2
shift 2
;;
--tag)
TAG=$2
shift 2
;;
--token)
GH_TOKEN=$2
shift 2
;;
-h|--help)
sh_script_usage
;;
-v|--version)
sh_script_version
;;
--)
shift
break
;;
-*)
sh_script_usage
;;
*)
break
;;
esac
done
[[ $# -eq 0 ]] || sh_script_usage
[[ -n ${REPO_SLUG} ]] || {
echo_err "Please provide a --repo-slug."
exit 1
}
[[ -n ${TAG} ]] || {
echo_err "Please provide a --tag."
exit 1
}
[[ -n ${ASSET} ]] || {
echo_err "Please provide a --asset."
exit 1
}
RELEASE_URL="https://api.github.com/repos/${REPO_SLUG}/releases/tags/${TAG}"
curl -qfsSL \
-H "authorization: token ${GH_TOKEN}" \
-o ${TMP_CURL} \
"${RELEASE_URL}"
echo
UPLOAD_ASSETS_URL=$(cat ${TMP_CURL} | jq -r ".upload_url" | sed "s/[{][?].*//")
UPLOAD_ASSET_URL="${UPLOAD_ASSETS_URL}?name=$(basename ${ASSET})"
echo_info "Uploading ${ASSET}..."
curl -qfsSL \
-X POST \
-H "authorization: token ${GH_TOKEN}" \
-H "content-type: application/octet-stream" \
--data-binary @${ASSET} \
"${UPLOAD_ASSET_URL}"
echo