summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAaron Ball <nullspoon@iohq.net>2015-07-17 00:02:31 -0600
committerAaron Ball <nullspoon@iohq.net>2015-07-17 00:02:31 -0600
commit3220712525a0ed989a7116a9aebd609afea41997 (patch)
tree622c49048addd04e2c898cef7734b1547bac93b3
downloadgit-deploy-cache-3220712525a0ed989a7116a9aebd609afea41997.tar.gz
git-deploy-cache-3220712525a0ed989a7116a9aebd609afea41997.tar.xz
Initial commit of git-deploy-cache
Currently deploys a bare git repo for the specified branch into a deployment base path. It also sets up and exports varios environmental variables for later script use. Still needs to be able to delete those paths of the branch is deleted. Lots of work to do from here.
-rwxr-xr-xgit-deploy-cache.sh114
1 files changed, 114 insertions, 0 deletions
diff --git a/git-deploy-cache.sh b/git-deploy-cache.sh
new file mode 100755
index 0000000..8540cf3
--- /dev/null
+++ b/git-deploy-cache.sh
@@ -0,0 +1,114 @@
+#!/usr/bin/env bash
+#
+# == Description
+#
+# This script deploys the given bare git repo and branch to a working location.
+# It decides what needs to be deployed based on the variables received from a
+# git post-receive hook via STDIN.
+#
+# @param oldref SHA1 hash of commit being advanced from
+# @param newref SHA1 hash of most recent git commit
+# @param refname Git name for ref. This is used to derive the branch name
+# commited to
+#
+
+
+#
+# Sets up some handy variables for a git post-receive script to use.
+#
+# == Variables
+#
+# REPOSITORY: Repository that was pushed to
+# BRANCH: Name of the branch that was modified
+# FILES_A: List of added files
+# FILES_M: List of modified files
+# FILES_D: List of deleted files
+#
+function enviro_setup {
+ oldrev=${1}
+ newrev=${2}
+ refname=${3}
+
+ # Repository containing the branch
+ export REPOSITORY=$(basename $(pwd))
+
+ # Name of the branch pushed to
+ export BRANCH=${refname##*/}
+
+ # Command to list files affected by this commit
+ file_cmd="git diff --name-status ${oldrev} ${newrev}"
+
+ # List of Added files
+ export FILES_A=$(${file_cmd} | grep '^A' | cut -d ' ' -f 2)
+ # List of Modified files
+ export FILES_M=$(${file_cmd} | grep '^M' | cut -d ' ' -f 2)
+ # List of Deleted files
+ export FILES_D=$(${file_cmd} | grep '^D' | cut -d ' ' -f 2)
+ # List of all files changed (added or modified) but not deleted
+ # Short for files_existing
+ export FILES_E=$(${file_cmd} | grep '^[AM]' | cut -d ' ' -f 2)
+}
+
+
+#
+# Logging function for everything here
+#
+function log {
+ echo "$(date '+%s'): ${@}" >> ~/gitposthook.out
+}
+
+
+#
+# Deploys a working copy of the given repo and branch to the specified location
+#
+# @param remote Bare remote to be deployed locally
+# @param branch Branch to be deployed
+# @param dest Destination location to deploy the working copy to
+#
+function deploy_cache {
+ remote=${1}
+ # Strip trailing / if one exists (this throws off our later parse of the path
+ # to get the name of the repo
+ if [[ ${remote:${#remote} - 1} == '/' ]]; then
+ remote=${remote:0:-1}
+ fi
+
+ branch=${2}
+ dest=${3}
+
+ repo=${remote##*/}
+ deploy=${dest}/${repo}/${branch}
+
+ if [[ -d ${deploy} ]]; then
+ log "Deleting ${deploy}"
+ rm -rf ${deploy}
+ fi
+
+ log "Deploying to ${deploy}"
+ mkdir -p ${deploy}
+ cd ${deploy}
+ log "Checkout branch ${branch}"
+ git clone -l -b ${branch} ${remote} ./
+ cd -
+}
+
+
+#
+# Main function
+#
+function main {
+ # Read STDIN
+ read oldrev newrev refname
+ deploy_base="${1}"
+
+ # Set up global enviro variables that will be of use later
+ enviro_setup ${oldrev} ${newrev} ${refname}
+
+ log "Deployment triggered by ${newrev}"
+
+ # Deploy a working copy of the repo so we have something to work on
+ deploy_cache $(pwd) ${BRANCH} ${deploy_base}
+}
+
+main ${@}
+#${oldrev} ${newrev} ${refname}

Generated by cgit