Tools Index

Gitolite

1. Install Gitolite

Install Gitolite port first;

        $ prt-get depinst gitolite
        

2. Configure Gitolite

Create user and home directory;

        # mkdir -p /srv/gitolite
        # useradd -r -s /bin/bash -m -d /srv/gitolite gitolite
        # chown gitolite:gitolite /srv/gitolite
        

Password is necessary so the user is not locked and can login via ssh. This password will not be used.

        # passwd gitolite
        

Copy public key that will be used for authenticating gitolite administrator, read ssh how to create one, key don't need to have password. If the server is on remote a remote machine you can use scp to copy the public key;

        $ scp /home/bob/.ssh/gitolite.pub admin@core.privat-network.net:/home/admin/
        bob@core.privat-network.net's password:
        gitolite.pub                              100%  390     0.4KB/s   00:00
        

Then on remote machine;

        # cp /home/admin/gitolite.pub /srv/gitolite.pub
        
        # su - gitolite
        $ gitolite setup -pk gitolite.pub
        Initialized empty Git repository in /srv/gitolite/repositories/gitolite-admin.git/
        Initialized empty Git repository in /srv/gitolite/repositories/testing.git/
        WARNING: /srv/gitolite/.ssh missing; creating a new one
        (this is normal on a brand new install)
        WARNING: /srv/gitolite/.ssh/authorized_keys missing; creating a new one
        (this is normal on a brand new install)
        $
        
        $ rm gitolite.pub
        $ exit
        
        $ ssh -v -i ~/.ssh/gitolite gitolite@localhost -p 2222
        

3. Gitolite Administration

Read how to setup ssh identities, gitolite documentation. Start by cloning gitolite-adimin;

        $ git clone git-admin:gitolite-admin
        

3.1. Recover Admin Account

First copy the key to remote server, in this example key is on same server;

        # install -o gitolite -g gitolite /home/bob/.ssh/gitolite.pub /srv/gitolite/gitolite.pub
        

Update gitolite key with new key;

        # su - gitolite
        $ gitolite setup -pk gitolite.pub
        
        $ rm gitolite.pub
        $ exit
        

3.2. Users

Gitolite helps add and remove users from authorized_keys by allowing add or remove keys from keydir directory in clone.

3.2.1. Add User

        $ mv bob.pub keydir/
        $ git add keydir
        $ git commit -m "Added bob public key"
        $ git push
        

3.2.2. Remove User

        $ git rm keydir/bob.pub
        $ git commit -m "Removed bob public key"
        $ git push
        

3.3. Repositories

3.3.1. Add Repository

Add repository atom and user bob to devteam group, edit conf/gitolite.conf;

        @guests         =   gitweb
        @interns        =   bob alice
        @dev            =   fred mary joe
        @teamleads      =   mary
        @staff          =   @interns @dev @teamleads

        repo  @secret
            - = @guests
            option deny-rules = 1

        repo @floss
            RW+                     =   @dev @staff
            R                       =   @all

        repo @project
            RW+                     =   @teamleads
            -   master              =   @dev
            -   refs/tags/v[0-9]    =   @dev
            RW+ develop/            =   @dev
            RW+ feature/            =   @dev
            RW+ hot-fix/            =   @dev
            RW                      =   @dev
            R                       =   @interns

        repo gitolite-admin
            RW+     =   gitolite

        repo c9-doc c9-ports c9-pmwiki
            config gitweb.owner         =   "c9 team"
            config gitweb.category      =   "c9"

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

        repo c9-ports
            config gitweb.description   =   "c9 ports"

        repo c9-pmwiki
            config gitweb.description   =   "c9 wiki"
            option hook.post-receive     =  deploy-web-doc

        repo c9-assistant
            config gitweb.owner         =   "c9 team"
            config gitweb.category      =   "c9"
            config gitweb.description   =   "c9 open assistant"

        @secret    =   gitolite-admin
        @project   =   c9-doc c9-ports c9-pmwiki c9-assistant
        

Commit and push;

        $ git add -u
        $ git push
        

3.3.2 Rename Repository

Rename rep void to sysdoc, on remote host;

        # cd /srv/gitolite/repositories/
        # mv void.git c9-doc.git
        

On workstation edit conf/gitolite.conf;

        repo c9-doc
                RW+     =       bob
        

Commit and push;

        $ git add -u
        $ git push
        

3.3.3 Delete Repository

        # cd /srv/gitolite/repositories/
        # rm -rf c9-doc.git
        

