This repository has been archived by the owner on Mar 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
publish-github-release-assets-helper.sh
193 lines (171 loc) · 6.92 KB
/
publish-github-release-assets-helper.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#!/bin/bash
####################################################################################
# MIT License
# Copyright (c) 2017 Bernhard Grünewaldt
# See https://github.com/codeclou/publish-github-release-assets-helper/blob/master/LICENSE
####################################################################################
VERSION=1.0.1
set -e
####################################################################################
#
# COLORS
#
####################################################################################
export CLICOLOR=1
C_RED='\x1B[31m'
C_CYN='\x1B[96m'
C_GRN='\x1B[32m'
C_MGN='\x1B[35m'
C_RST='\x1B[39m'
####################################################################################
#
# FUNCTIONS
#
####################################################################################
# Used to be able to use pass-by-reference in bash
#
#
return_by_reference() {
if unset -v "$1"; then
eval $1=\"\$2\"
fi
}
# USAGE:
# upload_asset_to_github_release \
# codeclou \
# foo \
# 1.0 \
# 123 \
# build-results/ \
# swagger.json \
# "application/json"
#
# @param $1 {string} repository owner
# @param $2 {string} repository name
# @param $3 {release_name} release/tag name
# @param $4 {int} github release id
# @param $5 {string} filepath
# @param $6 {string} filename
# @param $7 {string} mime type
function upload_asset_to_github_release {
local owner=$1
local repo=$2
local release_name=$3
local release_id=$4
local filepath=$5
local filename=$6
local mime_type=$7
#
# LIST ASSETS
#
local assets=$(curl -s \
--fail \
-H "Accept: application/json" \
-H "Content-Type: application/json" \
-H "Authorization: token ${GITHUB_AUTH_TOKEN}" \
"https://api.github.com/repos/${owner}/${repo}/releases/${release_id}/assets" \
| jq ".")
#
# CHECK IF ASSET EXISTS
#
local asset_id=$(echo $assets | jq -r ".[] | select(.name == \"$filename\") | .id")
#
# DELETE ASSET IF EXISTS
#
if [ "$asset_id" != "" ]
then
local delete_existing_asset_response=$(curl \
-s -o /dev/null \
-w "%{http_code}" \
-H "Content-Type: ${mime_type}" \
-H "Authorization: token ${GITHUB_AUTH_TOKEN}" \
-X DELETE \
"https://api.github.com/repos/${owner}/${repo}/releases/assets/${asset_id}")
if [[ "$delete_existing_asset_response" != "204" ]]
then
echo -e $C_CYN">> publish asset ........:${C_RST}${C_RED} DELETING EXISTING${C_RST} ${filename} of release ${release_name} failed with http ${delete_existing_asset_response}."
exit 1
else
echo -e $C_CYN">> publish asset ........:${C_RST}${C_GRN} DELETING EXISTING${C_RST} ${filename} of release ${release_name} succeeded with http ${delete_existing_asset_response}."
fi
fi
#
# UPLOAD ASSET
#
local upload_response_status=$(curl \
-s -o /dev/null \
-w "%{http_code}" \
-H "Content-Type: ${mime_type}" \
-H "Authorization: token ${GITHUB_AUTH_TOKEN}" \
-X POST \
--data-binary "@${filepath}${filename}" \
"https://uploads.github.com/repos/${owner}/${repo}/releases/${release_id}/assets?name=${filename}")
if [[ "$upload_response_status" != "201" ]]
then
echo -e $C_CYN">> publish asset ........:${C_RST}${C_RED} UPLOADING ${C_RST} ${filename} of release ${release_name} failed with http ${upload_response_status}."
exit 1
else
echo -e $C_CYN">> publish asset ........:${C_RST}${C_GRN} UPLOADING ${C_RST} ${filename} of release ${release_name} succeeded with http ${upload_response_status}."
fi
echo ""
}
# NOTE:
# - release_name must match an existing Git Tag!
# - if a release already exists, it just returns the release_id
#
# USAGE:
# release_id=-1
# create_github_release_and_get_release_id "codeclou" "foo" "1.0" "master" release_id
# echo $release_id
#
# @param $1 {string} repository owner
# @param $2 {string} repository name
# @param $3 {release_name} release/tag name
# @param $4 {target_commitish} 'master' from which branch your tag should be created off
# @param $5 {int} return value passByReference release id
function create_github_release_and_get_release_id {
local owner=$1
local repo=$2
local release_name=$3
local target_commitish=$4
echo ""
echo -e $C_MGN' __ ___ __ _ __ __ __ '$C_RST
echo -e $C_MGN' ___ __ __/ / / (_)__ / / ___ _(_) /_/ / __ __/ / '$C_RST
echo -e $C_MGN' / _ \/ // / _ \/ / (_-</ _ \ / _ `/ / __/ _ \/ // / _ \ '$C_RST
echo -e $C_MGN' / .__/\_,_/_.__/_/_/___/_//_/ \_, /_/\__/_//_/\_,_/_.__/ '$C_RST
echo -e $C_MGN'/_/ __ /___/ __ __ __ '$C_RST
echo -e $C_MGN' _______ / /__ ___ ____ ___ ___ ____ ___ ___ / /____ / / ___ / /__ ___ ____'$C_RST
echo -e $C_MGN' / __/ -_) / -_) _ `(_-</ -_) / _ `(_-<(_-</ -_) __(_-< / _ \/ -_) / _ \/ -_) __/'$C_RST
echo -e $C_MGN'/_/ \__/_/\__/\_,_/___/\__/ \_,_/___/___/\__/\__/___/ /_//_/\__/_/ .__/\__/_/ '$C_RST
echo -e $C_MGN' /_/ '$C_RST
echo ""
echo -e $C_MGN' Publish GitHub Release Assets with ease'$C_RST
echo -e $C_MGN" v${VERSION} - https://github.com/codeclou/publish-github-release-assets-helper"$C_RST
echo -e $C_MGN' ------'$C_RST
echo ""
local release_id=$(curl -s \
--fail \
-H "Accept: application/json" \
-H "Content-Type: application/json" \
-H "Authorization: token ${GITHUB_AUTH_TOKEN}" \
"https://api.github.com/repos/${owner}/${repo}/releases/tags/${release_name}" \
| jq -r ".id")
if (( release_id > 0 ))
then
echo -e $C_CYN">> create release .......:${C_RST}${C_MGN} SKIPPING ${C_RST} creation of release ${release_name} skipped since it does already exist."
else
# https://developer.github.com/v3/repos/releases/#create-a-release
release_id=$(curl -s \
--fail \
-H "Accept: application/json" \
-H "Content-Type: application/json" \
-H "Authorization: token ${GITHUB_AUTH_TOKEN}" \
-X POST \
-d "{ \"tag_name\": \"$release_name\", \"target_commitish\": \"$target_commitish\", \"name\": \"$release_name\", \"body\": \"$release_name\" }" \
"https://api.github.com/repos/${owner}/${repo}/releases" \
| jq -r ".id")
echo -e $C_CYN">> create release .......:${C_RST}${C_GRN} CREATED ${C_RST} creation of release ${release_name} succeeded with id ${release_id}."
fi
echo ""
local "$5" && return_by_reference $5 $release_id
}