How to create a simple hook in Stash

'How Do I...' and 'How to...' Guide to Stash

On this page

Still need help?

The Atlassian Community is here for you.

Ask the community

Usage

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:

$STASH_HOME/shared/data/repositores/REPO_ID/hooks

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 $STASH_HOME/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_HOME/shared/data/repositories/1

Stash hooks are enabled and configured on a per repository basis so you will need to create your hook in each repositories hooks directory. 

Repository ID

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  pre-receive or post-receive hooks: 

Inside the hooks directory of a repository you will find two directories called pre-receive.d and 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 pre or post-receive directory

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. 

Simple Hook Message
echo "pre-receive message 30"
exit  0

This message will display in the clients command line "pre-receive message 30".

Do not edit the post-receive.d/20_stash_callback or pre-receive.d/20_stash_callback files 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.

Troubleshooting

If a hook is not functioning one thing you can check is the application.xml file located in STASH_HOME/logs/support
Make sure that the isExecutable element is true.   

 

 

tip/resting Created with Sketch.

There is a 30 minute tutorial available here

Related Content

Last modified on Feb 23, 2016

Was this helpful?

Yes
No
Provide feedback about this article
Powered by Confluence and Scroll Viewport.