Pico/_build/tools/github-deploy.sh

74 lines
2.5 KiB
Bash
Raw Normal View History

#!/usr/bin/env bash
2016-01-07 01:59:48 +00:00
##
2016-01-14 11:49:46 +00:00
# Pushes commits to a GitHub repo
2016-01-07 01:59:48 +00:00
#
# @author Daniel Rudolf
# @link http://picocms.org
# @license http://opensource.org/licenses/MIT
#
2015-11-25 23:29:14 +00:00
set -e
2015-11-23 20:46:36 +00:00
# parameters
2016-01-14 11:49:46 +00:00
CHECK_REPO_SLUG="$1" # optional GitHub repo (e.g. picocms/Pico) to check
# its latest commit as basic race condition protection
2016-01-14 11:49:46 +00:00
CHECK_REMOTE_REF="$2" # optional remote Git reference (e.g. heads/master)
CHECK_LOCAL_COMMIT="$3" # optional local commit SHA1
2015-11-25 20:36:56 +00:00
# environment variables
# GITHUB_OAUTH_TOKEN GitHub authentication token, see https://github.com/settings/tokens
2015-11-25 20:55:28 +00:00
# print parameters
2016-01-14 11:49:46 +00:00
echo "Deploying repo..."
printf 'CHECK_REPO_SLUG="%s"\n' "$CHECK_REPO_SLUG"
printf 'CHECK_REMOTE_REF="%s"\n' "$CHECK_REMOTE_REF"
printf 'CHECK_LOCAL_COMMIT="%s"\n' "$CHECK_LOCAL_COMMIT"
2015-11-25 21:04:16 +00:00
echo
# check for changes
2016-01-14 11:49:46 +00:00
if [ -z "$(git log --oneline '@{upstream}..')" ]; then
printf 'Nothing to deploy; skipping...\n\n'
exit 0
fi
2015-11-23 20:46:36 +00:00
# race condition protection for concurrent Travis builds
# this is no definite protection (race conditions are still possible during `git push`),
# but it should give a basic protection without disabling concurrent builds completely
if [ -n "$CHECK_REPO_SLUG" ] && [ -n "$CHECK_REMOTE_REF" ] && [ -n "$CHECK_LOCAL_COMMIT" ]; then
# retrieve information using GitHub APIv3
echo "Checking latest commit$([ -n "$GITHUB_OAUTH_TOKEN" ] && echo " (authorized)")..."
CHECK_API_URL="https://api.github.com/repos/$CHECK_REPO_SLUG/git/refs/$CHECK_REMOTE_REF"
if [ -n "$GITHUB_OAUTH_TOKEN" ]; then
CHECK_API_RESPONSE="$(curl --fail --silent --show-error --header "Authorization: token $GITHUB_OAUTH_TOKEN" "$CHECK_API_URL")"
else
CHECK_API_RESPONSE="$(curl --fail --silent --show-error "$CHECK_API_URL")"
fi
# evaluate JSON response
CHECK_REMOTE_COMMIT="$(echo "$CHECK_API_RESPONSE" | php -r "
\$json = json_decode(stream_get_contents(STDIN), true);
if (\$json !== null) {
if (isset(\$json['ref']) && (\$json['ref'] === 'refs/$CHECK_REMOTE_REF')) {
if (isset(\$json['object']) && isset(\$json['object']['sha'])) {
echo \$json['object']['sha'];
}
}
}
")"
# compare source reference against the latest commit
if [ "$CHECK_REMOTE_COMMIT" != "$CHECK_LOCAL_COMMIT" ]; then
2016-01-14 13:16:24 +00:00
echo "Latest local commit '$CHECK_LOCAL_COMMIT' doesn't match latest remote commit '$CHECK_REMOTE_COMMIT'; aborting..." >&2
exit 1
fi
2016-01-14 12:35:08 +00:00
echo
fi
2015-11-23 20:46:36 +00:00
# push changes
2016-01-14 12:35:08 +00:00
printf 'Pushing changes...\n'
2016-01-14 11:49:46 +00:00
git push
2015-11-25 20:55:28 +00:00
echo