Git hook scripts are not executing
This Knowledge Base article was written specifically for the Atlassian Server platform. Due to the Functional differences in Atlassian Cloud, the contents of this article cannot be applied to Atlassian Cloud applications.
Bitbucket Server relies on hook scripts that are installed into each repository (and managed by the system) to provide change information that, in turn, allows it to index JIRA issues, update pull requests and check branch permissions, for instance.
If the hooks are not installed correctly, or are damaged, these required callbacks do not happen and the functionality associated with them fails.
When git hooks are not executing, Bitbucket Server does not detect changes to a repository and as a consequence:
- Newly created repositories stay stuck in the 'empty repository' screen after changes have been pushed to the repository.
- Branch permissions are not applied.
- JIRA issue keys are not detected in pushed commits. As a result no JIRA issue links are displayed for the commits and the commits do not show up on the JIRA issue.
- Fork synchronisation is not triggered.
- Caches are not cleared and cloning a repository can return stale data.
Causes and Resolutions
The git callback hooks are not installed correctly, or are damaged, these required callbacks do not happen and the functionality associated with them fails.
Hook Scripts are Missing
20_bitbucket_callback. Both names will be accepted by Bitbucket and renaming the scripts is not necessary!
Bitbucket Server requires certain Git hooks to be installed into each repository for it to function correctly. But in this case many of the repositories are 'corrupt'. The following snippet from the
application-properties/application.xml file, produced by the Support Tools Plugin, is an example of the files that need to be installed. If these entries are missing, hooks will not be run.
<hook-files> <hook-file> <path>/var/bitbucket-data/data/repositories/1/hooks/post-receive</path> </hook-file> <hook-file> <path>/var/bitbucket-data/data/repositories/1/hooks/pre-receive.d/20_bitbucket_callback</path> </hook-file> <hook-file> <path>/var/bitbucket-data/data/repositories/1/hooks/post-receive.d/20_bitbucket_callback</path> </hook-file> <hook-file> <path>/var/bitbucket-data/data/repositories/1/hooks/pre-receive</path> </hook-file> </hook-files>
Affected repositories can be fixed by copying the 4 missing scripts -
pre-receive.d/20_bitbucket_callback - into the repository under <BITBUCKET_HOME>/data/repositories/<Repo_ID>/hooks directory. Ensure the scripts are executable after they are copied. To identify which repository ID maps to your affected repository, refer to this guide.
Hook Scripts are not Executable
Even if the required hook scripts are installed, they can only be run if they are executable. This can be checked with the same
application-properties/application.xml, generated by the Support Tools Plugin, by looking for lines like the following. Notice that
false for the scripts.
<hook-files> <hook-file> <path>/var/bitbucket-data/data/repositories/1/hooks/post-receive</path> <size>426</size> <isExecutable>false</isExecutable> </hook-file> <hook-file> <path>/var/bitbucket-data/data/repositories/1/hooks/post-receive.d/20_bitbucket_callback</path> <size>412</size> <isExecutable>false</isExecutable> </hook-file> </hook-files>
- Affected repositories can be fixed by running
chmod u+xon non-executable scripts. The exact path to each script is included in the XML.
- It is also common that the mount line for the Bitbucket Server volume does not have an executable flag. You should mount the partition with an executable flag. Example of a wrong mount:
$ mount | grep bitbucket /dev/sdb1 /bitbucket ext4 rw,user 0 0
A demonstration with a temporary ramdisk:
A) Mounting without
exec, giving a script
exec permissions and trying to execute it - this won't work
> mkdir /tmp/ramdisk && sudo mount -t tmpfs -o rw,user tmpfs /tmp/ramdisk/ > echo "echo no exec' >> /tmp/ramdisk/ && chmod a+x /tmp/ramdisk/test > /tmp/ramdisk/test -bash: /tmp/ramdisk/test: Permission denied > sudo umount /tmp/ramdisk/
B) Mounting with
exec, giving a script
exec permissions and trying to execute it - this will work:
> mkdir /tmp/ramdisk && sudo mount -t tmpfs -o rw,user,exec tmpfs /tmp/ramdisk/ > echo "echo hello' >> /tmp/ramdisk/ && chmod a+x /tmp/ramdisk/test > /tmp/ramdisk/test hello > sudo umount /tmp/ramdisk/