deploy-phpdoc.sh 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. printf 'Nothing to deploy; skipping...\n\n'
  21. exit 0
  22. fi
  23. # setup git
  24. printf 'Preparing repo...\n'
  25. git config push.default simple
  26. git config user.name "Travis CI"
  27. git config user.email "travis-ci@picocms.org"
  28. if [ -n "$GITHUB_OAUTH_TOKEN" ]; then
  29. git config credential.helper 'store --file=.git/credentials'
  30. (umask 077 && echo "https://GitHub:$GITHUB_OAUTH_TOKEN@github.com" > .git/credentials)
  31. fi
  32. # commit changes
  33. printf '\nCommiting changes...\n'
  34. git add --all
  35. git commit --message="$COMMIT_MESSAGE"
  36. # race condition protection for concurrent Travis builds
  37. # this is no definite protection (race conditions are still possible during `git push`),
  38. # but it should give a basic protection without disabling concurrent builds completely
  39. if [ -n "$CHECK_REPO_SLUG" ] && [ -n "$CHECK_REMOTE_REF" ] && [ -n "$CHECK_LOCAL_COMMIT" ]; then
  40. # retrieve information using GitHub APIv3
  41. printf '\nChecking latest commit...\n'
  42. CHECK_API_URL="https://api.github.com/repos/$CHECK_REPO_SLUG/git/refs/$CHECK_REMOTE_REF"
  43. if [ -n "$GITHUB_OAUTH_TOKEN" ]; then
  44. CHECK_API_RESPONSE="$(wget -O- --header="Authorization: token $GITHUB_OAUTH_TOKEN" "$CHECK_API_URL" 2> /dev/null)"
  45. else
  46. CHECK_API_RESPONSE="$(wget -O- "$CHECK_API_URL" 2> /dev/null)"
  47. fi
  48. # evaluate JSON response
  49. CHECK_REMOTE_COMMIT="$(echo "$CHECK_API_RESPONSE" | php -r "
  50. \$json = json_decode(stream_get_contents(STDIN), true);
  51. if (\$json !== null) {
  52. if (isset(\$json['ref']) && (\$json['ref'] === 'refs/$CHECK_REMOTE_REF')) {
  53. if (isset(\$json['object']) && isset(\$json['object']['sha'])) {
  54. echo \$json['object']['sha'];
  55. }
  56. }
  57. }
  58. ")"
  59. # compare source reference against the latest commit
  60. if [ "$CHECK_REMOTE_COMMIT" != "$CHECK_LOCAL_COMMIT" ]; then
  61. echo "WARNING: latest local commit '$CHECK_LOCAL_COMMIT' doesn't match latest remote commit '$CHECK_REMOTE_COMMIT'" >&2
  62. exit 0
  63. fi
  64. fi
  65. # push changes
  66. printf '\nPushing changes...\n'
  67. git push origin
  68. echo