From 4caa43d6aece6e8b13a9c325781c47809479e8e1 Mon Sep 17 00:00:00 2001 From: Silvino Silva Date: Tue, 27 Dec 2016 23:35:37 +0000 Subject: new gitolite hook --- tools/conf/srv/gitolite/deploy-web-doc | 42 +++++ tools/conf/srv/gitolite/deploy-web.sh | 75 +++++++++ tools/conf/srv/gitolite/deploy.sh | 175 ++++++++++++++++++++ tools/conf/srv/gitolite/deployweb | 74 --------- tools/conf/srv/gitolite/hook-deployweb | 22 --- tools/conf/srv/gitolite/hook.sh | 95 +++++++++++ tools/gitolite.html | 286 +++++++++++++++++++++------------ 7 files changed, 572 insertions(+), 197 deletions(-) create mode 100755 tools/conf/srv/gitolite/deploy-web-doc create mode 100644 tools/conf/srv/gitolite/deploy-web.sh create mode 100755 tools/conf/srv/gitolite/deploy.sh delete mode 100755 tools/conf/srv/gitolite/deployweb delete mode 100755 tools/conf/srv/gitolite/hook-deployweb create mode 100644 tools/conf/srv/gitolite/hook.sh (limited to 'tools') diff --git a/tools/conf/srv/gitolite/deploy-web-doc b/tools/conf/srv/gitolite/deploy-web-doc new file mode 100755 index 0000000..ae8e2db --- /dev/null +++ b/tools/conf/srv/gitolite/deploy-web-doc @@ -0,0 +1,42 @@ +#!/bin/bash +###################################################################### +# +# Put this file in your gitolite-admin; +# ~/gitolite-admin/local/hooks/repo-specific/deploy-web-doc +# +# set host to empty to create package for each push +# or set remote host to create package based on last deployed push +# host="https://doc.localhost" +host="" +# set name of witch branch should be deployed +branch_to_deploy="deploy_branch" + +###################################################################### + + +url="$host/.last_deploy" +source /srv/gitolite/deploy/hook.sh +read oldrev newrev refname +push_branch=$(git rev-parse --symbolic --abbrev-ref $refname) + +#SCRIPT_VARS=$(set) +#echo "project: $PROJECT" +#echo "local dir: $PWD" > /srv/gitolite/deploy/${GL_REPO} +#echo "${SCRIPT_VARS}" >> /srv/gitolite/deploy/${GL_REPO} + +if [[ $push_branch = $branch_to_deploy ]]; then + + # if host empty we make local tracking + if [[ $host = "" ]]; then + if [[ $(is_initial ${GL_REPO}) = "true" ]]; then + oldrev="initial" + fi + else + if [[ ! $(valid_url $url) = "true" ]]; then + echo "Deploy: set $url on remote to start creating packages" + exit 1 + fi + oldrev=$(get_remote_rev $url) + fi + create_package ${GL_REPO} ${PWD} ${oldrev} ${newrev} "deploy-web.sh" +fi diff --git a/tools/conf/srv/gitolite/deploy-web.sh b/tools/conf/srv/gitolite/deploy-web.sh new file mode 100644 index 0000000..01e92ac --- /dev/null +++ b/tools/conf/srv/gitolite/deploy-web.sh @@ -0,0 +1,75 @@ +#!/bin/bash + +pkg_path=$1 + +www_root="/srv/www" +www_user="nginx" +www_group="www" + +pkg_file="${pkg_path}/project" +pkg_rm="${pkg_path}/deleted" +pkg_files="${pkg_path}/files" + +if [ ! -f ${pkg_file} ]; then + echo "Deploy web: invalid pkg_file ${pkg_file}" + exit 1 +fi + +pkg_name=$(head -1 ${pkg_file}) +pkg_new=$(head -3 ${pkg_file} | tail -1) +pkg_new7=$(echo ${pkg_new} | cut -c1-7) + +pkg_www="${www_root}/${pkg_name}" +pkg_back="${pkg_www}/backup_deploy" +pkg_last="${pkg_www}/.last_deploy" + +if [ ! -d ${pkg_www} ]; then + echo "Deploy web: invalid pkg_www ${pkg_www}" + exit 1 +fi + +# first backup all data +if [[ ! $(ls ${pkg_www} | grep -v "backup_deploy") = "" ]]; then + if [ ! -d ${pkg_back} ]; then + sudo -u ${www_user} mkdir -p ${pkg_back} + fi + backup_file="${pkg_back}/${pkg_name}-$(date '+%Y-%j-%H-%M-%S').tar.gz" + echo "Deploy web: making backup ${backup_file}" + sudo -u ${www_user} tar --exclude ${pkg_back} --xattrs -zcpf ${backup_file} ${pkg_www} +fi + +# remove files and directories that have been deleted +if [ -f ${pkg_rm} ]; then + + echo "Deploy web: files to delete:" + # first we delete files + while read deleted_file; do + deleted_file="${pkg_www}/${deleted_file}" + if [ -f ${deleted_file} ]; then + echo "file rm ${deleted_file}" + rm ${deleted_file} + fi + done <${pkg_rm} + + # delete directories + while read deleted_file; do + deleted_file="${pkg_www}/${deleted_file}" + if [ -d ${deleted_file} ]; then + echo "file rm ${deleted_file}" + rm ${deleted_file} + fi + done <${pkg_rm} + +fi + +# copy new files to destination +if [ -d ${pkg_files} ]; then + echo "Deploy web: cp from ${pkg_files} to ${pkg_www}" + sudo -u ${www_user} cp -r ${pkg_files}/* ${pkg_www} +fi + +echo ${pkg_new} > ${pkg_last} +echo "Deploy: scripts/deployweb.sh ${pkg_name} ${pkg_new7} deployed." + +#remove temporary package +rm -r ${pkg_path} diff --git a/tools/conf/srv/gitolite/deploy.sh b/tools/conf/srv/gitolite/deploy.sh new file mode 100755 index 0000000..df11f4a --- /dev/null +++ b/tools/conf/srv/gitolite/deploy.sh @@ -0,0 +1,175 @@ +#!/bin/bash + +# origin package directory +packages_dir="/srv/gitolite/deploy/packages" +# temporary work directory +deploy_dir="/srv/gitolite/deploy/deploy_dir" +# scripts to deploy packages +deploy_scripts="/srv/gitolite/deploy/scripts" + +function get_script(){ + # receives package path return script to call + local pkg_path=$1 + echo $(head -2 ${pkg_path}/project | tail -1) +} + +function get_new(){ + # receives package path return commit hash (new) + local pkg_path=$1 + echo $(head -3 ${pkg_path}/project | tail -1) +} + +function get_dep(){ + # receives package path return previews commit hash (old) + local pkg_path=$1 + + new=$(head -3 ${pkg_path}/project | tail -1) + old=$(head -4 ${pkg_path}/project | tail -1) + + if [[ ! ${new} = ${old} ]]; then + echo ${old} | cut -c1-7 + fi +} + +function project_extract(){ + + # project directory containing extracted packages + local prj_dir=$1 + + # final extracted package + local prj_pkg="${prj_dir}/package" + + # temporary vars for swapping/iterating pkg_news + local pkg_new="" + local pkg_old="" + local pkg_dir="" + local pkg_temp="" + local pkg_next=1 + local pkg_del="" + local x=0 + local y=0 + + # array with all the news hashes + local pkg_news=($(ls ${prj_dir})) + + # total new packages + local total=${#pkg_news[@]} + + echo "Deploy: $(basename ${prj_dir}) extracting packages ${pkg_news[*]}" + + # find first package + for pkg_new in ${pkg_news[@]} + do + # get package dependency + pkg_dir="${prj_dir}/${pkg_new}" + pkg_old=$(get_dep ${pkg_dir}) + if [[ ! " ${pkg_news[@]} " =~ " ${pkg_old} " ]]; then + # pkg_news don't contain package + # we found initial package + pkg_temp=${pkg_news[0]} + pkg_news[0]=${pkg_new} + pkg_news[${x}]=${pkg_temp} + break + fi + x=$((${x}+1)) + done + + # Order packages by dependency start with first package + for (( y=0; y<${total}; y++ )) + do + pkg_next=$(($y+1)) + if [[ ${pkg_next} = ${total} ]]; then + ## we are in the last one + break + fi + + pkg_new=${pkg_news[$y]} + for (( x=pkg_next; x<${total}; x++ )) + do + pkg_dir="${prj_dir}/${pkg_news[${x}]}" + pkg_old=$(get_dep ${pkg_dir}) + # is dependent on current + if [[ ${pkg_old} = ${pkg_new} ]]; then + pkg_temp=${pkg_news[${pkg_next}]} + pkg_news[${pkg_next}]=${pkg_news[${x}]} + pkg_news[${x}]=${pkg_temp} + # we can break and pass next one + break + fi + done + done + + # create project final package directory + mkdir -p ${prj_pkg}/files + + # copy project information of last commit + cp ${prj_dir}/${pkg_news[$((${total}-1))]}/project ${prj_pkg} + + # now that packages are ordered we can start creating files + for pkg_new in ${pkg_news[@]} + do + pkg_dir=${prj_dir}/${pkg_new} + tar xf ${pkg_dir}/files.tar.xz \ + --directory ${prj_pkg}/files + + # if deleted files exists + if [ -f "${pkg_dir}/deleted" ]; then + # first collect all files/directories don't exist + while read pkg_del; do + # if file don't exist add entry to project deleted file + pkg_temp="${prj_pkg}/files/${pkg_del}" + if [ ! -f ${pkg_temp} ]; then + if [ ! -d ${pkg_temp} ]; then + # is not a file or directory from previous packages + echo ${pkg_del} >> ${prj_pkg}/deleted + fi + fi + done <${prj_dir}/${pkg_new}/deleted + + # delete directories and files + while read pkg_del; do + pkg_temp="${prj_pkg}/files/${pkg_del}" + if [ -d ${pkg_temp} ]; then + rm -r ${pkg_temp} + elif [ -f ${pkg_temp} ]; then + rm ${pkg_temp} + fi + done <${prj_dir}/${pkg_new}/deleted + fi + + #remove temporary directory + rm -r ${prj_dir}/${pkg_new} + done + + # call project deploy script + call_script=${deploy_scripts}/$(get_script $prj_pkg) + echo "Deploy: calling deploy script: ${call_script}" + /bin/bash ${call_script} ${prj_pkg} + +} + +if [[ ! $(ls ${deploy_dir}) = "" ]]; then + rm -r ${deploy_dir}/* +fi + +# first extract all packages from origin directory +for pkg_path in `find ${packages_dir} -type f -name "*.tar.gz"` +do + if [ -f ${pkg_path} ]; then + pkg_name=$(basename ${pkg_path}) + pkg_proj=$(echo ${pkg_name} | cut -d "_" -f 1) + pkg_new7=$(echo ${pkg_name} | tail -c -15 | cut -c -7) + pkg_temp=${deploy_dir}/${pkg_proj}/${pkg_new7} + mkdir -p ${pkg_temp} + tar xf ${pkg_path} --directory ${pkg_temp} + rm ${pkg_path} + fi +done + +# loop for all projects and deploy them +for prj_dir in `find ${deploy_dir} -maxdepth 1 -mindepth 1 -type d` +do + # order index of hashes based on old commit + echo "prj_dir $prj_dir" + project_extract ${prj_dir} +done diff --git a/tools/conf/srv/gitolite/deployweb b/tools/conf/srv/gitolite/deployweb deleted file mode 100755 index 5a18ed1..0000000 --- a/tools/conf/srv/gitolite/deployweb +++ /dev/null @@ -1,74 +0,0 @@ -#!/bin/sh - -###################################################################### -# -# Put this file in; -# /usr/share/gitolite/deployweb -# -DIR_WWW=/srv/www/ -DEPLOY_BRANCH=deployweb -TARGET_USER=nginx - -for DP_FILE in /srv/gitolite/deploy/* -do - - if [ ! -f "$DP_FILE" ]; then - # Nothing to do ;) - #echo "Deploy: invalid DP_FILE" - exit 1; - fi - - # Get project name - PROJECT=$(basename "$DP_FILE") - echo "Deploy: PROJECT=${PROJECT}" - - # Get git repository path and verify if exists - DIR_GIT=$(head -n 1 $DP_FILE) - if [ ! -d "$DIR_GIT" ]; then - echo "Deploy: invalid DIR_GIT: ${DIR_GIT}" - exit 2; - fi - echo "Deploy: DIR_GIT=${DIR_GIT}" - - # Get directory to deploy and verify if exists - GIT_WORK_TREE=${DIR_WWW}${PROJECT}/ - if [ ! -d "$GIT_WORK_TREE" ]; then - echo "Deploy: invalid GIT_WORK_TREE: ${GIT_WORK_TREE}" - echo "Deploy: creating directory: $GIT_WORK_TREE}" - mkdir -p $GIT_WORK_TREE - fi - echo "Deploy: GIT_WORK_TREE={$GIT_WORK_TREE}" - - # Deploy (checkout) - echo "Deploy: starting git checkout" - - git --git-dir=$DIR_GIT \ - --work-tree=$GIT_WORK_TREE \ - checkout -f $DEPLOY_BRANCH - - - # Fix ownership and permissions - echo "Deploy: fixing permissions" - - echo "Deploy: setting owner: chown -R ${TARGET_USER}" - chown -R ${TARGET_USER}:${TARGET_USER} $GIT_WORK_TREE - - echo "Deploy: setting directory permissions: chmod 755" - find $GIT_WORK_TREE -type d -print0 | xargs -0 chmod 755 - - echo "Deploy: setting file permissions: chmod 644" - find $GIT_WORK_TREE -type f -print0 | xargs -0 chmod 644 - - # Call project script - if [ -f "${GIT_WORK_TREE}/deploy.sh" ]; then - echo "Deploy: calling ${GIT_WORK_TREE}deploy.sh" - cd ${GIT_WORK_TREE} - sudo -u ${TARGET_USER} sh ${GIT_WORK_TREE}deploy.sh - fi - - # Done with project - echo "Deploy: removing deploy file="$DP_FILE - rm $DP_FILE - - exit 0; -done diff --git a/tools/conf/srv/gitolite/hook-deployweb b/tools/conf/srv/gitolite/hook-deployweb deleted file mode 100755 index 1a32bd9..0000000 --- a/tools/conf/srv/gitolite/hook-deployweb +++ /dev/null @@ -1,22 +0,0 @@ -#!/bin/sh - -###################################################################### -# -# Put this file in your gitolite-admin; -# ~/gitolite-admin/local/hooks/repo-specific/hook-deployweb -# -while read oldrev newrev refname -do - BRANCH=$(git rev-parse --symbolic --abbrev-ref $refname) - echo "Commit was for branch $BRANCH" - - if [ "$BRANCH" = "deployweb" ]; then - - # Get project name from current directory (without .git) - PROJECT=$(basename "$PWD") - PROJECT=${PROJECT%.git} - - echo "Project $PROJECT added to deploy list." - echo $PWD > /srv/gitolite/deploy/$PROJECT - fi -done diff --git a/tools/conf/srv/gitolite/hook.sh b/tools/conf/srv/gitolite/hook.sh new file mode 100644 index 0000000..1f977ca --- /dev/null +++ b/tools/conf/srv/gitolite/hook.sh @@ -0,0 +1,95 @@ +#!/bin/bash + +# final packages dir +packages_dir="/srv/gitolite/deploy/packages" +# hook work directory +hook_dir="/srv/gitolite/deploy/hook_dir" + +function is_initial(){ + local prj_name=$1 + if [ ! -d ${hook_dir}/${prj_name} ]; then + echo "true" + else + echo "false" + fi +} + +function get_remote_rev(){ + echo $(wget --no-check-certificate -qO- $1) +} + +function valid_url(){ + if [[ `wget -S --spider $1 --no-check-certificate 2>&1 | grep 'HTTP/1.1 200 OK'` ]]; + then + echo "true"; + fi +} + +function create_package(){ + # project name + local prj_name=$1 + # git repository directory + local git_dir=$2 + # last/old commit revision + local pkg_old=$3 + # new commit revision + local pkg_new=$4 + # script deploy call when extracting this package + local pkg_script=$5 + + local pkg_new7=$(echo $pkg_new | cut -c1-7) + + # project directory + local prj_dir="${hook_dir}/${prj_name}" + # package directory + local pkg_dir="${prj_dir}/${pkg_new7}" + # final tar file + local pkg_tar="${packages_dir}/${prj_name}_${pkg_new7}.tar.gz" + + # if temporary work directory exists maybe other process is creating packages + if [ -d "$pkg_dir" ]; then + echo "Deploy: temporary directory ${pkg_dir} exists, maybe other precess" + exit 1 + fi + # create temporary directory for this package + mkdir -p ${pkg_dir} + + echo "Deploy: ${prj_name} ${pkg_new7} package call ${pkg_script} on deploy." + + # save metadata to be used by deploy script + echo $prj_name > ${pkg_dir}/project + echo $pkg_script >> ${pkg_dir}/project + echo $pkg_new >> ${pkg_dir}/project + + # if is a valid old commit create a package with changes since then + # else create a full package (all files) + local is_commit=$(git --git-dir=${git_dir} cat-file -t ${pkg_old} 2>&1) + if [[ $is_commit = "commit" ]]; then + echo "Deploy: creating package from old commit." + # list with files to extract (Added Copied Modified Renamed) + file_list=$(git --git-dir=${git_dir} --no-pager diff \ + --diff-filter=ACMR \ + --name-only ${pkg_old} ${pkg_new}) + + # create tar archive with same name as commit hash with files + git --git-dir=${git_dir} archive -o ${pkg_dir}/files.tar.xz ${pkg_new} ${file_list} + + # first we create list of files to be removed + git --git-dir=${git_dir} --no-pager diff \ + --diff-filter=DR \ + --name-status -t ${pkg_old} ${pkg_new} | cut -f 2 > ${pkg_dir}/deleted + + # save old commit on metadata + echo $pkg_old >> ${pkg_dir}/project + else + echo "Deploy: creating initial package." + git --git-dir=${git_dir} archive -o ${pkg_dir}/files.tar.xz ${pkg_new} + + fi + + tar -zcpf ${pkg_tar} --directory=${pkg_dir} . + + echo "Deploy: package ${pkg_tar} ready !" + rm -r ${pkg_dir} + return 0 +} diff --git a/tools/gitolite.html b/tools/gitolite.html index 8083ca0..2fcc67e 100644 --- a/tools/gitolite.html +++ b/tools/gitolite.html @@ -204,6 +204,28 @@

