1
votes

I'am aware of https://docs.microsoft.com/en-us/rest/api/azure/devops/release/releases/create?view=azure-devops-rest-6.0#uri-parameters and i can create releases via REST, but ...

Problem: The issue with those releases is, that a release triggered via the REST lacks some predefined variables like Build.BuildNumber - or at least, they are not available at all scopes.

It seems like Build.BuildNumber is available in the pipeline stage, but is missing in when the release name format is computed. This means, a release name format Release-$(Build.BuildNumber)($(Release.ReleaseId)) will end up with a blank Build.BuildNumber when created using the payload below.

Details:

My json payload

{
  "definitionId": 9,
  "description": "Test Release",
  "artifacts": [
    {
      "alias": "My Build Artifact",
      "instanceReference": {
        "id": "6989",
        "name": null
      }
    }
  ],
  "isDraft": false,
  "reason": "none",
  "manualEnvironments": null
}

While artifacts.instanceReference.id references a valid build (which of course has a buildNumber) - so that during the release stage Build.BuildNumber is properly populated.

I send the payload via

curl -X POST -u username:redactedPAT -H "Content-Type: application/json" -d @payload.json https://vsrm.dev.azure.com/redactedCompany/redactedProjectId/_apis/release/releases\?api-version\=6.0

Question: What is the right way to create a release as if this would be created via the GUI including all the META-Data? Do i miss use the API somehow or do i need to manually set the environment variables for this to work (or even somehow via variables). Since the buildNumber is available in the stage, could that be rather/even a bug?

1

1 Answers

1
votes

The release name is evaluated at the compile time, which means it is populated before the pipeline tasks are executed. You can check out below workarounds to populated the release name.

1, The $(Build.BuildNumber) variable you defined in the release name format will retrieve the name property you pass to the artifacts instanceReference in the request body. So you can pass the BuildNumber value to the name property under instanceReference in the request body. See here.

{
  ...
  "artifacts": [
    {
      "alias": "My Build Artifact",
      "instanceReference": {
        "id": "6989",
        "name": BuildNumber #set the buildNumber here.
      }
    }
  ],
  ...
}

2, Another workaround is to use logging commands to update the release name in a script task.

You can add a script task in your release pipeline stage. And run below logging command to update the release name during the release pipeline execution.

echo "##vso[release.updatereleasename]Release-$(Build.BuildNumber)($(Release.ReleaseId))"

See below example:

enter image description here

When the task is executed. it will update the release name with your desired format.