forked from direnv/direnv
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
#!/usr/bin/env bash | ||
# | ||
# Upload binary artifacts when a new release is made. | ||
# | ||
# Usage: ./script/dist-push [<tag>] | ||
# | ||
# Depends on: bash, coreutils, jq, curl | ||
set -euo pipefail | ||
|
||
# Ensure that the GITHUB_TOKEN secret is included | ||
if [[ -z "${GITHUB_TOKEN:-}" ]]; then | ||
echo "Set the GITHUB_TOKEN env variable." | ||
exit 1 | ||
fi | ||
|
||
# Prepare the headers for our curl-command. | ||
AUTH_HEADER="Authorization: token ${GITHUB_TOKEN}" | ||
# Set the github repository in CI | ||
: "${GITHUB_REPOSITORY:=direnv/direnv}" | ||
|
||
# Create the correct Upload URL. | ||
if [[ -n "${1:-}" ]]; then | ||
RELEASE_ID=$(curl -sfL \ | ||
-H "${AUTH_HEADER}" \ | ||
"https://api.github.com/repos/$GITHUB_REPOSITORY/releases/tags/$1" \ | ||
| jq .id) | ||
else | ||
# if not tag is given, assume we are in CI | ||
RELEASE_ID=$(jq --raw-output '.release.id' "$GITHUB_EVENT_PATH") | ||
fi | ||
|
||
# start from the project root | ||
cd "$(dirname "$0")/.." | ||
|
||
# make sure we have all the dist files | ||
make dist | ||
|
||
# For each matching file.. | ||
for file in dist/*; do | ||
echo "Processing '${file}'" | ||
|
||
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. | ||
if ! response=$( | ||
curl \ | ||
-sSL \ | ||
-XPOST \ | ||
-H "${AUTH_HEADER}" \ | ||
--upload-file "${file}" \ | ||
--header "Content-Type:application/octet-stream" \ | ||
--write-out "%{http_code}" \ | ||
--output "$tmp" \ | ||
"${upload_url}" | ||
); 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 | ||
jq . <"$tmp" | ||
rm "$tmp" | ||
done |