This repository has been archived by the owner on Dec 22, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathupload-script
executable file
·174 lines (140 loc) · 3.74 KB
/
upload-script
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
#!/bin/bash
#
# Upload binary artifacts when a new release is made.
#
# We should be invoked with a pattern for files to upload,
# and optionally the release-ID to associate them with.
#
# These will be stored in the environment as:
#
# INPUT_ARGS
#
# INPUT_RELEASEID
#
#
#
# Ensure that the GITHUB_TOKEN secret is included
#
if [[ -z "$GITHUB_TOKEN" ]]; then
echo "Set the GITHUB_TOKEN env variable."
exit 1
fi
#
# Ensure that there is a pattern specified.
#
if [[ -z "$INPUT_ARGS" ]]; then
echo "Missing file (pattern) to upload."
exit 1
fi
#
# Sanity-check, show our arguments.
#
echo "Arguments : ${INPUT_ARGS}"
echo "Release ID: ${INPUT_RELEASEID}"
#
# In the past we invoked a build-script to generate the artifacts
# prior to uploading.
#
# Now we no longer do so, they must exist before they are uploaded.
#
# Test for them here.
#
# Have we found any artifacts?
found=
for file in $INPUT_ARGS; do
if [ -e "$file" ]; then
found=1
fi
done
#
# Abort if no matching files were found.
#
if [ -z "${found}" ]; then
echo "*****************************************************************"
echo " "
echo " Artifacts are missing, and this action no longer invokes the "
echo " legacy-build script."
echo " "
echo " Please see the README.md file for github-action-publish-binaries"
echo " which demonstrates how to build AND upload artifacts."
echo " "
echo "*****************************************************************"
exit 1
fi
#
# Prepare the headers for our curl-command.
#
AUTH_HEADER="Authorization: token ${GITHUB_TOKEN}"
#
# Get the release ID
#
if [ -z "${INPUT_RELEASEID}" ]; then
#
# Not specified? Use the one from the payload
#
RELEASE_ID=$(jq --raw-output '.release.id' "$GITHUB_EVENT_PATH")
echo "Discovered RELEASE_ID: ${RELEASE_ID}"
else
#
# Otherwise use the supplied value.
#
RELEASE_ID="${INPUT_RELEASEID}"
echo "Using supplied RELEASE_ID: ${RELEASE_ID}"
fi
#
# For each matching file..
#
for file in $INPUT_ARGS; do
echo "Processing file ${file}"
if [ ! -e "$file" ]; then
echo "***************************"
echo " file not found - skipping."
echo "***************************"
continue
fi
if [ ! -s "$file" ]; then
echo "**************************"
echo " file is empty - skipping."
echo "**************************"
continue
fi
FILENAME=$(basename "${file}")
UPLOAD_URL="https://uploads.github.com/repos/${GITHUB_REPOSITORY}/releases/${RELEASE_ID}/assets?name=${FILENAME}"
echo "Upload URL is ${UPLOAD_URL}"
# Generate a temporary file.
tmp=$(mktemp)
# Upload the artifact - capturing HTTP response-code in our output file.
response=$(curl \
-sSL \
-XPOST \
-H "${AUTH_HEADER}" \
--upload-file "${file}" \
--header "Content-Type:application/octet-stream" \
--write-out "%{http_code}" \
--output $tmp \
"${UPLOAD_URL}")
# If the curl-command returned a non-zero response we must abort
if [ "$?" -ne 0 ]; then
echo "**********************************"
echo " curl command did not return zero."
echo " Aborting"
echo "**********************************"
cat $tmp
rm $tmp
exit 1
fi
# If upload is not successful, we must abort
if [ $response -ge 400 ]; then
echo "***************************"
echo " upload was not successful."
echo " Aborting"
echo " HTTP status is $response"
echo "**********************************"
cat $tmp
rm $tmp
exit 1
fi
# Show pretty output, since we already have jq
cat $tmp | jq .
rm $tmp
done