On workstation edit conf/gitolite.conf and remove c9-doc.

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 this line on /srv/gitolite/.gitolite.rc, within the %RC block;

        LOCAL_CODE => "$rc{GL_ADMIN_BASE}/local",
        

Uncomment the 'repo-specific-hooks' line in the rc file or add it to the ENABLE list if it doesn't exist.

        # allow repo-specific hooks to be added
        'repo-specific-hooks',
        

Create directory on gitolite-admin clone;

        $ cd ~/gitolite-admin
        $ mkdir -p local/hooks/repo-specific
        

Now add your hooks to that directory, but instead of using the git "standard" names (pre-receive, post-receive, post-update), you use descriptive names (e.g. "deploy", "RSS-post", etc).

On remote run;

        # su - gitolite
        $ mkdir .gitolite/local/hooks/repo-specific
        $ gitolite setup
        

4.2. Deploy and Hook script

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

        $ 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/deploy-web-doc;

        #!/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
        

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
            config gitweb.description   =   "c9 documentation"
            option hook.post-receive     =  deploy-web-doc
        

Add, commit, and push the admin repo;

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

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

        $ cd c9-doc
        $ git checkout -b deploy_branch
        

4.3.2. Deploy Script

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

        #!/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}
        

4.3.3. Debuging hooks

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

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

        $ 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;

        # crontab -e
        
        #
        # /etc/crontab: crond(8) configuration
        #

        # this way it will log
        # * * * * * /usr/share/gitolite/deployweb
        # without log
        * * * * * /usr/share/gitolite/deployweb > /dev/null 2>&1

        @hourly   ID=sys.hourly   /usr/sbin/runjobs /etc/cron/hourly
        @daily    ID=sys.daily    /usr/sbin/runjobs /etc/cron/daily
        @weekly   ID=sys.weekly   /usr/sbin/runjobs /etc/cron/weekly
        @monthly  ID=sys.monthly  /usr/sbin/runjobs /etc/cron/monthly

        # End of file
        

5. Gitweb

        $ prt-get depinst p5-cgi p5-cgi-session libcgi \
                        fcgiwrap spawn-fcgi highlight
        

5.1. Configure gitweb

Edit /etc/gitweb.conf

        our $git_temp = "/srv/www/gitweb_tmp";

        our $projects_list = "/srv/gitolite/projects.list";
        # The directories where your projects are. Must not end with a slash.
        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);

        our $projects_list_group_categories = 1;
        

5.2. Configure nginx

Edit /etc/rc.d/fcgiwrap to run as nginx user member of gitolite and www group;

        USER=nginx
        GROUP=nginx
        
        # usermod -G www,gitolite nginx
        

Create link from gitweb to web server folder;

        # ln -s /usr/share/gitweb /srv/www
        

Add this to default or main nginx virtual host;

	location /gitweb/gitweb.cgi {
            alias /srv/www/gitweb;
	    include fastcgi_params;
	    gzip off;
	    fastcgi_param   SCRIPT_FILENAME  /srv/www/gitweb/gitweb.cgi;
	    fastcgi_param   GITWEB_CONFIG    /etc/gitweb.conf;
	    fastcgi_pass    unix:/var/run/fcgiwrap.sock;
	}

	location /gitweb {
	    alias /srv/www/gitweb;
	    index gitweb.cgi;
	}
	

5.3. Configure gitolite

Edit /srv/gitolite/.gitolite.rc, change UMASK and GIT_CONFIG_KEYS to;

        # default umask gives you perms of '0700'; see the rc file docs for
        # how/why you might change this
        UMASK                           =>  027,

        # look for "git-config" in the documentation
        GIT_CONFIG_KEYS                 =>  '.*',
        

Fix permissions;

        $ sudo chown -R gitolite:gitolite /srv/gitolite
        $ sudo chmod g+rx /srv/gitolite/projects.list
        $ sudo chmod -R 755 /srv/gitolite/repositories/
        

Edit gitolite-admin/conf/gitolite.conf;

        repo c9-doc
            config gitweb.owner         =   c9 team
            config gitweb.description   =   c9 documentation
            config gitweb.category      =   c9

        repo c9-ports
            config gitweb.owner         =   c9 team
            config gitweb.description   =   c9 ports
            config gitweb.category      =   c9
        
Tools Index

This is part of the c9-doc Manual. Copyright (C) 2016 c9 team. See the file Gnu Free Documentation License for copying conditions.