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        =   clair bob
        @dev            =   alice david
        @teamleads      =   mike
        @staff          =   @interns @dev @teamleads


        repo gitolite-admin
            RW+     =   gitolite

        repo @floss
                R                       =   @all

        repo @proto
                RW+                     =   @staff

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

        @project     =   c9-doc c9-ports

        repo c9-doc c9-ports
            option hook.post-receive     =  hook-deployweb

        repo testing
            RW+     =   @staff
        

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
        

4. Gitolite 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.1. Deploy Hook

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.

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.

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

        #!/bin/bash

        ######################################################################
        #
        # 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" == "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

        done
        

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

        repo c9-doc
            option hook.post-receive     =  hook-deployweb
        

Add, commit, and push the admin repo;

        $ git add -u && git commit -m "deploy hook"
        

Create deploy directory on remote;

        # su - gitolite
        $ mkdir deploy
        

On remote run;

        # su - gitolite
        $ gitolite setup
        

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

Create /usr/share/gitolite/deployweb;

        #!/bin/sh

        ######################################################################
        #
        # Put this file in;
        # /usr/share/gitolite/deployweb
        #
        DIR_WWW=/srv/www/
        DEPLOY_BRANCH=master
        TARGET_USER=www

        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
        

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.