Git hook scripts are not executing after upgrading to Bitbucket Data Center 8.x

Still need help?

The Atlassian Community is here for you.

Ask the community

Platform notice: Server and Data Center only. This article only applies to Atlassian products on the Server and Data Center platforms.

Support for Server* products ended on February 15th 2024. If you are running a Server product, you can visit the Atlassian Server end of support announcement to review your migration options.

*Except Fisheye and Crucible

Summary

Bitbucket Server/Data Center prior to 8.x 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. In Bitbucket 8.x, Mesh installs the hook scripts in the $BITBUCKET_HOME/shared/config/git/hooks directory.

If the hooks are not installed correctly or are damaged, these required callbacks do not happen and the functionality associated with them fails.

Environment

Bitbucket Server/Data Center 8.0 and above.

Diagnosis

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.
  • Pull requests are not updated when changes are pushed to their branches.

Causes and Solutions

The git callback hooks are not installed correctly or are damaged, these required callbacks do not happen and the functionality associated with them fails.

Hooks stopped working after Bitbucket has been upgraded to 8.x and newer

If you have been running self-written hooks without the Bitbucket App module and those scripts stopped working after the upgrade to 8.x and newer you need:

  1. Rewrite the scripts to implement Bitbucket App module
  2. Migrate the scripts to 3rd party App Module like External Hooks by Reconquest or ScriptRunner for Bitbucket by Adaptavist
  3. Register the hooks as described in KB How to Execute pre-receive hook after upgrading Bitbucket Server from 7.x to 8.x version

Hook Scripts are Missing

In versions of Bitbucket prior to 4.11, the hook scripts will be named 20_stash_callback, not 20_bitbucket_callback. Both names will be accepted by Bitbucket and renaming the scripts is not necessary!

Bitbucket Server prior to 8.x 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.


<git-hooks>
  <hook>
    <name>pre-receive</name>
    <size>430 B</size>
    <executable>true</executable>
    <callbacks>
      <callback>
        <name>20_bitbucket_callback</name>
        <size>416 B</size>
        <executable>true</executable>
      </callback>
    </callbacks>
  </hook>
  <hook>
    <name>post-receive</name>
    <size>432 B</size>
    <executable>true</executable>
    <callbacks>
      <callback>
        <name>20_bitbucket_callback</name>
        <size>418 B</size>
        <executable>true</executable>
      </callback>
    </callbacks>
  </hook>
</git-hooks>

RESOLUTION

Affected repositories for Bitbucket Server prior to 8.x can be fixed by copying the 4 missing scripts - post-receive, post-receive.d/20_bitbucket_callback, pre-receive and pre-receive.d/20_bitbucket_callback - into the repository under <BITBUCKET_HOME>/shared/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 isExecutable shows false for the scripts.

<hook>
  <name>pre-receive</name>
  <size>430 B</size>
  <executable>false</executable>
  <callbacks>
    <callback>
      <name>20_bitbucket_callback</name>
      <size>416 B</size>
      <executable>false</executable>
    </callback>
  </callbacks>
</hook>

RESOLUTION

  1. Affected repositories can be fixed by running chmod u+x on non-executable scripts. The exact path to each script is included in the XML.
  2. 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/
DescriptionBitbucket Server prior to 8.x 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.

Last modified on Apr 23, 2024

Was this helpful?

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