Script to Programmatically Increment Laravel Application Version

DevOps Laravel Version Script

This script was developed to increment / bump the version of your Laravel application. It does this inline with your git tags and commit hash automatically.

This method of versioning your application means that the version number is also contained in your version control.

Using the method I have detailed below the version value can be displayed in the footer of your application. It is then much easier for developers and user to know which version they are using which helps with bug reporting and keeping track of automated deployments etc.

This solution works by using a script which changes a the value in a php config file programmatically. This is then utilised as a Laravel script to change config values programmatically, and thus incrementing the Laravel application version.

There are two components to this version increment method.

1. Config file

The first is to create a config file within your laravel appliction to contain the version number. The file should be placed the file in the config directory.

In this example I have created “laravelapp.php” with contence below.

laravelapp.php

<?php return [ 'version' => 'v0.1-alpha-123-ab12ab12' ];

2. Script

The second part is to create the script that will increment / bump the version of your Laravel application using this script.

versionBump.sh

#!/bin/bash
targetConfigFile="code/config/laravelapp.php";

oldVersion=$(awk '{print $6}' $targetConfigFile);
newVersion="'"$(git describe --tags)"'";

echo "Bumping..."
echo " Old Version = "$oldVersion
echo " New Version = "$newVersion

awk '{$6 = "'$newVersion'"; print}' $targetConfigFile > $targetConfigFile".tmp" \
&& mv $targetConfigFile".tmp" $targetConfigFile

echo "Done."

The version is created automatically. It essentially uses this command git describe --tags output for the version value.

At this stage, you are done.

But a very useful feature of this solution is to use the version number in your Laravel application.

3. Display Version (optional)

This version value can then be displayed in the footer of your application. Here is an example from my application using bootstrap styling.

<!-- Footer -->
<footer class="sticky-footer bg-white">
    <div class="container my-auto">
        <div class="copyright text-center my-auto">
            <span>Copyright &copy; { { config('app.name', 'Laravel') } } { { now()->year } }.</span>
        </div>
        <div class="copyright text-center my-auto">
            <span>Version { { @config('laravelapp.version','0.0.0')} }</span>
        </div>
    </div>
</footer>
<!-- End of Footer -->

This versionBump.sh Laravel script is simple to use and uses a command line tool called AWK which is standard in most unix/linux based operating systems. This means it can easily be integrated as an automatic step in your deployment workflow. This is something that I am planning to do within the Medical Imaging System deployment process

You can read more about how I have used awk in my previous projects.

If you would like to read more about the background to this work, it was conducted as part of system development of the Medical Imaging System.

If you have any questions or comments you can contact me.