-
Notifications
You must be signed in to change notification settings - Fork 1
/
create-release.sh
executable file
·123 lines (107 loc) · 3.75 KB
/
create-release.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
#!/usr/bin/env bash
set -e
[ -z "$DEBUG" ] || set -x;
usage() {
echo "$0 <repo> <tag> [<release name>] [-- <asset>...]" >&2;
}
if [ "$1" = "-h" -o "$1" = "--help" ]; then
usage
cat >&2 <<EOS
Pass the following arguments:
* \`<repo>\`: ":user/:name" of the repository. For example, "foca/mpp".
* \`<tag>\`: Name of the tag for this release. For example, "v1.0.0".
* \`<auth>\`: github auth token.
* \`<release name>\`: Optional suffix for the release name.
You can pass a list of files to upload as release assets by giving them after a
\`--\` argument.
If you supply text on \`STDIN\` it will be used as the release notes.
EXAMPLES:
$ $0 foca/mpp v1.0.0 -- pkg/*.tar.gz
Creates a release named "mpp v1.0.0" and adds any tar.gz file in
\`./pkg\` as an asset.
$ $0 foca/mpp v1.0.1 "Bugfixes" -- pkg/*.tar.gz < notes.md
Creates a release named "mpp v1.0.1: Bugfixes", adds any tar.gz
file in \`./pkg\` as an asset, and uses the contents of \`notes.md\`
as the release notes.
NOTE:
This uses your \`.netrc\` file to authenticate with GitHub. In order to run the
script, make sure you have **both** \`api.github.com\` and \`upload.github.com\` in
this file. For example:
machine api.github.com
login foca
password <an access token>
machine upload.github.com
login foca
password <an access token>
Generate this access token at https://github.com/settings/tokens and make sure
it has access to the \`"repo"\` scope.
EOS
exit 1;
fi
[ -n "$3" ] || (usage; exit 1);
REPO="$1"
shift
TAG="$1"
shift
AUTH="$1"
shift
NAME="$(basename "$REPO") ${TAG}"
if [ -n "$1" -a "$1" != "--" ]; then
NAME="${NAME}: $1";
shift
fi
BODY=""
[ -t 0 ] || BODY=`cat`;
if [ "$1" = "--" -a "$#" -ge "2" ]; then
shift
ASSETS="$@"
fi
# Validate token.
curl -o /dev/null -sH "Authorization: token $AUTH" https://api.github.com/repos/${REPO}/ || { echo "Error: Invalid repo, token or network issue!"; exit 1; }
payload=$(
jq --null-input \
--arg tag "$TAG" \
--arg name "$NAME" \
--arg body "$BODY" \
'{ tag_name: $tag, name: $name, body: $body, draft: false }'
)
response=$(
curl --fail \
--netrc \
--silent \
--location \
--header "Authorization: token $AUTH" \
--data "$payload" \
"https://api.github.com/repos/${REPO}/releases"
)
upload_url="$(echo "$response" | jq -r .upload_url | sed -e "s/{?name,label}//")"
for file in $ASSETS; do
curl --netrc \
--header "Authorization: token $AUTH" \
--header "Content-Type:application/gzip" \
--data-binary "@$file" \
"$upload_url?name=$(basename "$file")"
done
# Sourced from here https://gist.github.com/foca/38d82e93e32610f5241709f8d5720156
# Copyright (c) 2016 Nicolas Sanguinetti <hi@nicolassanguinetti.info>
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.