-
Notifications
You must be signed in to change notification settings - Fork 1
/
github.sh
executable file
·77 lines (67 loc) · 2.33 KB
/
github.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
#!/bin/bash -e
#
# Utility to generate a Github release and upload asset files from a local directory.
#
# Author: Enrique Garcia <[email protected]>
#
# Env parameters:
#
# * GITHUB_TOKEN (required)
# * GITHUB_OWNER (required)
# * GITHUB_REPO (required)
# * RELEASE_TAG (required)
# * RELEASE_NAME (optional, default is empty)
# * RELEASE_TARGET (optional, default is master)
# * RELEASE_BODY (optional, default empty)
# * RELEASE_DRAFT (optional, default isn't a draft)
# * RELEASE_PRERELEASE (optional, default isn't a prerelease)
# * RELEASE_DIR (optional, if not present assets won't be uploaded)
# * DEBUG (optional, show trace output if is 'true')
#
# Example:
#
# GITHUB_TOKEN=4321 RELEASE_TAG=v0.1.6 RELEASE_DIR=$(pwd)/dist ./github.sh gh-release
#
# WARN: This script requires curl and jq
#
gh-release(){
# Required vars
if [[ -z $GITHUB_TOKEN || -z $GITHUB_OWNER || $GITHUB_REPO || $RELEASE_TAG ]]; then
echo 'GITHUB_TOKEN, GITHUB_OWNER, GITHUB_REPO and RELEASE_TAG env variables are required.' && exit 1;
fi
# Optional vars
RELEASE_TARGET=${RELEASE_TARGET:-master}
RELEASE_NAME=${RELEASE_NAME:-''}
RELEASE_BODY=${RELEASE_BODY:-''}
RELEASE_DRAFT=${RELEASE_DRAFT:-false}
RELEASE_PRERELEASE=${RELEASE_PRERELEASE:-false}
RELEASE_DIR=${RELEASE_DIR:-''}
GITHUB_AUTH_TOKEN="Authorization: token ${GITHUB_TOKEN}"
RELEASE_DATA=$(cat << EOF
{
"name": "${RELEASE_NAME}",
"tag_name": "${RELEASE_TAG}",
"target_commitish": "${RELEASE_TARGET}",
"body": "${RELEASE_BODY}",
"draft": ${RELEASE_DRAFT},
"prerelease": ${RELEASE_PRERELEASE}
}
EOF
)
RELEASE_RES=`curl -X POST \
-H "${GITHUB_AUTH_TOKEN}" \
-H "Content-Type: application/json" \
-d "${RELEASE_DATA}" \
https://api.github.com/repos/$GITHUB_OWNER/$GITHUB_REPO/releases`
RELEASE_ID=$(echo $RELEASE_RES | jq '.id')
[[ "$RELEASE_ID" && "$RELEASE_ID" != 'null' ]] || (echo "Invalid release id " && exit 1)
if [[ "$RELEASE_DIR" && -d $RELEASE_DIR ]]; then
for FILE in $RELEASE_DIR/*; do
ASSET_URL="https://uploads.github.com/repos/${GITHUB_OWNER}/${GITHUB_REPO}/releases/${RELEASE_ID}/assets?name=$(basename $FILE)"
echo "Uploading asset: ${FILE}"
curl -X PUT -H "$GITHUB_AUTH_TOKEN" --data-binary @"$FILE" -H "Content-Type: application/octet-stream" $ASSET_URL
done
else
echo "Not found directory ${RELEASE_DIR}" && exit 1
fi
}