deploy-phpdoc.sh 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #!/usr/bin/env bash
  2. set -e
  3. # base variables
  4. APP_NAME="$(basename "$0")"
  5. BASE_PWD="$PWD"
  6. # environment variables
  7. # GITHUB_OAUTH_TOKEN GitHub authentication token, see https://github.com/settings/tokens
  8. # parameters
  9. SOURCE_REPO_SLUG="$1" # source GitHub repo (e.g. picocms/Pico)
  10. SOURCE_REF="$2" # source reference (either [branch]@[commit], [branch] or [tag])
  11. SOURCE_DIR="$3" # absolute source path
  12. TARGET_REPO_SLUG="$4" # target GitHub repo (e.g. picocms/Pico)
  13. TARGET_BRANCH="$5" # target branch (e.g. gh-pages)
  14. TARGET_DIR="$6" # relative target path
  15. # print parameters
  16. echo "Deploying phpDocs..."
  17. printf 'SOURCE_REPO_SLUG="%s"\n' "$SOURCE_REPO_SLUG"
  18. printf 'SOURCE_REF="%s"\n' "$SOURCE_REF"
  19. printf 'SOURCE_DIR="%s"\n' "$SOURCE_DIR"
  20. printf 'TARGET_REPO_SLUG="%s"\n' "$TARGET_REPO_SLUG"
  21. printf 'TARGET_BRANCH="%s"\n' "$TARGET_BRANCH"
  22. printf 'TARGET_DIR="%s"\n' "$TARGET_DIR"
  23. echo
  24. # evaluate target reference
  25. if git check-ref-format "tags/$SOURCE_REF"; then
  26. SOURCE_REF_TYPE="tag"
  27. SOURCE_REF_TAG="$SOURCE_REF"
  28. elif [[ "$SOURCE_REF" == *" @ "* ]]; then
  29. SOURCE_REF_TYPE="commit"
  30. SOURCE_REF_BRANCH="${SOURCE_REF% @ *}"
  31. SOURCE_REF_COMMIT="${SOURCE_REF##* @ }"
  32. if ! git check-ref-format "heads/$SOURCE_REF_BRANCH" || ! git rev-parse --verify "$SOURCE_REF_COMMIT" > /dev/null; then
  33. echo "FATAL: $APP_NAME target reference '$SOURCE_REF' is invalid" >&2
  34. exit 1
  35. fi
  36. elif git check-ref-format "heads/$SOURCE_REF"; then
  37. SOURCE_REF_TYPE="branch"
  38. SOURCE_REF_BRANCH="$SOURCE_REF"
  39. else
  40. echo "FATAL: $APP_NAME target reference '$SOURCE_REF' is invalid" >&2
  41. exit 1
  42. fi
  43. # clone repo
  44. printf 'Cloning repo...\n'
  45. GIT_DIR="$SOURCE_DIR.git"
  46. git clone --branch="$TARGET_BRANCH" "https://github.com/$TARGET_REPO_SLUG.git" "$GIT_DIR"
  47. # setup git
  48. cd "$GIT_DIR"
  49. git config user.name "Travis CI"
  50. git config user.email "travis-ci@picocms.org"
  51. if [ -n "$GITHUB_OAUTH_TOKEN" ]; then
  52. git config credential.helper 'store --file=.git/credentials'
  53. (umask 077 && echo "https://GitHub:$GITHUB_OAUTH_TOKEN@github.com" > .git/credentials)
  54. fi
  55. # copy phpdoc
  56. printf '\nCopying phpDocs...\n'
  57. [ ! -d "$TARGET_DIR" ] || rm -rf "$TARGET_DIR"
  58. [ "${SOURCE_DIR:0:1}" == "/" ] || SOURCE_DIR="$BASE_PWD/$SOURCE_DIR"
  59. cp -R "$SOURCE_DIR" "$TARGET_DIR"
  60. # commit changes
  61. printf '\nCommiting changes...\n'
  62. git add --all "$TARGET_DIR"
  63. git commit --message="Update phpDocumentor class docs for $SOURCE_REF"
  64. # very simple race condition protection for concurrent Travis builds
  65. # this is no definite protection (race conditions are still possible during `git push`),
  66. # but it should give a basic protection without disabling concurrent builds completely
  67. if [ "$SOURCE_REF_TYPE" == "commit" ]; then
  68. # load branch data via GitHub APIv3
  69. printf '\nRetrieving latest commit...\n'
  70. LATEST_COMMIT_URL="https://api.github.com/repos/$SOURCE_REPO_SLUG/git/refs/heads/$SOURCE_REF_BRANCH"
  71. if [ -n "$GITHUB_OAUTH_TOKEN" ]; then
  72. LATEST_COMMIT_RESPONSE="$(wget -O- --header="Authorization: token $GITHUB_OAUTH_TOKEN" "$LATEST_COMMIT_URL" 2> /dev/null)"
  73. else
  74. LATEST_COMMIT_RESPONSE="$(wget -O- "$LATEST_COMMIT_URL" 2> /dev/null)"
  75. fi
  76. # evaluate JSON response
  77. LATEST_COMMIT="$(echo "$LATEST_COMMIT_RESPONSE" | php -r "
  78. \$json = json_decode(stream_get_contents(STDIN), true);
  79. if (\$json !== null) {
  80. if (isset(\$json['ref']) && (\$json['ref'] === 'refs/heads/$SOURCE_REF_BRANCH')) {
  81. if (isset(\$json['object']) && isset(\$json['object']['sha'])) {
  82. echo \$json['object']['sha'];
  83. }
  84. }
  85. }
  86. ")"
  87. # compare target reference against the latest commit
  88. if [ "$LATEST_COMMIT" != "$SOURCE_REF_COMMIT" ]; then
  89. echo "WARNING: $APP_NAME source reference '$SOURCE_REF' doesn't match the latest commit '$LATEST_COMMIT'" >&2
  90. exit 0
  91. fi
  92. fi
  93. # push changes
  94. printf '\nPushing changes...\n'
  95. git push "https://github.com/$TARGET_REPO_SLUG.git" "$TARGET_BRANCH:$TARGET_BRANCH"
  96. echo