deploy-phpdoc.sh 3.0 KB

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