How to create a simple hook in Bitbucket Server

お困りですか?

アトラシアン コミュニティをご利用ください。

コミュニティに質問

プラットフォームについて: Server と Data Center のみ - この記事は、サーバーおよびデータセンター プラットフォームのアトラシアン製品にのみ適用されます。

目的

Bitbucket Server has two primary ways you can create a hook.

The recommended way is to create a Plugin Using our Java plugin development framework. It is also possible, although strongly discouraged, to create a server side Git hook in your Bitbucket Server instance's file system.

ソリューション

Using the Java plugin framework

Common question:

We need a pre-receive and post-commit hook that applies to only one repository, not that whole set of repos. It appears that loading a hook makes it appears across all repos and it's a matter of keeping them disabled where they don't apply. Correct?

There are two primary approaches to creating a pre-receive hook using the Java plugin development framework. 

The primary approach consists on implementing PreReceiveRepositoryHook. In this case, a standard configuration is required, and indeed this configuration panel will be present in all repositories in the Stash. This approach is documented here:

Here is a blog post and a tutorial that further describe that approach:

Another approach involves implementing the PreReceiveHook interface. This does not imply a configuration screen, although you can add one if you so desire that would be present only on certain repositories. The basics you would need to get implement just the hook (no-configuration) can be found:

The main page for Plugins on Bitbucket server are on the documentation below:


Creating the hook

Before you execute the manual procedure below, note that there is a plugin on Marketplace that helps you set this up (as opposed to doing so manually as outlined below):


The steps below will focus on hooks directly on the file system.


1) Navigate to a repository in a bitbucket instance:

Please see Identifying a Repository on the Bitbucket Server if you aren't sure how to find this location. 

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

2) Navigate to the directory of  pre-receive or post-receive hooks: 

Inside the hooks directory of a repository (on the server where Bitbucket is running)  you will find two directories called pre-receive.d and post-receive.d. A hook should be created in one of these two locations accordingly to your objectives. 

Pre-receive hooks are used to potentially block pushes to Bitbucket Server. 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: 21_pre_receive, 22_pre_receive, 23_pre_receive and so on. 

This number should always be something higher than 20 because the script that we ship needs to be executed first.

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_bitbucket_callback or pre-receive.d/20_bitbucket_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 Bitbucket Server own the pre or post-receive hook.

トラブルシューティング

  • If a hook is not functioning one thing you can check is the application.xml file located in BITBUCKET_HOME/logs/support

  • Make sure that the isExecutable element is true.   

 

  • If the script still doesn't run, you can always approach the troubleshooting by creating a simple file containing an echo just as described on the step #3 above.
    • If you are executing a command that the user running Stash/Bitbucket server doesn't have permission to, that will fail.

関連コンテンツ

説明

Bitbucket Server has two primary ways you can create a hook. The recommended way is to create a Plugin Using our Java plugin development framework. It is also possible, though strongly discouraged, to create a server side Git hook in your Bitbucket Server instance's file system. This page discusses the latter process.

製品Bitbucket
最終更新日: 2018 年 9 月 12 日

この内容はお役に立ちましたか?

はい
いいえ
この記事についてのフィードバックを送信する
Powered by Confluence and Scroll Viewport.