4

I'm wondering is there a simple way to install hooks for certain repo using gitolite_admin.

Let's imagine i want to have post-update hook for repo awesome using gitolite_admin repo cloned to my workstation...

#conf/gitolite_conf
repo    awesome
        RW+     =   deployer

contents of post-update:

#!/bin/sh
echo "Post receive-hook => updating Redmine repository"
sudo -u deployer perl -we '`cd /home/deployer/repo/awesome.git && git fetch -q --all`'
Danil
  • 2,497
  • 2
  • 19
  • 27
  • The latest commit in Gitolite seems to introduce repo-specifc hook: see [my edited answer below](http://stackoverflow.com/a/18431200/6309) – VonC Sep 04 '13 at 11:02

3 Answers3

3

In addition to sitaram's answer, the recent (August 29th, 2013) commit 62fb31755a formerly introduce repo specific hooks:

it's basically just creating a symlink in <repo.git>/hooks pointing to some file inside $rc{LOCAL_CODE}/hooks/repo-specific (except the gitolite-admin repo)

You cannot specific a hook for gitolite-admin though.
And you hook is only one of the three following authorized hooks:

  • pre-receive
  • post-receive
  • post-update

That means you can:

  • store your repo specific hooks in your gitolite-admin/hooks/repo-specific/xx
  • declare those your in the gitolite-admin local options on the server.

First enable those hooks:

ENABLE => [

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

Then declare the hooks on the server gitolite-admin repo:

gitolite git-config gitolite-options.hook=reponame hookname scriptname

(with a tab or \t between reponame hookname scriptname)


Original answer:

As mention in the gitolite man page on hooks

if you want to install a hook in only a few specific repositories, do it directly on the server.

(otherwise, you would be managing hooks for all git repos through gitolite-admin/common/hooks)

That being said, you could take advantage of VREF in gitolite V3.x, which are update hooks: those can be set for some repos and for some user, like any other rule.
You could then:

  • make your VREF script leave a 'flag' (a file) in the appropriate bare git repo being updated
  • make a common 'deploy' post-update hook, which would first look for that flag, and if found, deploy the repo (and remove the flag).

Again:

  • a post-update hook managed through gitolite-admin can only be common to all git repos (not what you want)
  • only VREFs can be associated to repos and users through gitolite.conf

The solution above tries to take those two facts into account to achieve what you are looking for: a deploy script running only for certain repos and managed through the gitolite.conf config file of the gitolite-admin repo.

Raphael
  • 9,779
  • 5
  • 63
  • 94
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
3

You could also look at "repo-specific environment variables"

A special form of the option syntax can be used to set repo-specific environment variables that are visible to gitolite triggers and any git hooks you may install.

For example, let's say you installed a post-update hook that initiates a CI job. By default, of course, this hook will be active for all gitolite-managed repos. However, you only want it to run for some specific repos, say r1, r2, and r4.

To do that, first add this to the gitolite.conf:

repo r1 r2 r4
    option ENV.CI = 1

This creates an environment variable called GL_OPTION_CI with the value 1, before any trigger or hook is invoked.

Note: option names must start with ENV., followed by a sequence of characters composed of alphas, numbers, and the underscore character.

Now the hook running the CI job can easily decide what to do:

# exit if $GL_OPTION_CI is not set
[ -z $GL_OPTION_CI ] && exit

... rest of CI job code as before ...

Of course you can also do the opposite; i.e. decide that the listed repos should not run the CI job but all other repos should:

repo @all
    option ENV.CI = 1

repo r1 r2 r4
    option ENV.CI = ""

That feature is fairly recent (started in commit 999f9cd39, but in this case, completed in commit 63865a16 June 2013 for 3.5.2).
But even you don't have that version, there are other ways to do this using option variables, as the last part of that section explains.

Before this feature was added, you could still do this, by using the gitolite git-config command inside the hook code to test for options and configs set for the repo, like:

if gitolite git-config -q reponame gitolite-options.option-name
then
    ...

And you can use git config variables in the same way.
Or you can use group membership -- see the comments against function "in_group" in "Easy.pm" for details.

# in_group()

# return true if $ENV{GL_USER} is set and is in the given group

# shell equivalent
# if gitolite list-memberships $GL_USER | grep -x $GROUPNAME >/dev/null; then ...
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
1

Here are step by step instructions to complement @VonC's answer

gitolite hook for specific repository

Community
  • 1
  • 1
RickyCheers
  • 334
  • 2
  • 6