4. Gitolite Hooks

+

This document creates three scripts, one is run when gitolite receives + push to a project with hook active, second script is run under root + user to allow operations where gitolite user have no rights, third one + is project specific.

+ +

This example try to have a separate creation of a package and its deployment, + in case deploy script is not on the same machine other method can be used to send + the package.

+ +

A normal package will have a files.tar with all or new files to extract, + if necessary a deleted file with the list of files to be removed and a + project file with data about the package like new hash commit, or witch + script to call to deploy.

+ +

Package is created under gitolite + /srv/gitolite/deploy/hook.sh + script and + /srv/gitolite/deploy/deploy.sh, + deploy in this example is called called by cron.

+ +

4.1. Activate Hooks

+

Example from Cookbook how to apply hooks only to certain repos. Uncomment or add @@ -241,159 +263,221 @@ $ gitolite setup -

4.1. Deploy Hook

+

4.2. Deploy and Hook script

-

This manual create two users; one gitolite that handle git - central server and system www for web servers. To avoid permission - problems this example use gitolite hooks and cron. By using cron - we have permission to use chown, this way files end up with right - www user ownership and permissions.

+

Create deploy directory on remote, /srv/gitolite/deploy + was chosen to have less impact on the package system;

-

This hook allows to select wich branch is deployed and if exists, - calls a script inside project folder with user www. This allows to - do post deploy (checkout) tasks such as composer update.

