How to create a simple hook in Stash
Stash has many hooks that you can download on Atlassian Marketplace as an add on. You can also create your own low-level hooks in your Stash server's file system. These simple hooks are great for things like triggering the index of a FishEye repository.
Creating the hook
1) Navigate to a repository in a stash instance:
To configure a hook you must navigate to the directory of the repository you would like to configure on the Stash server. Stash repositories are located in your Stash home folder at
/shared/data/repositories. You can find a specific Stash repository location in the UI of Stash by going to the repository and then settings. There you will see a line that says "Location on disc" and a path similar to
Stash hooks are enabled and configured on a per repository basis so you will need to create your hook in each repositories hooks directory.
If you have a lot of repositories you need to identify by ID, run a query to the Stash database for the ID numbers similar to :
SELECT name, id FROM <DataBase>.repository;
This query will list all repositories and corresponding ID's.
2) Navigate to the directory of
Inside the hooks directory of a repository you will find two directories called
post-receive.d. A hook should be created in one of these two locations depending on when the hook should occur.
Pre-receive hooks are used to potentially block pushes to Stash. An example might be to stop deletion of branches or limit force-pushes to personal branches. Pre-receive hooks do not fire when a pull-request is merged - for that, you may also want to implement a repository merge-check.
Post-receive hooks are invoked after a push has taken place. An example might be to notify a build server, such as Bamboo, that there are new commits on a branch to be built.
3) Create a file in the
The number at the beginning of the file name will indicate the order your hook will be executed. If you have multiple hooks and you want them to happen in a specific order number them accordingly:
1_pre_receive, 2_pre_receive, 3_pre_receive and so on.
echo "pre-receive message 30" exit 0
This message will display in the clients command line "
pre-receive message 30".
pre-receive.d/20_stash_callbackfiles in these directories. This file must remain untouched and have full permissions for hooks to function.
It is a good practice to have the user running Stash own the pre or post-receive hook.
application.xml filelocated in
There is a 30 minute tutorial available here.