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