about summary refs log tree commit diff stats
path: root/tools/gitolite.html
diff options
context:
space:
mode:
authorSilvino Silva <silvino@bk.ru>2016-12-27 23:35:37 +0000
committerSilvino Silva <silvino@bk.ru>2017-01-12 03:35:54 +0000
commit4caa43d6aece6e8b13a9c325781c47809479e8e1 (patch)
treedceffdd2dd3fdf5acaa6102a8d43a21dfe652c1f /tools/gitolite.html
parentcb6dc0b2a4325611b32a22a7cdb36d7f42edb15e (diff)
downloaddoc-4caa43d6aece6e8b13a9c325781c47809479e8e1.tar.gz
new gitolite hook
Diffstat (limited to 'tools/gitolite.html')
-rw-r--r--tools/gitolite.html286
1 files changed, 185 insertions, 101 deletions
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 @@
 
         <h2 id="hooks">4. Gitolite Hooks</h2>
 
+        <p>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.</p>
+
+        <p>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.</p>
+
+        <p>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.</p>
+
+        <p>Package is created under gitolite
+        <a href="conf/srv/gitolite/hook.sh">/srv/gitolite/deploy/hook.sh</a>
+        script and
+        <a href="conf/srv/gitolite/deploy.sh">/srv/gitolite/deploy/deploy.sh</a>,
+        deploy in this example is called called by cron.</p>
+
+        <h3 id="gtl-activate">4.1. Activate Hooks</h3>
+
         <p>Example from
         <a href="http://gitolite.com/gitolite/cookbook.html#v3.6-variation-repo-specific-hooks">Cookbook</a>
         how to apply hooks only to certain repos. Uncomment or add
@@ -241,159 +263,221 @@
         $ gitolite setup
         </pre>
 
-        <h3 id="gtl-deploy">4.1. Deploy Hook</h3>
+        <h3 id="gtl-deploy">4.2. Deploy and Hook script</h3>
 
-        <p>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.</p>
+        <p>Create deploy directory on remote, /srv/gitolite/deploy
+        was chosen to have less impact on the package system;</p>
 
-        <p>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.</p>
+        <pre>
+        $ sudo -u gitolite mkdir /srv/gitolite/deploy
+        </pre>
+
+        <p>Script
+        <a href="conf/srv/gitolite/hook.sh">/srv/gitolite/deploy/hook.sh</a>
+        receives call create_package "project-name" "git-dir"
+        "valid oldrev/invalid" "newrev" "script/to/call.sh" from gitolite hook
+        and creates a package.</p>
+
+        <pre>
+        $ sudo -u gitolite cp conf/srv/gitolite/hook.sh /srv/gitolite/deploy/
+        </pre>
+
+        <p>Script
+        <a href="conf/srv/gitolite/deploy.sh">/srv/gitolite/deploy/deploy.sh</a>
+        loops for each package, extracts, order commit hashes to create final
+        snapshot of files and call script to handle deploy.</p>
+
+        <pre>
+        $ sudo -u gitolite cp conf/srv/gitolite/deploy.sh /srv/gitolite/deploy/
+        </pre>
+
+        <h3 id="gtl-setup">4.3. Setup project hook</h3>
+
+        <p>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.</p>
+
+        <h4>4.3.1. Hook Script</h4>
 
         <p>Create
-        <a href="conf/srv/gitolite/hook-deployweb">gitolite-admin/local/hooks/repo-specific/hook-deployweb</a>;</p>
+        <a href="conf/srv/gitolite/deploy-web-doc">gitolite-admin/local/hooks/repo-specific/deploy-web-doc</a>;</p>
 
         <pre>
         #!/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 &gt; /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" &gt; /srv/gitolite/deploy/${GL_REPO}
+        #echo "${SCRIPT_VARS}" &gt;&gt; /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
         </pre>
 
-        <p>Add scripts to the repos you want them to be active in
-        your conf file. For example:</p>
+        <p>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:</p>
 
         <pre>
         repo c9-doc
-            option hook.post-receive     =  hook-deployweb
+            config gitweb.description   =   "c9 documentation"
+            option hook.post-receive     =  deploy-web-doc
         </pre>
 
         <p>Add, commit, and push the admin repo;</p>
 
         <pre>
-        $ 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"
         </pre>
 
-        <p>Create deploy directory on remote;</p>
+        <p>Now we can test if our script is functioning by creating a branch on c9-doc
+        making a random change and push;<p>
 
         <pre>
-        # su - gitolite
-        $ mkdir deploy
+        $ cd c9-doc
+        $ git checkout -b deploy_branch
         </pre>
 
-        <p>On remote run;</p>
+        <h4>4.3.2. Deploy Script</h4>
+
+        <p>Create
+        <a href="conf/srv/gitolite/deploy-web.sh">/srv/gitolite/deploy/scripts/deploy-web.sh</a>;</p>
 
         <pre>
-        # su - gitolite
-        $ gitolite setup
-        </pre>
+        #!/bin/bash
 
-        <p>Create deploy script that cron will call
-        every minute, this script will check inside
-        /srv/gitolite/deploy folder for projects that have
-        been updated.</p>
+        pkg_path=$1
 
-        <p>Create <a href="conf/srv/gitolite/deployweb">/usr/share/gitolite/deployweb</a>;</p>
+        www_root="/srv/www"
+        www_user="nginx"
+        www_group="www"
 
-        <pre>
-        #!/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 &lt;${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 &lt;${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}
+        </pre>
 
-            echo "Deploy: setting directory permissions: chmod 755"
-            find $GIT_WORK_TREE -type d -print0 | xargs -0 chmod 755
+        <h4>4.3.3. Debuging hooks</h4>
 
-            echo "Deploy: setting file permissions: chmod 644"
-            find $GIT_WORK_TREE -type f -print0 | xargs -0 chmod 644
+        <p>Comment gitolite admin repo script "if" and uncomment debug sections, this allow to
+        source the file with environment of hook.</p>
 
-            # 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
+        <p>Later you can delete this branch locally and remote and start fresh. To test
+        if hook is called each time you push run;</p>
 
-            # Done with project
-            echo "Deploy: removing deploy file="$DP_FILE
-            rm $DP_FILE
 
-            exit 0;
-        done
+        <pre>
+        $ echo $(( ( RANDOM % 10 ) +1 )) >> index.html && git add -u && git commit -m "test deploy" && git push git
         </pre>
 
+        <p>See if a file was created in /srv/gitolite/deploy with name of project and
+        with environmental variables of gitolite script.</p>
+
+        <p>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.</p>
+
+        <h4 id="gtl-deploy">4.4. Deploy with Cron</h4>
+
         <p>Add cron job to call deploy script every minute;</p>
 
         <pre>
@@ -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);