+
+        $ sudo -u gitolite mkdir /srv/gitolite/deploy
+        
+ +

Script + /srv/gitolite/deploy/hook.sh + receives call create_package "project-name" "git-dir" + "valid oldrev/invalid" "newrev" "script/to/call.sh" from gitolite hook + and creates a package.

+ +
+        $ sudo -u gitolite cp conf/srv/gitolite/hook.sh /srv/gitolite/deploy/
+        
+ +

Script + /srv/gitolite/deploy/deploy.sh + loops for each package, extracts, order commit hashes to create final + snapshot of files and call script to handle deploy.

+ +
+        $ sudo -u gitolite cp conf/srv/gitolite/deploy.sh /srv/gitolite/deploy/
+        
+ +

4.3. Setup project hook

+ +

Project hooks create a package by calling hook.sh script and + deploy a package being called by deploy.sh. Deploy script is a + simple example that handle multiple web projects.

+ +

4.3.1. Hook Script

Create - gitolite-admin/local/hooks/repo-specific/hook-deployweb;

+ gitolite-admin/local/hooks/repo-specific/deploy-web-doc;

         #!/bin/bash
-
         ######################################################################
         #
         # Put this file in your gitolite-admin;
-        # ~/gitolite-admin/local/hooks/repo-specific/hook-deployweb
+        # ~/gitolite-admin/local/hooks/repo-specific/deploy-web-doc
         #
