Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save omego/2ff731aabf03ec9aa353888e88c04488 to your computer and use it in GitHub Desktop.
Save omego/2ff731aabf03ec9aa353888e88c04488 to your computer and use it in GitHub Desktop.

Get thg latest git tag + latest commit date and hash and use it as a laravel app version

Based on TALL Stack Tips

config/version.php

<?php

use Symfony\Component\Yaml\Yaml;

//get the created tag from git
$tag = exec('git describe --tags --abbrev=0');

// defaults to -.-.- if no tag is found
if (empty($tag)) {
    $tag = '-.-.-';
}

//get the commit hash
$hash = trim(exec('git log --pretty="%h" -n1 HEAD'));

//get the last commit date
$date = Carbon\Carbon::parse(trim(exec('git log -n1 --pretty=%ci HEAD')))->setTimezone('+03:00');

// available in the app as version.tag, version.hash, version.date or version.string
$array = [
    'tag' => $tag,
    'date' => $date->format('y-m-d H:i:s'),
    'hash' => $hash,
    'string' => sprintf('%s-%s (%s)', $tag, $hash, $date->format('d/m/y H:i')),
];

$yaml = Yaml::dump($array);

file_put_contents('../version.yml', $yaml);

return Yaml::parseFile('../version.yml');

usage

config('version')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment