git-update-ghpages 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. #!/usr/bin/env bash
  2. set -o errexit
  3. copy_contents() {
  4. local source="$1"
  5. status "Copying contents from $source"
  6. if [[ ! "$dryrun" == "1" ]]; then
  7. (cd "$source" >/dev/null && tar c .) | tar xv
  8. else
  9. _ "(cd \"$source\" >/dev/null && tar c .) | tar xv"
  10. fi
  11. }
  12. # Sets git config
  13. set_config() {
  14. if [ -n "$GIT_NAME" ]; then _ git config user.name "$GIT_NAME"; fi
  15. if [ -n "$GIT_EMAIL" ]; then _ git config user.email "$GIT_EMAIL"; fi
  16. }
  17. # Runs the deployment
  18. run() {
  19. if [ ! -d "$source" ]; then
  20. echo "Source is not a directory: $source"
  21. exit 1
  22. fi
  23. local tmpdir="$(mktemp -d)"
  24. if [[ "$force" == "1" ]]; then
  25. _ cd "$tmpdir"
  26. _ git init
  27. _ git checkout -b "$branch"
  28. copy_contents "$source"
  29. if [[ "$useenv" == "1" ]]; then set_config; fi
  30. _ git add -A .
  31. git_commit
  32. git_push --force
  33. else
  34. _ cd "$tmpdir"
  35. _ git clone "$repo" . -b "$branch" || ( \
  36. _ git init && \
  37. _ git checkout -b "$branch")
  38. if [[ "$keep" == "0" ]]; then _ rm -rf ./*; fi
  39. copy_contents "$source"
  40. if [[ "$useenv" == "1" ]]; then set_config; fi
  41. _ git add -A .
  42. git_commit || true
  43. git_push
  44. fi
  45. _ rm -rf "$tmpdir"
  46. status_ "Done"
  47. }
  48. git_commit() {
  49. if [ -z "$author" ]; then
  50. _ git commit -m "$message"
  51. else
  52. _ git commit -m "$message" --author "$author"
  53. fi
  54. }
  55. git_push() {
  56. if [ -z "$GITHUB_TOKEN" ]; then
  57. _ git push "${repo}" "$branch" "$@"
  58. else
  59. status "Pushing via \$GITHUB_TOKEN $@"
  60. _ git push "https://${GITHUB_TOKEN}@github.com/${repospec}.git" "$branch" "$@" \
  61. --quiet >/dev/null 2>&1 || \
  62. ( status_ "Failed to push"; exit 1 )
  63. fi
  64. }
  65. status() {
  66. echo -e "\n\033[34m==>\033[0;1m" "$@\033[0m"
  67. }
  68. status_() {
  69. echo -e "\033[33;1m==>\033[0m" "$@"
  70. }
  71. _() {
  72. echo ""
  73. status_ "$@"
  74. if [[ ! "$dryrun" == "1" ]]; then "$@"; fi
  75. }
  76. help() {
  77. local cmd="$(basename $0)"
  78. echo 'Usage:'
  79. echo " $cmd <REPO> <SOURCE>"
  80. echo ''
  81. echo 'Parameters:'
  82. echo " REPO repository to push to in 'user/repo' form"
  83. echo " SOURCE path to upload to repository's gh-pages branch"
  84. echo ''
  85. echo 'Options:'
  86. echo ' -h, --help show help screen'
  87. echo ' -f, --force force push'
  88. echo ' -n, --dry-run run in simulation mode'
  89. echo ' -e, --use-env pick up arguments from environment variables'
  90. echo ' -b, --branch use this branch name (default: gh-pages)'
  91. echo ' -a, --author set the author'
  92. echo ' -k, --keep keep existing files in the repo'
  93. echo ''
  94. echo 'Env var options:'
  95. echo ' GITHUB_TOKEN if set, use this to push to the repo'
  96. echo ''
  97. echo 'Optional env vars:'
  98. echo " Run with '-e' to enable the use of these variables."
  99. echo " GIT_NAME set this as the repos user.name"
  100. echo ' GIT_EMAIL set this as the repos user.email'
  101. echo ' GITHUB_REPO substitute as the REPO (1st argument)'
  102. echo ' GIT_SOURCE substitute as the SOURCE (2nd argument)'
  103. echo ' GIT_BRANCH use this branch name (--branch)'
  104. echo ''
  105. echo 'Example:'
  106. echo " $cmd rstacruz/myproject doc"
  107. echo " # pushes './doc' into the gh-pages branch of rstacruz/myproject"
  108. echo ''
  109. echo " export GITHUB_REPO='xyz/abc'"
  110. echo " export GIT_SOURCE='docs'"
  111. echo " $cmd -e"
  112. echo " # pushes './doc' into the gh-pages branch of xyz/abc"
  113. }
  114. #
  115. # Defaults
  116. #
  117. force=0
  118. dryrun=0
  119. repospec=
  120. source=
  121. branch=
  122. message="Update"
  123. useenv=0
  124. author=""
  125. keep=0
  126. #
  127. # Parse args
  128. #
  129. while [[ "$1" =~ ^- && ! "$1" == '--' ]]; do case $1 in
  130. -h | --help )
  131. help
  132. exit
  133. ;;
  134. -b | --branch )
  135. shift
  136. branch="$1"
  137. ;;
  138. -n | --dry-run )
  139. dryrun=1
  140. ;;
  141. -e | --use-env )
  142. useenv=1
  143. ;;
  144. -k | --keep )
  145. keep=1
  146. ;;
  147. -a | --author)
  148. shift
  149. author="$1"
  150. ;;
  151. -f | --force )
  152. force=1
  153. ;;
  154. esac; shift; done
  155. if [[ "$1" == '--' ]]; then shift; fi
  156. if [[ "$useenv" == "1" ]] && [[ -n "$GIT_BRANCH" ]] && [[ -z "$branch" ]]; then
  157. branch="$GIT_BRANCH"
  158. fi
  159. if [[ "$useenv" == "1" ]] && [[ -n "$GITHUB_REPO" ]] && [[ -n "$GIT_SOURCE" ]] && [[ -z "$2" ]]; then
  160. repospec="$GITHUB_REPO"
  161. source="$GIT_SOURCE"
  162. else
  163. repospec="$1"
  164. source="$2"
  165. fi
  166. : ${branch:="gh-pages"}
  167. if [ -z "$source" ]; then
  168. help
  169. exit 1
  170. fi
  171. source="`pwd -LP`/$source"
  172. repo="https://github.com/${repospec}.git"
  173. run