-        while read oldrev newrev refname
-        do
-                BRANCH=$(git rev-parse --symbolic --abbrev-ref $refname)
-                echo "Commit was for branch $BRANCH"
+        # set host to empty to create package for each push
+        # or set remote host to create package based on last deployed push
+        # host="https://doc.localhost"
+        host=""
+        # set name of witch branch should be deployed
+        branch_to_deploy="deploy_branch"
 
-                if [[ "$BRANCH" == "master" ]];then
+        ######################################################################
 
-                        # Get project name from current directory (without .git)
-                        PROJECT=$(basename "$PWD")
-                        PROJECT=${PROJECT%.git}
 
-                        echo "Project $PROJECT added to deploy list."
-                        echo $PWD > /srv/gitolite/deploy/$PROJECT
-                fi
+        url="$host/.last_deploy"
+        source /srv/gitolite/deploy/hook.sh
+        read oldrev newrev refname
+        push_branch=$(git rev-parse --symbolic --abbrev-ref $refname)
+
+        #SCRIPT_VARS=$(set)
+        #echo "project: $PROJECT"
+        #echo "local dir: $PWD" > /srv/gitolite/deploy/${GL_REPO}
+        #echo "${SCRIPT_VARS}" >> /srv/gitolite/deploy/${GL_REPO}
 
