Skip to content

Instantly share code, notes, and snippets.

@robert-wallis
Last active April 26, 2018 19:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robert-wallis/f76f33e4f1133c5233a852a2633771b8 to your computer and use it in GitHub Desktop.
Save robert-wallis/f76f33e4f1133c5233a852a2633771b8 to your computer and use it in GitHub Desktop.
Publish git tag with build folder gzipped to github releases from the command line.
#!/bin/bash
# Copyright (C) 2018, Robert A. Wallis, All Rights Reserved
#
# make_release.sh expects a `git tag v1.2.3` style tag to have been created for the version `1.2.3` stored in buildnumber.txt
# It then creates a release out of the commit notes from the previous version's tag to the current version.
# And uploads the contents of the build/ folder as v1.2.3.tgz as an asset.
set -e
GITHUB_USER="robert-wallis"
GITHUB_REPO="yourRepoHere"
GITHUB="https://api.github.com"
GITHUB_UPLOADS="https://uploads.github.com"
VERSION="v$(cat buildnumber.txt)"
if [[ "$(which jq)" == "" ]]; then
echo "please install jq; brew install jq"
exit -1
fi
if [[ "$VERSION" == "v" ]]; then
echo "buildnumber.txt empty"
exit -1
fi
LAST_TAGS="$(git tag | sort --version-sort | tail -n 2)"
LAST_TAG="${LAST_TAGS##*$'\n'}"
if [[ "$VERSION" == $LAST_TAG ]]; then
LAST_TAG=(${LAST_TAGS[@]})
fi
LOGS=$(git log --pretty=format:'- %h %B' $LAST_TAG..HEAD | jq -Rj '(. + "\\n")')
BACKUP_FILE="${VERSION}.tgz"
tar -czvf "${BACKUP_FILE}" build/
echo "Github 2FA for ${GITHUB_USER}?"
read OTP
BODY="$(echo $LOGS | tr \" \')"
JSON=$(cat <<-ENDJSON
{
"tag_name": "${VERSION}",
"target_commitish": "",
"name": "${VERSION}",
"body": "${BODY}",
"draft": true,
"prerelease": true
}
ENDJSON
)
echo "Creating Release ${VERSION}"
echo "$JSON" | curl -u ${GITHUB_USER} -X POST -H "X-GitHub-OTP: ${OTP}" -d @- "${GITHUB}/repos/${GITHUB_USER}/${GITHUB_REPO}/releases" > res.json
RELEASE_ID=$(cat res.json | jq -r '.id')
HTML_URL=$(cat res.json | jq -r '.html_url')
rm res.json
MIME_TYPE=$(file -b --mime-type ${BACKUP_FILE})
echo "Uploading Build Archive ${MIME_TYPE} to Release id ${RELEASE_ID}"
curl -u ${GITHUB_USER} -X POST -H "X-GitHub-OTP: ${OTP}" -H "Content-Type: ${MIME_TYPE}" --data-binary "@${BACKUP_FILE}" "${GITHUB_UPLOADS}/repos/${GITHUB_USER}/${GITHUB_REPO}/releases/${RELEASE_ID}/assets?name=${BACKUP_FILE}"
rm ${BACKUP_FILE}
echo "Done making release: ${VERSION} ${HTML_URL}"
open "${HTML_URL}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment