2018-09-24 21:42:16 +02:00
|
|
|
#!/bin/bash
|
|
|
|
|
2019-09-09 17:15:10 +02:00
|
|
|
if [[ -n "$TOKEN" ]]; then
|
2018-09-24 22:47:40 +02:00
|
|
|
GITHUB_TOKEN=$TOKEN
|
|
|
|
fi
|
2018-09-24 22:33:52 +02:00
|
|
|
|
|
|
|
if [[ -z "$GITHUB_TOKEN" ]]; then
|
|
|
|
echo "Set the GITHUB_TOKEN env variable."
|
2018-09-24 21:46:18 +02:00
|
|
|
exit 1
|
2018-09-24 21:42:16 +02:00
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ -z "$GITHUB_REPOSITORY" ]]; then
|
2018-09-24 21:46:18 +02:00
|
|
|
echo "Set the GITHUB_REPOSITORY env variable."
|
|
|
|
exit 1
|
2018-09-24 21:42:16 +02:00
|
|
|
fi
|
|
|
|
|
2018-09-25 03:20:06 +02:00
|
|
|
if [[ -z "$GITHUB_REF" ]]; then
|
|
|
|
echo "Set the GITHUB_REF env variable."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2018-09-24 21:42:16 +02:00
|
|
|
URI=https://api.github.com
|
|
|
|
API_VERSION=v3
|
|
|
|
API_HEADER="Accept: application/vnd.github.${API_VERSION}+json"
|
2018-09-24 22:33:52 +02:00
|
|
|
AUTH_HEADER="Authorization: token ${GITHUB_TOKEN}"
|
2018-09-24 21:42:16 +02:00
|
|
|
|
2018-09-24 21:46:18 +02:00
|
|
|
main(){
|
|
|
|
local files
|
2018-09-24 21:42:16 +02:00
|
|
|
|
2018-09-24 21:46:18 +02:00
|
|
|
if [ "$#" -eq 0 ]; then
|
|
|
|
echo "Must pass files to be uploaded..."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
files=( "$@" )
|
2018-09-24 21:42:16 +02:00
|
|
|
|
2018-09-24 21:46:18 +02:00
|
|
|
# Validate the GitHub token.
|
|
|
|
curl -o /dev/null -sSL -H "${AUTH_HEADER}" -H "${API_HEADER}" "${URI}/repos/${GITHUB_REPOSITORY}" || { echo "Error: Invalid repo, token or network issue!"; exit 1; }
|
2018-09-24 21:42:16 +02:00
|
|
|
|
2018-09-25 03:20:06 +02:00
|
|
|
echo "Github ref: ${GITHUB_REF}"
|
|
|
|
|
2018-09-25 00:22:14 +02:00
|
|
|
# Get the tags.
|
|
|
|
tag_response=$(curl -sSL -H "${AUTH_HEADER}" -H "${API_HEADER}" "${URI}/repos/${GITHUB_REPOSITORY}/tags")
|
2018-09-24 21:46:18 +02:00
|
|
|
|
2018-09-25 01:37:37 +02:00
|
|
|
tag_name=$(echo "$tag_response" | jq -e --raw-output .[0].name)
|
2018-09-24 21:46:18 +02:00
|
|
|
|
2018-09-25 00:26:45 +02:00
|
|
|
# Get the latest release.
|
|
|
|
latest_response=$(curl -sSL -H "${AUTH_HEADER}" -H "${API_HEADER}" "${URI}/repos/${GITHUB_REPOSITORY}/releases/latest")
|
|
|
|
latest_release=$(echo "$latest_response" | jq -e --raw-output .tag_name)
|
|
|
|
release_id=$(echo "$latest_response" | jq -e --raw-output .id)
|
|
|
|
|
2018-09-25 03:52:03 +02:00
|
|
|
if [[ "$tag_name" != "$latest_release" ]] || [[ "$release_id" == "null" ]]; then
|
2018-09-25 00:26:45 +02:00
|
|
|
# Create the release.
|
|
|
|
echo "Creating release for tag name: ${tag_name}"
|
2018-09-25 03:52:03 +02:00
|
|
|
response=$(curl -XPOST -sSL -H "${AUTH_HEADER}" -H "${API_HEADER}" "${URI}/repos/${GITHUB_REPOSITORY}/releases" --data '{"tag_name": "'"${tag_name}"'","name":"'"${tag_name}"'","draft":false,"prerelease":false}')
|
2018-09-25 01:37:37 +02:00
|
|
|
release_id=$(echo "$response" | jq -e --raw-output .id)
|
2018-09-25 00:26:45 +02:00
|
|
|
fi
|
|
|
|
|
2018-09-25 03:52:03 +02:00
|
|
|
if [[ "$release_id" == "null" ]]; then
|
|
|
|
echo "Release ID cannot be null."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2018-09-25 00:22:14 +02:00
|
|
|
|
|
|
|
# Upload the files.
|
2018-09-25 01:37:37 +02:00
|
|
|
echo "Uploading files: ${files[*]}"
|
2018-09-25 03:05:37 +02:00
|
|
|
echo "For tag name: ${tag_name}"
|
2018-09-24 21:50:03 +02:00
|
|
|
|
2018-09-25 01:53:37 +02:00
|
|
|
# shellcheck disable=SC2068
|
|
|
|
for file in ${files[@]}; do
|
2018-09-24 21:50:03 +02:00
|
|
|
filename=$(basename "$file")
|
2018-09-25 03:01:40 +02:00
|
|
|
rp=$(realpath "$file")
|
|
|
|
|
2018-09-25 03:12:18 +02:00
|
|
|
curl -sSL -H "${AUTH_HEADER}" \
|
2018-09-25 03:01:40 +02:00
|
|
|
--data-binary @"$rp" \
|
2018-09-24 21:50:03 +02:00
|
|
|
-H "Content-Type: application/octet-stream" \
|
2018-09-25 02:46:45 +02:00
|
|
|
"https://uploads.github.com/repos/${GITHUB_REPOSITORY}/releases/${release_id}/assets?name=${filename}&access_token=${GITHUB_TOKEN}"
|
2018-09-24 21:50:03 +02:00
|
|
|
|
2018-09-25 03:10:54 +02:00
|
|
|
echo "Successfully uploaded: ${rp}"
|
2018-09-24 21:50:03 +02:00
|
|
|
done
|
|
|
|
|
|
|
|
echo "Uploading assets to ${tag_name} complete!"
|
2018-09-24 21:46:18 +02:00
|
|
|
}
|
|
|
|
|
2018-09-25 01:37:37 +02:00
|
|
|
main "$@"
|