Skip to main content

DDEV & CaptainHookPHP

· 2 min read
Stephan Hochdörfer

Since we are currently moving to DDEV to standardize our Docker development setups, I was wondering how easy it will be to add my favorite Git Hook Manager CaptainHookPHP to the setup.

Since CaptainHook supports Docker as run-mode for quite a while and since the run-mode configuration is quite extensible, it took me not much time to connect the dots.

First, a captainhook command for ddev needs to be created, which can be invoked by the Git hooks. For this, create a new file called captainhook in the .ddev/commands/web/ directory with the following content:

#!/bin/bash

## Description: Executes CaptainHookPHP Git hook

"$@"

Since the full path to the CaptainHook CLI script, incl. all parameters is passed via the generated Git Hook scripts, it is enough to execute whatever input is given. The command can now be invoked via ddev captainhook.

In your captainhook.json configuration file, the run-mode needs to be configured like this:

{
"config": {
"run-mode": "docker",
"run-exec": "ddev captainhook",
"run-path": "/var/www/html/vendor/bin/captainhook"
}
}

The run-mode needs to be set to docker, the executable command is ddev captainhook and additionally, the run-path needs to be defined, otherwise the CaptainHook installer is not properly able to resolve the path to the CLI script.

To install the Git Hooks run the following command on your host or in the web docker container:

./vendor/bin/captainhook install

This will instruct CaptainHook to generate the Git Hook scripts, which look like this:

#!/bin/sh

ddev captainhook /var/www/html/vendor/bin/captainhook hook:commit-msg "$@"

As you can see, the Git Hook will run the ddev captainhook command, which we defined earlier, and pass the path to the CaptainHook CLI script as well as the Git hook type.

These are all the steps needed to be able to use CaptainHook in a DDEV environment.

UPDATE: Tobias Gaertner pointed out that the captainhook bash script is not needed. Instead, we can rely on the ddev exec command to execute scripts in the container. The simplified configuration looks like this:

{
"config": {
"run-mode": "docker",
"run-exec": "ddev exec",
"run-path": "/var/www/html/vendor/bin/captainhook"
}
}