deploy-phpdoc.sh 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #!/usr/bin/env bash
  2. APP_NAME="$(basename "$0")"
  3. BASE_PWD="$PWD"
  4. set -e
  5. # environment variables
  6. # GITHUB_OAUTH_TOKEN GitHub authentication token, see https://github.com/settings/tokens
  7. # parameters
  8. SOURCE_DIR="$1" # absolute local source path
  9. TARGET_REPO_SLUG="$2" # target repo (e.g. picocms/Pico)
  10. TARGET_BRANCH="$3" # target branch (e.g. gh-pages)
  11. TARGET_REF="$4" # target reference (either [branch]@[commit], [branch] or [tag])
  12. # evaluate target reference
  13. if git check-ref-format "tags/$TARGET_REF"; then
  14. TARGET_REF_TYPE="tag"
  15. TARGET_REF_TAG="$TARGET_REF"
  16. TARGET_DIR="$TARGET_REF_TAG"
  17. elif [[ "$TARGET_REF" == *@* ]]; then
  18. TARGET_REF_TYPE="commit"
  19. TARGET_REF_BRANCH="${TARGET_REF%@*}"
  20. TARGET_REF_COMMIT="${TARGET_REF##*@}"
  21. TARGET_DIR="$TARGET_REF_BRANCH"
  22. if ! git check-ref-format "heads/$TARGET_REF_BRANCH"; then
  23. echo "FATAL: $APP_NAME target reference '$TARGET_REF' is invalid" >&2
  24. exit 1
  25. fi
  26. elif git check-ref-format "heads/$TARGET_REF"; then
  27. TARGET_REF_TYPE="branch"
  28. TARGET_REF_BRANCH="$TARGET_REF"
  29. TARGET_DIR="$TARGET_REF_BRANCH"
  30. else
  31. echo "FATAL: $APP_NAME target reference '$TARGET_REF' is invalid" >&2
  32. exit 1
  33. fi
  34. # clone repo
  35. GIT_DIR="$SOURCE_DIR.git"
  36. git clone -b "$TARGET_BRANCH" "https://github.com/$TARGET_REPO_SLUG.git" "$GIT_DIR"
  37. # setup git
  38. cd "$GIT_DIR"
  39. git config user.name "Travis CI"
  40. git config user.email "travis-ci@picocms.org"
  41. [ -n "$GITHUB_OAUTH_TOKEN" ] && git config credential.https://github.com.username "$GITHUB_OAUTH_TOKEN"
  42. # copy phpdoc
  43. [ -e "$TARGET_DIR" ] && echo "FATAL: $(basename "$0") target directory '$TARGET_DIR' exists" >&2 && exit 1
  44. [ "${SOURCE_DIR:0:1}" == "/" ] || SOURCE_DIR="$BASE_PWD/$SOURCE_DIR"
  45. cp -R "$SOURCE_DIR" "phpDoc/$TARGET_DIR"
  46. # commit changes
  47. git add "$TARGET_DIR"
  48. git commit -m "Add phpDocumentor class docs for $TARGET_REF"
  49. # very simple race condition protection for concurrent Travis builds
  50. # this is no definite protection (race conditions are still possible during `git push`),
  51. # but it should give a basic protection without disabling concurrent builds completely
  52. if [ "$TARGET_REF_TYPE" == "commit" ]; then
  53. # get latest commit
  54. LATEST_COMMIT="$(wget -O- "https://api.github.com/repos/$TARGET_REPO_SLUG/git/refs/heads/$TARGET_REF_BRANCH" 2> /dev/null | php -r "
  55. \$json = json_decode(stream_get_contents(STDIN), true);
  56. if (\$json !== null) {
  57. if (isset(\$json['ref']) && (\$json['ref'] === 'refs/heads/$TARGET_REF_BRANCH')) {
  58. if (isset(\$json['object']) && isset(\$json['object']['sha'])) {
  59. echo \$json['object']['sha'];
  60. }
  61. }
  62. }
  63. ")"
  64. # compare target reference against the latest commit
  65. if [ "$LATEST_COMMIT" != "$TARGET_REF_COMMIT" ]; then
  66. echo "WARNING: $APP_NAME target reference '$TARGET_REF' doesn't match the latest commit '$LATEST_COMMIT'" >&2
  67. exit 0
  68. fi
  69. fi
  70. # push changes
  71. git push "https://github.com/$TARGET_REPO_SLUG.git" "$TARGET_BRANCH:$TARGET_BRANCH"