Release versioning of web application in Bamboo build

Automating the build and deploy workflow in Atlassian Bamboo is a useful practice to streamline the delivery of a web application. In this post I’m sharing a method to do automatic release versioning of a Javascript web application. With this method you can declare the application version in just one place and have Bamboo assign it to each build, along with the build number, such as:

2.0.0.85
2.0.0.86
...

The first thing to think about is where to store the application version. In the case of a project with NodeJS dependencies, we already have a package.json file in the project root directory where we can store the current version:

{
    "name": "My Application",
    "author": "John Doe",
    "version": "2.0.0",
     "devDependencies": {
         "bower": "*",
         "grunt": "^0.4.0"
     },
     "engines": {
         "node": ">=0.10.0"
     }
}

The next step is having Bamboo read the current version from package.json during the build process. To do that, we can leverage the Inject Bamboo variables task, which reads variables from a text file in “key=value” format.  So we first have to extract the version from package.json and write it to a text file. We can do this by adding a new Script task to the build job. The inline shell script we’ll write will use Node to read the version from package.json, append the Bamboo build number and write it to a text file with the expected format:

app_version="$(node -e 'console.log(require("./package.json").version);')"
version="${app_version}.${bamboo_buildNumber}"
echo "version=$version" > variables.txt

After the script task we can now add the Inject Bamboo variables task:

Bamboo Inject Variables from file task

The version variable will be stored in the bamboo.artifact namespace, and we can access it this way:

${bamboo.artifact.version}

The last step is to configure the release versioning of the deployment plan to use the custom generated version:

Bamboo custom release versioning