deploy-phpdoc.sh 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. # print parameters
  14. echo "Deploying phpDocs..."
  15. printf 'GITHUB_SLUG="%s"\n' "$GITHUB_SLUG"
  16. printf 'SOURCE_DIR="%s"\n' "$SOURCE_DIR"
  17. printf 'SOURCE_REF="%s"\n' "$SOURCE_REF"
  18. printf 'TARGET_DIR="%s"\n' "$TARGET_DIR"
  19. printf 'TARGET_BRANCH="%s"\n' "$TARGET_BRANCH"
  20. echo
  21. # evaluate target reference
  22. if git check-ref-format "tags/$SOURCE_REF"; then
  23. SOURCE_REF_TYPE="tag"
  24. SOURCE_REF_TAG="$SOURCE_REF"
  25. elif [[ "$SOURCE_REF" == *" @ "* ]]; then
  26. SOURCE_REF_TYPE="commit"
  27. SOURCE_REF_BRANCH="${SOURCE_REF% @ *}"
  28. SOURCE_REF_COMMIT="${SOURCE_REF##* @ }"
  29. if ! git check-ref-format "heads/$SOURCE_REF_BRANCH" || ! git rev-parse --verify "$SOURCE_REF_COMMIT" > /dev/null; then
  30. echo "FATAL: $APP_NAME target reference '$SOURCE_REF' is invalid" >&2
  31. exit 1
  32. fi
  33. elif git check-ref-format "heads/$SOURCE_REF"; then
  34. SOURCE_REF_TYPE="branch"
  35. SOURCE_REF_BRANCH="$SOURCE_REF"
  36. else
  37. echo "FATAL: $APP_NAME target reference '$SOURCE_REF' is invalid" >&2
  38. exit 1
  39. fi
  40. # clone repo
  41. printf 'Cloning repo...\n'
  42. GIT_DIR="$SOURCE_DIR.git"
  43. git clone --branch="$TARGET_BRANCH" "https://github.com/$GITHUB_SLUG.git" "$GIT_DIR"
  44. # setup git
  45. cd "$GIT_DIR"
  46. git config user.name "Travis CI"
  47. git config user.email "travis-ci@picocms.org"
  48. if [ -n "$GITHUB_OAUTH_TOKEN" ]; then
  49. git config credential.helper 'store --file=.git/credentials'
  50. (umask 077 && echo "https://GitHub:$GITHUB_OAUTH_TOKEN@github.com" > .git/credentials)
  51. fi
  52. # copy phpdoc
  53. printf '\nCopying phpDocs...\n'
  54. [ ! -d "$TARGET_DIR" ] || rm -rf "$TARGET_DIR"
  55. [ "${SOURCE_DIR:0:1}" == "/" ] || SOURCE_DIR="$BASE_PWD/$SOURCE_DIR"
  56. cp -R "$SOURCE_DIR" "$TARGET_DIR"
  57. # commit changes
  58. printf '\nCommiting changes...\n'
  59. git add --all "$TARGET_DIR"
  60. git commit -m "Update phpDocumentor class docs for $SOURCE_REF"
  61. # very simple race condition protection for concurrent Travis builds
  62. # this is no definite protection (race conditions are still possible during `git push`),
  63. # but it should give a basic protection without disabling concurrent builds completely
  64. if [ "$SOURCE_REF_TYPE" == "commit" ]; then
  65. # get latest commit
  66. printf '\nRetrieving latest commit...\n'
  67. LATEST_COMMIT="$(wget -O- "https://api.github.com/repos/$GITHUB_SLUG/git/refs/heads/$SOURCE_REF_BRANCH" 2> /dev/null | php -r "
  68. \$json = json_decode(stream_get_contents(STDIN), true);
  69. if (\$json !== null) {
  70. if (isset(\$json['ref']) && (\$json['ref'] === 'refs/heads/$SOURCE_REF_BRANCH')) {
  71. if (isset(\$json['object']) && isset(\$json['object']['sha'])) {
  72. echo \$json['object']['sha'];
  73. }
  74. }
  75. }
  76. ")"
  77. # compare target reference against the latest commit
  78. if [ "$LATEST_COMMIT" != "$SOURCE_REF_COMMIT" ]; then
  79. echo "WARNING: $APP_NAME source reference '$SOURCE_REF' doesn't match the latest commit '$LATEST_COMMIT'" >&2
  80. exit 0
  81. fi
  82. fi
  83. # push changes
  84. printf '\nPushing changes...\n'
  85. git push "https://github.com/$GITHUB_SLUG.git" "$TARGET_BRANCH:$TARGET_BRANCH"
  86. echo