- Install the GitHash package with
composer require tjvb/githashto install the githash package
- Add the githash to your logs, views, or anything you want.
- If you use Laravel install Laravel GitHash with
composer require tjvb/laravel-githashto install the laravel-githash package that add the hash added to your log files and have a blade component available.
I created two new packages that are related, in this post I give a little background for the packages. The purpose of the packages is to get the hash of the current commit.
In an environment with queue runners, concurrent request and deployments it can be difficult to define the version of the code that is used for a specific request or job. If you want to know the reason for an unexpected result or change in duration, it can be useful to know the exact version of your code.
The first step was to create a framework agnostic package that can get the current hash from a git repository. For this there are multiple options. The default method is to run the command
git rev-parse HEAD.
For this I created two GitHashFinder implementations. The first use the symfony/process package if it is available. The second implementation does the same command, but with the shell_exec function. This function can be disabled so also doesn't work on any environment. Both implementations need to have git available.
The third GitHashFinder implementation doesn't use git itself but reads the git files. For this it is needed that the information contains a branch.
The package uses the withContext functionality to add the hash data to your log records. It also provides the functionality to save the hash in a cache file to prevent the need to execute the command every time and speed up the process. The cache is enabled by default if debug is disabled.
An additional function is that the package provides a blade component, this can be used to show the hash on your admin pages.
With https://gitlab.com/tjvb/githash you can get the hash of your current commit and with https://gitlab.com/tjvb/laravel-githash you have an integration within your Laravel project that adds the hash to your log items.
I hope the packages are useful for other people and feel free to create a MR if you have any addition.