-        done
+        if [[ $push_branch = $branch_to_deploy  ]]; then
+
+            # if host empty we make local tracking
+            if [[ $host = "" ]]; then
+                if [[ $(is_initial ${GL_REPO}) = "true" ]]; then
+                    oldrev="initial"
+                fi
+            else
+                if [[ ! $(valid_url $url) = "true" ]]; then
+                    echo "Deploy: set $url on remote to start creating packages"
+                    exit 1
+                fi
+                oldrev=$(get_remote_rev $url)
+            fi
+            create_package ${GL_REPO} ${PWD} ${oldrev} ${newrev} "deploy-web.sh"
+        fi
         
-

Add scripts to the repos you want them to be active in - your conf file. For example:

+

Activate this hook, the idea is to start with this one as a template working + and then implement the final one. Edit gitolite admin configuration file and + activate:

         repo c9-doc
-            option hook.post-receive     =  hook-deployweb
+            config gitweb.description   =   "c9 documentation"
+            option hook.post-receive     =  deploy-web-doc
         

Add, commit, and push the admin repo;

-        $ git add -u && git commit -m "deploy hook"
+        $ git add local/hooks/repo-specific/hook-deployweb
+        $ git add -u && git commit -m "added deploy c9 hook"
         
-

Create deploy directory on remote;

