spinen / laravel-version
Helpers to keep track of the version of your Laravel application.
Installs: 16 853
Dependents: 1
Suggesters: 0
Security: 0
Stars: 8
Watchers: 10
Forks: 0
Open Issues: 0
Requires
- php: ^8.1
- illuminate/console: ~8|~9|~10|~11
- illuminate/container: ~8|~9|~10|~11
- illuminate/routing: ~8|~9|~10|~11
- illuminate/support: ~8|~9|~10|~11
- illuminate/view: ~8|~9|~10|~11
Requires (Dev)
- laravel/pint: ^1.6
- mikey179/vfsstream: ^1.6.11
- mockery/mockery: ^1.5.1
- phpunit/phpunit: ^9.6.5
- psy/psysh: ^0.11.1
- symfony/var-dumper: ^6.2
This package is auto-updated.
Last update: 2024-10-09 01:06:27 UTC
README
There are many times that it is nice to know the version of your application. At Spinen, we adhere to Semantic Versioning for our applications using git-flow. We keep a file in the root of our projects named VERSION
with the current version. The CI/CD process modifies the VERSION
file to append meaningful data. Then in the views we display the version like this <meta name="application-version" content="{{ $version }}">
. Additionally, we have a smokescreen test to hit a /version
route to make sure that the expected version of the site is running.
Build Status
Prerequisite
As side from Laravel >= 8, there are no packages that are required.
Install
Install Version:
$ composer require spinen/laravel-version
The package uses the auto registration feature of Laravel 5.
Description of version file
You need a file, with the Semantic Version of your application. For example...
4.3.6
Then you can add additional data either manually or via your CI/CD pipeline to be similar to this...
4.3.6
feature/some_great_thing
sha:3c40a5b0d0a07973bd117a39b53367c9ff4d4cc0
build:11425
20190220170058+0000
Breakdown of the line of the file
Some notes about the file...
- We assume that the first line is only
major
.minor
.patch
- The first non-empty line after the version will become the
pre_release
- If
pre_release
ismaster
, then it gets ignored - All of the lines after the line being used as the
pre_release
get concatenated together with a.
to become themeta
, so there can be as many lines as you would like
Using the package
The Spinen\Version\Version
object loads the configured version file to parse the file into the following public properties on the object...
You can inject Spinen\Version\Version
into your code to gain access to the properties. For our use, here are 3 main uses of the package...
$version
variable in views/version
routeversion
commands
Variable in views
An instance of \Spinen\Version\Version
is added to to all views as the $version
variable. You can do things like...
- Add version to HTML Header
<meta name="application-version" content="{{ $version }}">
to get<meta name="application-version" content="4.3.6-feature/some_great_thing+sha:3c40a5b0d0a07973bd117a39b53367c9ff4d4cc0.build:11425.20190220170058+0000">
- NOTE: Casting object to string is the same as
$version->semver
- Add version to footer of page
<small class="app_version">{{ $version->version }}</small>
to get<small class="app_version">4.3.6</small>
Route
Visiting /version
will return the version...
$ curl https://localhost/version 4.3.6-feature/some_great_thing+sha:3c40a5b0d0a07973bd117a39b53367c9ff4d4cc0.build:11425.20190220170058+0000
Commands
The following artisan
commands are added...
Configuration
Publish the package config file to config/version.php
:
$ php artisan vendor:publish --tag version-config
This file is fully documented. You will need to make the changes to that file to suit your needs. There are 3 main configuration items...
file
- Name of the file that has the versionroute
- Configuration of the route to display the versionview
- Configuration of the view composer to add the version to the views
Example CI to modify version file
We use GitLab, so here is a partial example job
that we have in our .gitlab-ci.yml
...
version: stage: build image: php7.2 dependencies: [] script: - echo "" >> VERSION - echo "${CI_COMMIT_REF_NAME}" >> VERSION - echo "sha:${CI_COMMIT_SHA}" >> VERSION - echo "build:${CI_PIPELINE_ID}" >> VERSION - date +"%Y%m%d%k%M%S%z" >> VERSION artifacts: name: "${CI_BUILD_NAME}_${CI_BUILD_REF_NAME}-version" paths: - VERSION expire_in: 3 days