Photo by pxhere.com

Manage your application version automatically using Git and Azure DevOps

Save time when deploying a new version for your application!

Posted by Damien Aicheh on 12/19/2019 · 6 mins

When developing an application you always need to manage different environments and versions for your application. To deliver a new version of your project, one way to do it will be:

  • Upgrade the version of your application in code
  • Commit your code
  • Add a new version tag to your Git repository
  • Start your pipeline to generate your new application package

This kind of process is too long and error prone, while the only thing you want is to have the same version between your Git tag and your application package delivered. So why not just read the Git tag?

Introducing Mobile Versions tasks

To solve this problem I developed a new set of Azure DevOps tasks called Mobile Versioning, easy to use in your pipeline and free. This group of tasks provide a way to extract the version from a Git repository tag and apply this version to your mobile application as you want.

Usage

First of all, go to your Azure DevOps and install the Mobile Versioning extension into your organisation.

Mobile Versioning Tasks

Extract the version from the tag

This group contains multiple tasks, the most important one is the ExtractVersionFromTag. To use it in your azure-pipelines.yml you just have to call it like this:

- task: ExtractVersionFromTag@1
  inputs:
    projectFolderPath: '$(Build.SourcesDirectory)' # Optional. Default is: $(Build.SourcesDirectory)
    tagPrefixMatch: 'my_prefix' # Optional. Used to filter tags with a prefix. Ex: dev_

This will extract the version of the last tag you pushed to your Git repository, from the branch you selected to build your pipeline. Then the task will assign each part of your tag to a global variable.

For example if you have a tag with a value of v1.2.3 or 1.2.3 this will automatically assign the global variables $(MAJOR) to 1, $(MINOR) to 2 and $(PATCH) to 3. In addition this task will compute the number of commits from the first commit on your branch to this tag and assign this value to $(NUMBER_OF_COMMITS) and if you want only the number of commits since your tag you can use $(NUMBER_OF_COMMITS_SINCE_TAG). These 4 variables will then be usable in your azure-pipelines.yml.

Also the extension supports pre-release tags so if you have an alpha version like this: v1.2.3-alpha6 the alpha6 will be assign to the variable $(PRE_RELEASE) and you can use it like the previous ones.

By convention the Git tags are defined like this: v(Major).(Minor).(Patch). But the ExtractVersionFromTag task also manages the tags defined without the v prefix.

This task can be used whatever the technology you use for your Azure DevOps pipelines.

Update your mobile application version

The Mobile Versioning tasks currently provide 3 tasks to help mobile developers to automatically update their iOS and Android applications versions whatever the mobile technology you used.

  • UpdateiOSVersionInfoPlist for the Info.plist
  • UpdateAndroidVersionManifest for the AndroidManifest.xml
  • UpdateAndroidVersionGradle for the build.gradle inside the app folder

These tasks can be used with or without using the ExtractVersionFromTag task before.

To automatically update the version of your application, you just need to add these tasks after using the ExtractVersionFromTag task:

For iOS to update the Info.plist:

- task: UpdateiOSVersionInfoPlist@1
  inputs:
    infoPlistPath: 'your_project/Info.plist'
    bundleShortVersionString: '$(MAJOR).$(MINOR).$(PATCH)-$(PRE_RELEASE)' # Optional. Default is: $(MAJOR).$(MINOR).$(PATCH)
    bundleVersion: '$(NUMBER_OF_COMMITS)' # Optional. Default is: $(NUMBER_OF_COMMITS)

For Android:

If you need to update the AndroidManifest.xml:

- task: UpdateAndroidVersionManifest@1
  inputs:
    androidManifestPath: 'your_project/AndroidManifest.xml'
    versionName: '$(MAJOR).$(MINOR).$(PATCH)-$(PRE_RELEASE)' # Optional. Default is: $(MAJOR).$(MINOR).$(PATCH)
    versionCode: '$(NUMBER_OF_COMMITS)' # Optional. Default is: $(NUMBER_OF_COMMITS)

If you need to update the build.gradle inside the app folder:

- task: UpdateAndroidVersionGradle@1
  inputs:
    buildGradlePath: 'your_project/app/build.gradle'
    versionName: '$(MAJOR).$(MINOR).$(PATCH)-$(PRE_RELEASE)' # Optional. Default is: $(MAJOR).$(MINOR).$(PATCH)
    versionCode: '$(NUMBER_OF_COMMITS)' # Optional. Default is: $(NUMBER_OF_COMMITS)

Customize the version

Notice that for these 3 tasks you can override the default value of the properties for example instead of:

bundleShortVersionString: '$(MAJOR).$(MINOR).$(PATCH)'

you can concatenate it with whatever variable you previously define:

bundleShortVersionString: '$(MAJOR).$(MINOR).$(PATCH)-$(PRE_RELEASE)-($(NUMBER_OF_COMMITS))'

or simply use it like this:

bundleShortVersionString: '1.2.3'

Final touch

This project is a first starting point to help developers managing their applications versions. Feel free to contribute to this project if you want.

You will find examples of use in the Github project repository.

Happy coding!

Do not hesitate to follow me on to not miss my next tutorial!