+

Now we can test if our script is functioning by creating a branch on c9-doc + making a random change and push;

-        # su - gitolite
-        $ mkdir deploy
+        $ cd c9-doc
+        $ git checkout -b deploy_branch
         
-

On remote run;

+

4.3.2. Deploy Script

+ +

Create + /srv/gitolite/deploy/scripts/deploy-web.sh;

-        # su - gitolite
-        $ gitolite setup
-        
+ #!/bin/bash -

Create deploy script that cron will call - every minute, this script will check inside - /srv/gitolite/deploy folder for projects that have - been updated.

+ pkg_path=$1 -

Create /usr/share/gitolite/deployweb;

+ www_root="/srv/www" + www_user="nginx" + www_group="www" -
-        #!/bin/sh
+        pkg_file="${pkg_path}/project"
+        pkg_rm="${pkg_path}/deleted"
+        pkg_files="${pkg_path}/files"
 
-        ######################################################################
-        #
-        # Put this file in;
-        # /usr/share/gitolite/deployweb
-        #
-        DIR_WWW=/srv/www/
-        DEPLOY_BRANCH=master
-        TARGET_USER=www
+        if [ ! -f ${pkg_file} ]; then
+            echo "Deploy web: invalid pkg_file ${pkg_file}"
+            exit 1
+        fi
 
