deploy-phpdoc.sh 3.4 KB

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