-
Notifications
You must be signed in to change notification settings - Fork 271
/
Copy pathupload-resources-to-github
executable file
·118 lines (102 loc) · 3.72 KB
/
upload-resources-to-github
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
#!/bin/bash
set -euo pipefail
# Script to upload release assets to Github.
# This script cleans up after itself in cases of parital failures. i.e. either all assets are uploaded or none
SCRIPTPATH="$( cd "$(dirname "$0")" ; pwd -P )"
VERSION=$(make -s -f $SCRIPTPATH/../Makefile version)
BUILD_DIR=$SCRIPTPATH/../build/k8s-resources/$VERSION
BINARY_DIR=$SCRIPTPATH/../build/bin
BINARIES_ONLY="false"
K8s_ASSETS_ONLY="false"
SUFFIX=""
USAGE=$(cat << 'EOM'
Usage: upload-resources-to-github [-b]
Upload release assets to GitHub. Release assets include binaries for supported platforms and K8s resources for supported versions.
Example: upload-resources-to-github -b
Optional:
-b Upload binaries only [DEFAULT: upload all the assets]
-k Upload K8s assets only
-s SUFFIX String appended to resource file names
EOM
)
# Process our input arguments
while getopts "bks:" opt; do
case ${opt} in
b ) # Binaries only
BINARIES_ONLY="true"
;;
k ) # K8s assets only
K8s_ASSETS_ONLY="true"
;;
s) # Suffix
SUFFIX=$OPTARG
;;
\? )
echo "$USAGE" 1>&2
exit
;;
esac
done
INDV_K8S_RESOURCES=$BUILD_DIR/individual-resources${SUFFIX}.tar
AGG_RESOURCES_YAML=$BUILD_DIR/all-resources${SUFFIX}.yaml
QP_INDV_K8S_RESOURCES=$BUILD_DIR/individual-resources-queue-processor${SUFFIX}.tar
QP_AGG_RESOURCES_YAML=$BUILD_DIR/all-resources-queue-processor${SUFFIX}.yaml
RELEASE_ID=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \
https://api.github.com/repos/aws/aws-node-termination-handler/releases | \
jq --arg VERSION "$VERSION" '.[] | select(.tag_name==$VERSION) | .id')
ASSET_IDS_UPLOADED=()
trap 'handle_errors_and_cleanup $?' EXIT
handle_errors_and_cleanup() {
if [ $1 -eq 0 ]; then
exit 0
fi
if [[ ${#ASSET_IDS_UPLOADED[@]} -ne 0 ]]; then
echo -e "\nCleaning up assets uploaded in the current execution of the script"
for asset_id in "${ASSET_IDS_UPLOADED[@]}"; do
echo "Deleting asset $asset_id"
curl -X DELETE \
-H "Authorization: token $GITHUB_TOKEN" \
"https://api.github.com/repos/aws/aws-node-termination-handler/releases/assets/$asset_id"
done
exit $1
fi
}
gather_assets_to_upload() {
local resources=()
if [ $K8s_ASSETS_ONLY != "true" ]; then
for binary in $BINARY_DIR/*; do
resources+=("$binary")
done
fi
if [ $BINARIES_ONLY != "true" ]; then
resources+=("$INDV_K8S_RESOURCES" "$AGG_RESOURCES_YAML" "$QP_INDV_K8S_RESOURCES" "$QP_AGG_RESOURCES_YAML")
fi
echo "${resources[@]}"
}
# $1: absolute path to asset
upload_asset() {
resp=$(curl --write-out '%{http_code}' --silent \
-H "Authorization: token $GITHUB_TOKEN" \
-H "Content-Type: $(file -b --mime-type $1)" \
--data-binary @$1 \
"https://uploads.github.com/repos/aws/aws-node-termination-handler/releases/$RELEASE_ID/assets?name=$(basename $1)")
response_code=$(echo $resp | sed 's/\(.*\)}//')
response_content=$(echo $resp | sed "s/$response_code//")
# HTTP success code expected - 201 Created
if [[ $response_code -eq 201 ]]; then
asset_id=$(echo $response_content | jq '.id')
ASSET_IDS_UPLOADED+=("$asset_id")
echo "Created asset ID $asset_id successfully"
else
echo -e "❌ Upload failed with response code $response_code and message \n$response_content ❌"
exit 1
fi
}
ASSETS=$(gather_assets_to_upload)
COUNT=1
echo -e "\nUploading release assets for release id '$RELEASE_ID' to Github"
for asset in $ASSETS; do
name=$(echo $asset | tr '/' '\n' | tail -1)
echo -e "\n $((COUNT++)). $name"
upload_asset $asset
done