-        for DP_FILE in /srv/gitolite/deploy/*
-        do
+        pkg_name=$(head -1 ${pkg_file})
+        pkg_new=$(head -3 ${pkg_file} | tail -1)
+        pkg_new7=$(echo ${pkg_new} | cut -c1-7)
 
-            if [ ! -f "$DP_FILE" ]; then
-                # Nothing to do ;)
-                #echo "Deploy: invalid DP_FILE"
-                exit 1;
-            fi
+        pkg_www="${www_root}/${pkg_name}"
+        pkg_back="${pkg_www}/backup_deploy"
+        pkg_last="${pkg_www}/.last_deploy"
 
-            # Get project name
-            PROJECT=$(basename "$DP_FILE")
-            echo "Deploy: PROJECT=${PROJECT}"
+        if [ ! -d ${pkg_www} ]; then
+            echo "Deploy web: invalid pkg_www ${pkg_www}"
+            exit 1
+        fi
 
-            # Get git repository path and verify if exists
-            DIR_GIT=$(head -n 1 $DP_FILE)
-            if [ ! -d "$DIR_GIT" ]; then
-                echo "Deploy: invalid DIR_GIT: ${DIR_GIT}"
-                exit 2;
-            fi
-            echo "Deploy: DIR_GIT=${DIR_GIT}"
-
-            # Get directory to deploy and verify if exists
-            GIT_WORK_TREE=${DIR_WWW}${PROJECT}/
-            if [ ! -d "$GIT_WORK_TREE" ]; then
-                echo "Deploy: invalid GIT_WORK_TREE: ${GIT_WORK_TREE}"
-                echo "Deploy: creating directory: $GIT_WORK_TREE}"
-                mkdir -p $GIT_WORK_TREE
+        # first backup all data
+        if [[ ! $(ls ${pkg_www} | grep -v "backup_deploy") = "" ]]; then
+            if [ ! -d ${pkg_back} ]; then
+                sudo -u ${www_user} mkdir -p ${pkg_back}
             fi
-            echo "Deploy: GIT_WORK_TREE={$GIT_WORK_TREE}"
-
-            # Deploy (checkout)
-            echo "Deploy: starting git checkout"
+            backup_file="${pkg_back}/${pkg_name}-$(date '+%Y-%j-%H-%M-%S').tar.gz"
+            echo "Deploy web: making backup ${backup_file}"
+            sudo -u ${www_user} tar --exclude ${pkg_back} --xattrs -zcpf ${backup_file} ${pkg_www}
+        fi
+
+        # remove files and directories that have been deleted
+        if [ -f ${pkg_rm} ]; then
+
+            echo "Deploy web: files to delete:"
+            # first we delete files
+            while read deleted_file; do
+                deleted_file="${pkg_www}/${deleted_file}"
+                if [ -f ${deleted_file} ]; then
+                    echo "file      rm ${deleted_file}"
+                    rm ${deleted_file}
+                fi
+            done <${pkg_rm}
+
+            # delete directories
+            while read deleted_file; do
+                deleted_file="${pkg_www}/${deleted_file}"
+                if [ -d ${deleted_file} ]; then
+                    echo "file      rm ${deleted_file}"
+                    rm ${deleted_file}
+                fi
+            done <${pkg_rm}
 
-            git --git-dir=$DIR_GIT \
-                --work-tree=$GIT_WORK_TREE \
-                checkout -f $DEPLOY_BRANCH
+        fi
 
+        # copy new files to destination
+        if [ -d ${pkg_files} ]; then
+            echo "Deploy web: cp from ${pkg_files} to ${pkg_www}"
+            sudo -u ${www_user} cp -r ${pkg_files}/* ${pkg_www}
+        fi
 
-            # Fix ownership and permissions
-            echo "Deploy: fixing permissions"
+        echo ${pkg_new} > ${pkg_last}
+        echo "Deploy: scripts/deployweb.sh ${pkg_name} ${pkg_new7} deployed."
 
-            echo "Deploy: setting owner: chown -R ${TARGET_USER}"
-            chown -R ${TARGET_USER}:${TARGET_USER} $GIT_WORK_TREE
+        #remove temporary package
+        rm -r ${pkg_path}
+        
- echo "Deploy: setting directory permissions: chmod 755" - find $GIT_WORK_TREE -type d -print0 | xargs -0 chmod 755 +

4.3.3. Debuging hooks

- echo "Deploy: setting file permissions: chmod 644" - find $GIT_WORK_TREE -type f -print0 | xargs -0 chmod 644 +

Comment gitolite admin repo script "if" and uncomment debug sections, this allow to + source the file with environment of hook.

- # Call project script - if [ -f "${GIT_WORK_TREE}/deploy.sh" ]; then - echo "Deploy: calling ${GIT_WORK_TREE}deploy.sh" - cd ${GIT_WORK_TREE} - sudo -u ${TARGET_USER} sh ${GIT_WORK_TREE}deploy.sh - fi +

Later you can delete this branch locally and remote and start fresh. To test + if hook is called each time you push run;

- # Done with project - echo "Deploy: removing deploy file="$DP_FILE - rm $DP_FILE - exit 0; - done +
+        $ echo $(( ( RANDOM % 10 ) +1 )) >> index.html && git add -u && git commit -m "test deploy" && git push git
         
+

See if a file was created in /srv/gitolite/deploy with name of project and + with environmental variables of gitolite script.

+ +

From now on you can test changes directly on + /srv/gitolite/.gitolite/local/hooks/repo-specific/hook-deployweb + and repeat above command to see the results or create a separate script with + all variables generated by above script set so you don't have to push at all.

+ +

4.4. Deploy with Cron

+

Add cron job to call deploy script every minute;

@@ -434,7 +518,7 @@
 
         our $projects_list = "/srv/gitolite/projects.list";
         # The directories where your projects are. Must not end with a slash.
-        our $projectroot = "/srv/gitolite/repositories"; 
+        our $projectroot = "/srv/gitolite/repositories";
 
         # Base URLs for links displayed in the web interface.
         our @git_base_url_list = qw(git://core.privat-network.com http://git@core.private-network.com);
-- 
cgit 1.4.1-2-gfad0