deploy-phpdoc.sh 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #!/usr/bin/env bash
  2. set -e
  3. # environment variables
  4. # GITHUB_OAUTH_TOKEN GitHub authentication token, see https://github.com/settings/tokens
  5. # parameters
  6. COMMIT_MESSAGE="$1" # commit message
  7. CHECK_REPO_SLUG="$2" # optional GitHub repo (e.g. picocms/Pico) to check
  8. # its latest commit as basic race condition protection
  9. CHECK_REMOTE_REF="$3" # optional remote Git reference (e.g. heads/master)
  10. CHECK_LOCAL_COMMIT="$4" # optional local commit SHA1
  11. # print parameters
  12. echo "Deploying phpDocs..."
  13. printf 'COMMIT_MESSAGE="%s"\n' "$COMMIT_MESSAGE"
  14. printf 'CHECK_REPO_SLUG="%s"\n' "$CHECK_REPO_SLUG"
  15. printf 'CHECK_REMOTE_REF="%s"\n' "$CHECK_REMOTE_REF"
  16. printf 'CHECK_LOCAL_COMMIT="%s"\n' "$CHECK_LOCAL_COMMIT"
  17. echo
  18. # check for changes
  19. if [ -z "$(git status --porcelain)" ]; then
  20. echo "Nothing to deploy; skipping..."
  21. exit 0
  22. fi
  23. # setup git
  24. printf 'Preparing repo...\n'
  25. git config user.name "Travis CI"
  26. git config user.email "travis-ci@picocms.org"
  27. if [ -n "$GITHUB_OAUTH_TOKEN" ]; then
  28. git config credential.helper 'store --file=.git/credentials'
  29. (umask 077 && echo "https://GitHub:$GITHUB_OAUTH_TOKEN@github.com" > .git/credentials)
  30. fi
  31. # commit changes
  32. printf '\nCommiting changes...\n'
  33. git add --all
  34. git commit --message="$COMMIT_MESSAGE"
  35. # race condition protection for concurrent Travis builds
  36. # this is no definite protection (race conditions are still possible during `git push`),
  37. # but it should give a basic protection without disabling concurrent builds completely
  38. if [ -n "$CHECK_REPO_SLUG" ] && [ -n "$CHECK_REMOTE_REF" ] && [ -n "$CHECK_LOCAL_COMMIT" ]; then
  39. # retrieve information using GitHub APIv3
  40. printf '\nChecking latest commit...\n'
  41. CHECK_API_URL="https://api.github.com/repos/$CHECK_REPO_SLUG/git/refs/$CHECK_REMOTE_REF"
  42. if [ -n "$GITHUB_OAUTH_TOKEN" ]; then
  43. CHECK_API_RESPONSE="$(wget -O- --header="Authorization: token $GITHUB_OAUTH_TOKEN" "$CHECK_API_URL" 2> /dev/null)"
  44. else
  45. CHECK_API_RESPONSE="$(wget -O- "$CHECK_API_URL" 2> /dev/null)"
  46. fi
  47. # evaluate JSON response
  48. CHECK_REMOTE_COMMIT="$(echo "$CHECK_API_RESPONSE" | php -r "
  49. \$json = json_decode(stream_get_contents(STDIN), true);
  50. if (\$json !== null) {
  51. if (isset(\$json['ref']) && (\$json['ref'] === 'refs/$CHECK_REMOTE_REF')) {
  52. if (isset(\$json['object']) && isset(\$json['object']['sha'])) {
  53. echo \$json['object']['sha'];
  54. }
  55. }
  56. }
  57. ")"
  58. # compare source reference against the latest commit
  59. if [ "$CHECK_REMOTE_COMMIT" != "$CHECK_LOCAL_COMMIT" ]; then
  60. echo "WARNING: latest local commit '$CHECK_LOCAL_COMMIT' doesn't match latest remote commit '$CHECK_REMOTE_COMMIT'" >&2
  61. exit 0
  62. fi
  63. fi
  64. # push changes
  65. printf '\nPushing changes...\n'
  66. git push origin
  67. echo