|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +set -o errexit -o nounset |
| 4 | + |
| 5 | +#################################### |
| 6 | +# repo specific variables should be set in .travis.yml |
| 7 | +#################################### |
| 8 | +# TRIGGER_REPO_SLUG="patternfly/patternfly" |
| 9 | +# TRIGGER_REPO_BRANCH="master" |
| 10 | +#################################### |
| 11 | +#################################### |
| 12 | + |
| 13 | +SCRIPT=`basename $0` |
| 14 | +ACTION="Manual" |
| 15 | +REPO_NAME="origin" |
| 16 | +SOURCE_BRANCH=`git rev-parse --abbrev-ref HEAD` |
| 17 | +QUIET=false |
| 18 | + |
| 19 | +RED='\033[0;31m' |
| 20 | +YELLOW='\033[1;33m' |
| 21 | +GREEN='\033[1;32m' |
| 22 | +NC='\033[0m' # No Color |
| 23 | + |
| 24 | +echoHeader () { |
| 25 | + echo |
| 26 | + echo -e "${YELLOW}################################################################################${NC}" |
| 27 | + echo -e "${GREEN}${@}${NC}" |
| 28 | + echo -e "${YELLOW}################################################################################${NC}" |
| 29 | +} |
| 30 | + |
| 31 | +confirm () { |
| 32 | + if $QUIET; then |
| 33 | + true |
| 34 | + else |
| 35 | + # call with a prompt string or use a default |
| 36 | + QUESTION="${1:-Are you sure? [y/N]} " |
| 37 | + echo -e -n $QUESTION |
| 38 | + read -r RESPONSE |
| 39 | + case $RESPONSE in |
| 40 | + [yY][eE][sS]|[yY]) |
| 41 | + true |
| 42 | + ;; |
| 43 | + *) |
| 44 | + false |
| 45 | + ;; |
| 46 | + esac |
| 47 | + fi |
| 48 | +} |
| 49 | + |
| 50 | +setUserInfo () { |
| 51 | + git config --global user.name "patternfly-build" |
| 52 | + git config --global user.email "[email protected]" |
| 53 | + git config --global push.default simple |
| 54 | +} |
| 55 | + |
| 56 | +getDeployKey () { |
| 57 | + # Get the deploy key by using Travis's stored variables to decrypt deploy_key.enc |
| 58 | + ENCRYPTED_KEY_VAR="encrypted_${ENCRYPTION_LABEL}_key" |
| 59 | + ENCRYPTED_IV_VAR="encrypted_${ENCRYPTION_LABEL}_iv" |
| 60 | + ENCRYPTED_KEY=${!ENCRYPTED_KEY_VAR} |
| 61 | + ENCRYPTED_IV=${!ENCRYPTED_IV_VAR} |
| 62 | + openssl aes-256-cbc -K $ENCRYPTED_KEY -iv $ENCRYPTED_IV -in deploy_key.enc -out deploy_key -d |
| 63 | + chmod 600 deploy_key |
| 64 | + eval `ssh-agent -s` |
| 65 | + ssh-add deploy_key |
| 66 | +} |
| 67 | + |
| 68 | +checkTriggerRepo () { |
| 69 | + echo "$TRAVIS_REPO_SLUG $TRIGGER_REPO_SLUG $TRIGGER_REPO_BRANCH" |
| 70 | + if [ "${TRAVIS_REPO_SLUG}" = "${TRIGGER_REPO_SLUG}" ]; then |
| 71 | + echo "This action is running against ${TRIGGER_REPO_SLUG}." |
| 72 | + if [ -z "${TRAVIS_TAG}" -a "${TRAVIS_BRANCH}" != "${TRIGGER_REPO_BRANCH}" ]; then |
| 73 | + echo "This commit was made against ${TRAVIS_BRANCH} and not the ${TRIGGER_REPO_BRANCH} branch. Aborting." |
| 74 | + exit 1 |
| 75 | + fi |
| 76 | + else |
| 77 | + echo "This action is not running against ${TRIGGER_REPO_SLUG}. Aborting." |
| 78 | + exit 1 |
| 79 | + fi |
| 80 | +} |
| 81 | + |
| 82 | +inferRepo () { |
| 83 | + REPO=`git config remote.${REPO_NAME}.url` |
| 84 | + SSH_REPO=${REPO/https:\/\/github.com\//git@github.com:} |
| 85 | + echo "Inferred REPO ${SSH_REPO}" |
| 86 | +} |
| 87 | + |
| 88 | +confirmRepo () { |
| 89 | + confirm "${YELLOW}Push ${SITE_FOLDER} to repo ${SSH_REPO}/gh-pages? [y/N] ${NC}" |
| 90 | + return $? |
| 91 | +} |
| 92 | + |
| 93 | +checkRemoteBranchExists () { |
| 94 | + BRANCH="${1:-gh-pages}" |
| 95 | + EXISTING=`git ls-remote --heads ${REPO} ${BRANCH}` |
| 96 | + echo $EXISTING |
| 97 | +} |
| 98 | + |
| 99 | +cleanSite () { |
| 100 | + if [ -d "github.io" ]; then |
| 101 | + rm -rf github.io |
| 102 | + fi |
| 103 | +} |
| 104 | + |
| 105 | +cleanBranch () { |
| 106 | + # check if the branch exists without triggering errexit |
| 107 | + git rev-parse --verify --quiet gh-pages-deploy 1> /dev/null && rc=$? || rc=$? |
| 108 | + if [ "$rc" = 0 ]; then |
| 109 | + CURRENT_BRANCH=`git rev-parse --abbrev-ref HEAD` |
| 110 | + if [ "$CURRENT_BRANCH" = "gh-pages-deploy" ]; then |
| 111 | + git checkout $SOURCE_BRANCH |
| 112 | + fi |
| 113 | + git branch -D gh-pages-deploy |
| 114 | + fi |
| 115 | +} |
| 116 | + |
| 117 | +cloneSite () { |
| 118 | + git clone --branch gh-pages $SSH_REPO github.io |
| 119 | +} |
| 120 | + |
| 121 | +copySite () { |
| 122 | + echo rsync -q -rav --delete --exclude .git ${SITE_FOLDER} github.io |
| 123 | + rsync -q -rav --delete --exclude .git ${SITE_FOLDER}/ github.io/ |
| 124 | +} |
| 125 | + |
| 126 | +pushSite () { |
| 127 | + SHA=`git rev-parse HEAD` |
| 128 | + git -C github.io add . -A |
| 129 | + if [ -z "$(git -C github.io status --porcelain)" ]; then |
| 130 | + echo -e "${YELLOW}Site directory clean, no changes to commit.${NC}" |
| 131 | + else |
| 132 | + echo -e "${YELLOW}Changes in site directory, committing changes.${NC}" |
| 133 | + git -C github.io commit -q -a -m "Added files from commit #${SHA} " |
| 134 | + echo -e "Pushing commit ${SHA} to repo ${SSH_REPO}." |
| 135 | + confirmRepo && rc=$? || rc=$? |
| 136 | + if [ "$rc" = 0 ]; then |
| 137 | + git -C github.io push $SSH_REPO gh-pages:gh-pages |
| 138 | + fi |
| 139 | + fi |
| 140 | +} |
| 141 | + |
| 142 | +splitSite () { |
| 143 | + git checkout -b gh-pages-deploy |
| 144 | + git add -f ${SITE_FOLDER} |
| 145 | + git commit -q -m "Added ${SITE_FOLDER} folder" |
| 146 | + |
| 147 | + SHA=`git subtree split --prefix ${SITE_FOLDER} gh-pages-deploy` |
| 148 | + echo -e "Pushing commit ${SHA} to repo ${SSH_REPO}." |
| 149 | + confirmRepo && rc=$? || rc=$? |
| 150 | + if [ "$rc" = 0 ]; then |
| 151 | + git push ${SSH_REPO} ${SHA}:refs/heads/gh-pages --force |
| 152 | + fi |
| 153 | +} |
| 154 | + |
| 155 | +deploySite () { |
| 156 | + if [ "$SOURCE_BRANCH" = "gh-pages" -o "$SOURCE_BRANCH" = "gh-pages-deploy" ]; then |
| 157 | + echo -e "${RED}Error: cannot deploy from the gh-pages branch. Please checkout a different branch." |
| 158 | + fi |
| 159 | + |
| 160 | + git checkout ${SOURCE_BRANCH} |
| 161 | + inferRepo $REPO_NAME |
| 162 | + EXISTING=`checkRemoteBranchExists gh-pages` |
| 163 | + if [ -n "$EXISTING" ]; then |
| 164 | + echo -e "${GREEN}### gh-pages branch exists, pushing updates${NC}" |
| 165 | + cleanSite |
| 166 | + cloneSite |
| 167 | + copySite |
| 168 | + pushSite |
| 169 | + cleanSite |
| 170 | + else |
| 171 | + echo -e "${GREEN}### gh-pages branch does not exist, splitting branch${NC}" |
| 172 | + cleanBranch |
| 173 | + splitSite |
| 174 | + cleanBranch |
| 175 | + fi |
| 176 | +} |
| 177 | + |
| 178 | +manualDeploy () { |
| 179 | + deploySite |
| 180 | +} |
| 181 | + |
| 182 | +travisDeploy () { |
| 183 | + checkTriggerRepo |
| 184 | + setUserInfo |
| 185 | + getDeployKey |
| 186 | + deploySite |
| 187 | +} |
| 188 | + |
| 189 | +checkSiteFolderExists () { |
| 190 | + if [ -z $SITE_FOLDER ]; then |
| 191 | + echo -e "${RED}Error: Please specify a folder to publish.${NC}" |
| 192 | + usage |
| 193 | + exit -1 |
| 194 | + fi |
| 195 | + |
| 196 | + if [ ! -d ${SITE_FOLDER} ]; then |
| 197 | + echo -e "${RED}Error: The '${SITE_FOLDER}' folder does not exsit. Run the build script to generate it.${NC}" |
| 198 | + exit -1 |
| 199 | + fi |
| 200 | +} |
| 201 | + |
| 202 | +parseOpts() { |
| 203 | + while getopts htr:b: OPT "$@"; do |
| 204 | + case $OPT in |
| 205 | + h) usage; exit 0;; |
| 206 | + t) ACTION="Travis" |
| 207 | + QUIET=true |
| 208 | + ;; |
| 209 | + r) REPO_NAME=$OPTARG;; |
| 210 | + b) SOURCE_BRANCH=$OPTARG;; |
| 211 | + esac |
| 212 | + done |
| 213 | + |
| 214 | + shift $((OPTIND-1)) |
| 215 | + SITE_FOLDER="${1:- }" |
| 216 | +} |
| 217 | + |
| 218 | +usage () { |
| 219 | +cat <<- EEOOFF |
| 220 | +
|
| 221 | + This script will publish files to the gh-pages branch of your repo. |
| 222 | +
|
| 223 | + $SCRIPT [option] folder |
| 224 | +
|
| 225 | + Example: $SCRIPT |
| 226 | +
|
| 227 | + OPTIONS: |
| 228 | + h Display this message |
| 229 | + t Perform a deploy from travis, using a travis encrypted key |
| 230 | + r Repository name to publish to (deafult: origin) |
| 231 | + b Source branch to publish from (default: master) |
| 232 | +
|
| 233 | +EEOOFF |
| 234 | +} |
| 235 | + |
| 236 | +main () { |
| 237 | + parseOpts "$@" |
| 238 | + |
| 239 | + checkSiteFolderExists |
| 240 | + |
| 241 | + echoHeader "${ACTION} deploy of ${REPO_NAME}/${SOURCE_BRANCH}:${SITE_FOLDER} to gh-pages" |
| 242 | + |
| 243 | + case $ACTION in |
| 244 | + Manual) |
| 245 | + manualDeploy "$@" |
| 246 | + ;; |
| 247 | + Travis) |
| 248 | + travisDeploy |
| 249 | + esac |
| 250 | +} |
| 251 | + |
| 252 | +main "$@" |
0 commit comments