deploy-phpdoc.sh 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. GITHUB_SLUG="$1" # GitHub repo (e.g. picocms/Pico)
  9. SOURCE_DIR="$2" # absolute source path
  10. SOURCE_REF="$3" # source reference (either [branch]@[commit], [branch] or [tag])
  11. TARGET_DIR="$4" # relative target path
  12. TARGET_BRANCH="$5" # target branch (e.g. gh-pages)
  13. printf 'Deploying phpDocs (%s (%s) --> %s:%s/%s)...\n' "$SOURCE_DIR" "$SOURCE_REF" "$GITHUB_SLUG" "$TARGET_BRANCH" "$TARGET_DIR"
  14. # evaluate target reference
  15. if git check-ref-format "tags/$SOURCE_REF"; then
  16. SOURCE_REF_TYPE="tag"
  17. SOURCE_REF_TAG="$SOURCE_REF"
  18. elif [[ "$SOURCE_REF" == *@* ]]; then
  19. SOURCE_REF_TYPE="commit"
  20. SOURCE_REF_BRANCH="${SOURCE_REF%@*}"
  21. SOURCE_REF_COMMIT="${SOURCE_REF##*@}"
  22. if ! git check-ref-format "heads/$SOURCE_REF_BRANCH"; then
  23. echo "FATAL: $APP_NAME target reference '$SOURCE_REF' is invalid" >&2
  24. exit 1
  25. fi
  26. elif git check-ref-format "heads/$SOURCE_REF"; then
  27. SOURCE_REF_TYPE="branch"
  28. SOURCE_REF_BRANCH="$SOURCE_REF"
  29. else
  30. echo "FATAL: $APP_NAME target reference '$SOURCE_REF' is invalid" >&2
  31. exit 1
  32. fi
  33. # clone repo
  34. printf '\nCloning %s branch of %s...\n' "$TARGET_BRANCH" "https://github.com/$GITHUB_SLUG.git"
  35. GIT_DIR="$SOURCE_DIR.git"
  36. git clone -b "$TARGET_BRANCH" "https://github.com/$GITHUB_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. [ ! -d "$TARGET_DIR" ] || rm -rf "$TARGET_DIR"
  44. [ "${SOURCE_DIR:0:1}" == "/" ] || SOURCE_DIR="$BASE_PWD/$SOURCE_DIR"
  45. printf '\nCopying phpDoc (%s --> %s)...\n' "$SOURCE_DIR" "$GIT_DIR/$TARGET_DIR"
  46. cp -R "$SOURCE_DIR" "$TARGET_DIR"
  47. # commit changes
  48. printf '\nCommiting changes...\n'
  49. git add "$TARGET_DIR"
  50. git commit -m "Add phpDocumentor class docs for $SOURCE_REF"
  51. # very simple race condition protection for concurrent Travis builds
  52. # this is no definite protection (race conditions are still possible during `git push`),
  53. # but it should give a basic protection without disabling concurrent builds completely
  54. if [ "$SOURCE_REF_TYPE" == "commit" ]; then
  55. # get latest commit
  56. printf '\nRetrieving latest commit of %s:%s' "$GITHUB_SLUG" "$SOURCE_REF_BRANCH"
  57. LATEST_COMMIT="$(wget -O- "https://api.github.com/repos/$GITHUB_SLUG/git/refs/heads/$SOURCE_REF_BRANCH" 2> /dev/null | php -r "
  58. \$json = json_decode(stream_get_contents(STDIN), true);
  59. if (\$json !== null) {
  60. if (isset(\$json['ref']) && (\$json['ref'] === 'refs/heads/$SOURCE_REF_BRANCH')) {
  61. if (isset(\$json['object']) && isset(\$json['object']['sha'])) {
  62. echo \$json['object']['sha'];
  63. }
  64. }
  65. }
  66. ")"
  67. # compare target reference against the latest commit
  68. if [ "$LATEST_COMMIT" != "$SOURCE_REF_COMMIT" ]; then
  69. echo "WARNING: $APP_NAME target reference '$SOURCE_REF' doesn't match the latest commit '$LATEST_COMMIT'" >&2
  70. exit 0
  71. fi
  72. fi
  73. # push changes
  74. printf '\nPushing changes...\n'
  75. git push "https://github.com/$GITHUB_SLUG.git" "$TARGET_BRANCH:$TARGET_BRANCH"