|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# This file was taken from https://github.com/X1011/git-directory-deploy/blob/e37ac94cda4bfc5773c0f01d89d8c875a21ab4f9/deploy.sh, |
| 4 | +# Copyright 2013-2021 Daniel Smith |
| 5 | +# All rights reserved. |
| 6 | +# It's licensed under the terms of the 3-clause BSD license. |
| 7 | + |
| 8 | +set -o errexit #abort if any command fails |
| 9 | +me=$(basename "$0") |
| 10 | + |
| 11 | +help_message="\ |
| 12 | +Usage: $me [-c FILE] [<options>] |
| 13 | +Deploy generated files to a git branch. |
| 14 | +
|
| 15 | +Options: |
| 16 | +
|
| 17 | + -h, --help Show this help information. |
| 18 | + -v, --verbose Increase verbosity. Useful for debugging. |
| 19 | + -e, --allow-empty Allow deployment of an empty directory. |
| 20 | + -m, --message MESSAGE Specify the message used when committing on the |
| 21 | + deploy branch. |
| 22 | + -n, --no-hash Don't append the source commit's hash to the deploy |
| 23 | + commit's message. |
| 24 | + -c, --config-file PATH Override default & environment variables' values |
| 25 | + with those in set in the file at 'PATH'. Must be the |
| 26 | + first option specified. |
| 27 | +
|
| 28 | +Variables: |
| 29 | +
|
| 30 | + GIT_DEPLOY_DIR Folder path containing the files to deploy. |
| 31 | + GIT_DEPLOY_BRANCH Commit deployable files to this branch. |
| 32 | + GIT_DEPLOY_REPO Push the deploy branch to this repository. |
| 33 | +
|
| 34 | +These variables have default values defined in the script. The defaults can be |
| 35 | +overridden by environment variables. Any environment variables are overridden |
| 36 | +by values set in a '.env' file (if it exists), and in turn by those set in a |
| 37 | +file specified by the '--config-file' option." |
| 38 | + |
| 39 | +parse_args() { |
| 40 | + # Set args from a local environment file. |
| 41 | + if [ -e ".env" ]; then |
| 42 | + source .env |
| 43 | + fi |
| 44 | + |
| 45 | + # Set args from file specified on the command-line. |
| 46 | + if [[ $1 = "-c" || $1 = "--config-file" ]]; then |
| 47 | + source "$2" |
| 48 | + shift 2 |
| 49 | + fi |
| 50 | + |
| 51 | + # Parse arg flags |
| 52 | + # If something is exposed as an environment variable, set/overwrite it |
| 53 | + # here. Otherwise, set/overwrite the internal variable instead. |
| 54 | + while : ; do |
| 55 | + if [[ $1 = "-h" || $1 = "--help" ]]; then |
| 56 | + echo "$help_message" |
| 57 | + return 0 |
| 58 | + elif [[ $1 = "-v" || $1 = "--verbose" ]]; then |
| 59 | + verbose=true |
| 60 | + shift |
| 61 | + elif [[ $1 = "-e" || $1 = "--allow-empty" ]]; then |
| 62 | + allow_empty=true |
| 63 | + shift |
| 64 | + elif [[ ( $1 = "-m" || $1 = "--message" ) && -n $2 ]]; then |
| 65 | + commit_message=$2 |
| 66 | + shift 2 |
| 67 | + elif [[ $1 = "-n" || $1 = "--no-hash" ]]; then |
| 68 | + GIT_DEPLOY_APPEND_HASH=false |
| 69 | + shift |
| 70 | + else |
| 71 | + break |
| 72 | + fi |
| 73 | + done |
| 74 | + |
| 75 | + # Set internal option vars from the environment and arg flags. All internal |
| 76 | + # vars should be declared here, with sane defaults if applicable. |
| 77 | + |
| 78 | + # Source directory & target branch. |
| 79 | + deploy_directory=${GIT_DEPLOY_DIR:-dist} |
| 80 | + deploy_branch=${GIT_DEPLOY_BRANCH:-gh-pages} |
| 81 | + |
| 82 | + #if no user identity is already set in the current git environment, use this: |
| 83 | + default_username=${GIT_DEPLOY_USERNAME:-deploy.sh} |
| 84 | + default_email=${GIT_DEPLOY_EMAIL:-} |
| 85 | + |
| 86 | + #repository to deploy to. must be readable and writable. |
| 87 | + repo=${GIT_DEPLOY_REPO:-origin} |
| 88 | + |
| 89 | + #append commit hash to the end of message by default |
| 90 | + append_hash=${GIT_DEPLOY_APPEND_HASH:-true} |
| 91 | +} |
| 92 | + |
| 93 | +main() { |
| 94 | + parse_args "$@" |
| 95 | + |
| 96 | + enable_expanded_output |
| 97 | + |
| 98 | + if ! git diff --exit-code --quiet --cached; then |
| 99 | + echo Aborting due to uncommitted changes in the index >&2 |
| 100 | + return 1 |
| 101 | + fi |
| 102 | + |
| 103 | + commit_title=`git log -n 1 --format="%s" HEAD` |
| 104 | + commit_hash=` git log -n 1 --format="%H" HEAD` |
| 105 | + |
| 106 | + #default commit message uses last title if a custom one is not supplied |
| 107 | + if [[ -z $commit_message ]]; then |
| 108 | + commit_message="publish: $commit_title" |
| 109 | + fi |
| 110 | + |
| 111 | + #append hash to commit message unless no hash flag was found |
| 112 | + if [ $append_hash = true ]; then |
| 113 | + commit_message="$commit_message"$'\n\n'"generated from commit $commit_hash" |
| 114 | + fi |
| 115 | + |
| 116 | + previous_branch=`git rev-parse --abbrev-ref HEAD` |
| 117 | + |
| 118 | + if [ ! -d "$deploy_directory" ]; then |
| 119 | + echo "Deploy directory '$deploy_directory' does not exist. Aborting." >&2 |
| 120 | + return 1 |
| 121 | + fi |
| 122 | + |
| 123 | + # must use short form of flag in ls for compatibility with OS X and BSD |
| 124 | + if [[ -z `ls -A "$deploy_directory" 2> /dev/null` && -z $allow_empty ]]; then |
| 125 | + echo "Deploy directory '$deploy_directory' is empty. Aborting. If you're sure you want to deploy an empty tree, use the --allow-empty / -e flag." >&2 |
| 126 | + return 1 |
| 127 | + fi |
| 128 | + |
| 129 | + if git ls-remote --exit-code $repo "refs/heads/$deploy_branch" ; then |
| 130 | + # deploy_branch exists in $repo; make sure we have the latest version |
| 131 | + |
| 132 | + disable_expanded_output |
| 133 | + git fetch --force $repo $deploy_branch:$deploy_branch |
| 134 | + enable_expanded_output |
| 135 | + fi |
| 136 | + |
| 137 | + # check if deploy_branch exists locally |
| 138 | + if git show-ref --verify --quiet "refs/heads/$deploy_branch" |
| 139 | + then incremental_deploy |
| 140 | + else initial_deploy |
| 141 | + fi |
| 142 | + |
| 143 | + restore_head |
| 144 | +} |
| 145 | + |
| 146 | +initial_deploy() { |
| 147 | + git --work-tree "$deploy_directory" checkout --orphan $deploy_branch |
| 148 | + git --work-tree "$deploy_directory" add --all |
| 149 | + commit+push |
| 150 | +} |
| 151 | + |
| 152 | +incremental_deploy() { |
| 153 | + #make deploy_branch the current branch |
| 154 | + git symbolic-ref HEAD refs/heads/$deploy_branch |
| 155 | + #put the previously committed contents of deploy_branch into the index |
| 156 | + git --work-tree "$deploy_directory" reset --mixed --quiet |
| 157 | + git --work-tree "$deploy_directory" add --all |
| 158 | + |
| 159 | + set +o errexit |
| 160 | + diff=$(git --work-tree "$deploy_directory" diff --exit-code --quiet HEAD --)$? |
| 161 | + set -o errexit |
| 162 | + case $diff in |
| 163 | + 0) echo No changes to files in $deploy_directory. Skipping commit.;; |
| 164 | + 1) commit+push;; |
| 165 | + *) |
| 166 | + echo git diff exited with code $diff. Aborting. Staying on branch $deploy_branch so you can debug. To switch back to master, use: git symbolic-ref HEAD refs/heads/master && git reset --mixed >&2 |
| 167 | + return $diff |
| 168 | + ;; |
| 169 | + esac |
| 170 | +} |
| 171 | + |
| 172 | +commit+push() { |
| 173 | + set_user_id |
| 174 | + git --work-tree "$deploy_directory" commit -m "$commit_message" |
| 175 | + |
| 176 | + disable_expanded_output |
| 177 | + #--quiet is important here to avoid outputting the repo URL, which may contain a secret token |
| 178 | + git push --quiet $repo $deploy_branch |
| 179 | + enable_expanded_output |
| 180 | +} |
| 181 | + |
| 182 | +#echo expanded commands as they are executed (for debugging) |
| 183 | +enable_expanded_output() { |
| 184 | + if [ $verbose ]; then |
| 185 | + set -o xtrace |
| 186 | + set +o verbose |
| 187 | + fi |
| 188 | +} |
| 189 | + |
| 190 | +#this is used to avoid outputting the repo URL, which may contain a secret token |
| 191 | +disable_expanded_output() { |
| 192 | + if [ $verbose ]; then |
| 193 | + set +o xtrace |
| 194 | + set -o verbose |
| 195 | + fi |
| 196 | +} |
| 197 | + |
| 198 | +set_user_id() { |
| 199 | + if [[ -z `git config user.name` ]]; then |
| 200 | + git config user.name "$default_username" |
| 201 | + fi |
| 202 | + if [[ -z `git config user.email` ]]; then |
| 203 | + git config user.email "$default_email" |
| 204 | + fi |
| 205 | +} |
| 206 | + |
| 207 | +restore_head() { |
| 208 | + if [[ $previous_branch = "HEAD" ]]; then |
| 209 | + #we weren't on any branch before, so just set HEAD back to the commit it was on |
| 210 | + git update-ref --no-deref HEAD $commit_hash $deploy_branch |
| 211 | + else |
| 212 | + git symbolic-ref HEAD refs/heads/$previous_branch |
| 213 | + fi |
| 214 | + |
| 215 | + git reset --mixed |
| 216 | +} |
| 217 | + |
| 218 | +filter() { |
| 219 | + sed -e "s|$repo|\$repo|g" |
| 220 | +} |
| 221 | + |
| 222 | +sanitize() { |
| 223 | + "$@" 2> >(filter 1>&2) | filter |
| 224 | +} |
| 225 | + |
| 226 | +[[ $1 = --source-only ]] || main "$@" |
0 commit comments