Track Code deployments in New Relic with Laravel commands
Simplify New Relic deployments track with a custom Laravel. Track deployments easily without GitHub Actions by using 'php artisan app:deploy-mark'. Learn how to configure and integrate this command into your CI/CD pipeline for seamless New Relic deployment tracking.
I'm loving new-relic but I always found new relic deployments hard to track, especially as I'm not using GitHub actions for my deployment pipeline.
To make my life a lot easier I prepared a custom Laravel command which send to New Relic the new deployment information. The beauty of this command comes from the ability to run it whenever I deploy my code, as it only requires the ability of running a '''php artisan app:deploy-mark''' and the availability of git to retrieve the version (eventually we could change it with the something almost unique as the current date-time).
Here's the entire file of the command
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Http;
class deployMark extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'app:deploy-mark';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Register deployments to newrelic via graphql mutations.';
/**
* Execute the console command.
*/
public function handle(): bool
{
$apikey = config('services.newrelic.user_key');
#Specify an existing New Relic guuid ID
$app_id = config('services.newrelic.app_id');
$dep_rev = trim(exec('git log --pretty="%h" -n1 HEAD'));
//Todo: change it to match your data governance region
$url = "https://api.eu.newrelic.com/graphql";
try
{
$result = Http::withHeaders([
'API-Key' => $apikey,
])->post($url, [
'query' => 'mutation {changeTrackingCreateDeployment(
deployment: {
version: "' . $dep_rev . '",
entityGuid: "' . $app_id . '" }
) {
deploymentId
entityGuid
}}'
]);
} catch (\Exception $e) {
$this->error("There was an error: " . $e->getMessage());
return false;
}
if ($result->successful()) {
$this->info("Newrelic result" . $result->body());
} else {
$this->warn("Newrelic result" . $result->body());
}
return $result->successful();
}
}
To make it work I added a first an entry in the services.php config file:
'newrelic' => [
'user_key' => env('NEWRELIC_USER_KEY', null),
'app_id' => env('APP_ID_NEWRELIC', null),
]
Then I simply add my values inside the .env file like this:
NEWRELIC_USER_KEY=<your_user_api_key>
APP_ID_NEWRELIC=<your_app_guuid>
Now when I need to track a deployment I simply added a final step executing a simple `ph artisan app:deploy-mark`
This command is flexible enough to be used in many different Ci/cd pipelines stacks so if you end up using it please leave a comment with your usage.
I'm curious to see how it can be used.