0
votes

I am using the REST APIs of Azure DevOps to work with a release pipeline.
The release pipeline uses task as part of an agent job. This task deploys a VM into Azure using an ARM Template file and a parameters file. So far I can execute the release pipeline successfully with the Azure DevOps REST APIs and it deploys the VM successfully with the code below.

snippet of the GUI

Using the GUI I can also successfully customize the release pipeline when executed. For example I can add values to the “override template parameters” field below in the screenshot.

However after searching I see nothing from Microsoft on how to access fields within tasks when executing a release pipeline in their Azure DevOps REST API documentation.

How can I modify my “response body” in the code below to access one of the fields in a pipeline task such as “override template parameters” when I call this pipeline?

const sendHttpRequest = (method, url, reqBody) => 
{
  const promise = new Promise((resolve,reject) =>
  {
    const request = new XMLHttpRequest();
    request.open(method, url, true);                                                    // Send a request
    request.setRequestHeader('Content-Type', 'application/json');                     // Build the request header
    request.setRequestHeader ("Authorization", "Basic " + btoa('Basic' + ":" + pat)); // Supply authentication

    request.onload = () =>
    {                                                                                 // Retrieve the response
                                                                                      // Check the status of the request...
      if (request.status >= 200 && request.status < 400)
      {
        myOutput.innerHTML = "Request Status: Valid | "; 
        resolve(request.response);
      }
      else
      {
        myOutput.innerHTML = "Request Status: Invalid |";
        reject(request.response);
      }
    } // onload

    request.send(JSON.stringify(reqBody));
  }); // promise

  return promise;
};
/*
  Execute a pipeline
*/
function run_pipeline()
{
  var reqLink = 'https://dev.azure.com/'+ org_name+'/'+ prj_name+'/_apis/pipelines/'+pipeline_id+'/runs?api-version=6.0-preview.1';

  // CHANGE RESPONSE BODY TO THE DESIRED PIPELINE NEEDED TO BE RUN
  responseBody = {
    "previewRun": "false",
    "resources": null,
    "stagesToSkip": null,
    "templateParameters": null,
    "variables": null,
    "yamlOverride": null
  };

  sendHttpRequest('POST', reqLink, responseBody).then(responseData =>
    {
      // parse the obtained data
      var data = JSON.parse(responseData);
      myOutput.innerHTML += " Pipeline Executed!";
      console.log(data);
    })
    .catch(error => 
    {
      var message = JSON.parse(error);
      myOutput.innerHTML += " Error with Data...";
      console.log(message);
    });
};
1
Hi Did you get a chance to check out below rest api. how did it go? – Levi Lu-MSFTLevi Lu-MSFT
Hi, yes it worked. However, I am looking for a way to update the storageName parameter from fabrikam to "foo".Malik
Hi @Malik you can use create release rest api to update the storageName parameter. Please check out below update.Levi Lu-MSFT

1 Answers

0
votes

If you need to get the the fields in a release pipeline task. You might need to use get release rest api

GET https://vsrm.dev.azure.com/{organization}/{project}/_apis/Release/releases/{releaseId}/environments/{environmentId}?api-version=6.0-preview.6

You can get the release id and environment id from the Release ui address bar. see below:

enter image description here

Then you will find the fields of the task in response (deployPhasesSnapshot -->workflowTasks-->inputs)

enter image description here

Update:

If you want to update the storageName parameter. Please follow below steps:

1, create a pipline variable named storageName. And check Settable at release time. See below screenshot:

enter image description here

2, Set the override template parameters field of the deployment task as below:

enter image description here

3, If you want to create a new release. You can call create release rest api

POST https://vsrm.dev.azure.com/{organization}/{project}/_apis/release/releases?api-version=5.1

Then update the pipeline variable storageName in the request body. See below:

Request Body:

{
    "definitionId": 7,

    "variables": {  
                    "storageName": {
                                      "value": "foo" 
                                   }
                 }
}

Then the storageName parameter will be overrode to "foo".

Update 2:

If you want to update a existing release. You need to call update release environment rest api

 PATCH https://vsrm.dev.azure.com/{organization}/{project}/_apis/Release/releases/{releaseId}/environments/{environmentId}?api-version=5.1-preview.6.

First you need to set the pipeline variable storageName scope to its stage. See below:

enter image description here

Then update the storageName value in the request body. And set status to inProgress to redeploy the stage. So that the overrode storageName will be reflected in the release.

{
    "status": "inProgress",


    "variables": {  
                    "storageName": {
                                      "value": "storageName-rest-api-three" 
                                   }
                 }
}