gitazure-devopsyarnpkgpackage.jsonazure-pipelines-release-pipeline

Use git tag or package.json version as Release name in VSTS


I have a CI task in VSTS for automatic build and test and an automatic release definition for the 'Latest from build definition default branch with tags'.

The release name format is currently set to the default value $(rev:r) which just sets an increasing number that has no relation to any real version.

I'd prefer to use the version of my app as a release name either from the package.json or from the git tag created by yarn when I update the package version. Is there any way to achieve this?


Solution

  • You can not use git tag or package.json version for release name directly, since only part of predefined variables can be used for release name, and variables can not persist between build and release.

    But you can use group variable for assistance: update group variable value with git tag or package.json version at the end of your CI build, and use the group variable as the release name. Detail steps as below:

    1. Add variable group

      In Build and Release Hub -> Library Tab -> Add Variable group (such as release name) with a variable (such as tag) with any initial value (such as 0).

      enter image description here

    2. Change the group variable in CI build

      You can add a PowerShell task to change the group variable with git tag or package.json version.

      • To get the tag name, you can use the git command git tag --points-at HEAD.
      • To get the package.json version, you can use powershell script to search the line start with "version" etc.
      • To update the group variable, you should use the REST API Variablegroups - Update.
    3. Use the group variable in release

      To apply the git tag or package.json version in release name, you can change your release definition as below:

      Release definition -> Variables Tab -> Variable groups -> Link variable group -> select the variable group -> Options Tab -> specify the group variable tag with revision as the release name.

      enter image description here enter image description here

    Now when the release is triggered, it will use the git tag or package.json version with revision for the release name.