Skip to content

Check your SEO health in minutes with my SEO by Daniel (me). Ensure correct on page Seo on your site Run a free audit →

Quick search

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.

Automation • • 5 min read
Track Code deployments in New Relic with Laravel commands

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).

November 2025 Update:

In the last 3 years as a Newrelic Consultant I helped Saas companies, security companies and more to implemented their Observability needs, complete the following forms to discover how i can help your company too.

Want my expertize applied to you company? Contact me

If the form does not work complete it here: https://forms.danielpetrica.com/s/gar9xw0ioq5ph9fy5u733op9

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

Related articles

More posts on similar topics you might enjoy.

All articles

Get my updates in your inbox

Register to be the first to receive my new articles on Laravel, DevOps, and more.

Subscribe to the newsletter

One email when new articles are published. No spam, unsubscribe anytime.

Protected by Mailcoach. Double opt-in may apply.