Automating the release workflow with scripting

Backend Feb 4, 2023

Releasing a newer version of software often involves manually changing the version information, which usually means dealing with hard-coded strings. However, developers sometimes forget to change some of them. So why not write a script to save us from this? This article discusses the details of implementing such a script for releasing Laravel-based projects.

Version string in composer.json

Firstly, there is a field in composer.json that indicates the current version of the project. Therefore, the script needs to access the composer.json file. A simple but straightforward solution is documented below:

Firstly, we read the content of composer.json to a variable

const composerJson = JSON.parse(fs.readFileSync('./composer.json', 'utf8')
                               

Then we set the version to a new version and write it back

fs.writeFileSync('./composer.json', JSON.stringify(composerJson, null, 2))

Voila!

Version string in app.php

Next, we need a place for centralized control of version information that Laravel can easily access. And config/app.php seems to be the right option. So we add a new key called version to config/app.php. The implementation was similar to the previous one, but needed some small adjustments, and, regex to the rescue!

fs.writeFileSync('./config/app.php', appConfig.replace(/'version' => '.*',/g, `'version' => '${version}',`));

Commit it!

After making those changes, we can commit them with a message indicating a version release, and this can be done by the following commands.

execSync("git add . ")
execSync(`git commit --am "Release v$(version}"`);

It is also reasonable to create a new tag for the release.

execSync(`git tag -a v${version} -m "v${version}"`);

For Inertia.js users

If you want to share the version info to frontend, then don't forget to add this in your HandleInertiaRequests middleware:

[
    'appVersion' => config('app.version')
]

Tags