2015-11-23 18:04:08 +00:00
|
|
|
#!/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
|
2015-11-30 13:53:10 +00:00
|
|
|
# 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
|
|
|
|
2017-06-21 00:23:13 +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..."
|
2015-11-30 13:53:10 +00:00
|
|
|
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
|
2015-11-25 19:39:49 +00:00
|
|
|
|
2015-11-30 13:53:10 +00:00
|
|
|
# check for changes
|
2016-01-14 11:49:46 +00:00
|
|
|
if [ -z "$(git log --oneline '@{upstream}..')" ]; then
|
2015-11-30 14:24:00 +00:00
|
|
|
printf 'Nothing to deploy; skipping...\n\n'
|
2015-11-30 13:53:10 +00:00
|
|
|
exit 0
|
2015-11-25 19:39:49 +00:00
|
|
|
fi
|
2015-11-23 20:46:36 +00:00
|
|
|
|
2015-11-30 13:53:10 +00:00
|
|
|
# race condition protection for concurrent Travis builds
|
2015-11-25 19:39:49 +00:00
|
|
|
# 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
|
2015-11-30 13:53:10 +00:00
|
|
|
if [ -n "$CHECK_REPO_SLUG" ] && [ -n "$CHECK_REMOTE_REF" ] && [ -n "$CHECK_LOCAL_COMMIT" ]; then
|
|
|
|
# retrieve information using GitHub APIv3
|
2017-06-21 00:23:13 +00:00
|
|
|
echo "Checking latest commit$([ -n "$GITHUB_OAUTH_TOKEN" ] && echo " (authorized)")..."
|
2015-11-30 13:53:10 +00:00
|
|
|
CHECK_API_URL="https://api.github.com/repos/$CHECK_REPO_SLUG/git/refs/$CHECK_REMOTE_REF"
|
2015-11-26 00:13:37 +00:00
|
|
|
if [ -n "$GITHUB_OAUTH_TOKEN" ]; then
|
2017-06-21 00:23:13 +00:00
|
|
|
CHECK_API_RESPONSE="$(curl --fail --silent --show-error --header "Authorization: token $GITHUB_OAUTH_TOKEN" "$CHECK_API_URL")"
|
2015-11-26 00:13:37 +00:00
|
|
|
else
|
2017-06-21 00:23:13 +00:00
|
|
|
CHECK_API_RESPONSE="$(curl --fail --silent --show-error "$CHECK_API_URL")"
|
2015-11-26 00:13:37 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
# evaluate JSON response
|
2015-11-30 13:53:10 +00:00
|
|
|
CHECK_REMOTE_COMMIT="$(echo "$CHECK_API_RESPONSE" | php -r "
|
2015-11-25 19:39:49 +00:00
|
|
|
\$json = json_decode(stream_get_contents(STDIN), true);
|
|
|
|
if (\$json !== null) {
|
2015-11-30 13:53:10 +00:00
|
|
|
if (isset(\$json['ref']) && (\$json['ref'] === 'refs/$CHECK_REMOTE_REF')) {
|
2015-11-25 19:39:49 +00:00
|
|
|
if (isset(\$json['object']) && isset(\$json['object']['sha'])) {
|
|
|
|
echo \$json['object']['sha'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
")"
|
|
|
|
|
2015-11-26 01:15:34 +00:00
|
|
|
# compare source reference against the latest commit
|
2015-11-30 13:53:10 +00:00
|
|
|
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
|
2016-01-14 21:49:31 +00:00
|
|
|
exit 1
|
2015-11-25 19:39:49 +00:00
|
|
|
fi
|
2016-01-14 12:35:08 +00:00
|
|
|
|
|
|
|
echo
|
2015-11-25 19:39:49 +00:00
|
|
|
fi
|
2015-11-23 18:04:08 +00:00
|
|
|
|
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
|