简体   繁体   English

自定义 Azure DevOps REST API 调用

[英]Customizing Azure DevOps REST API call

I am using the REST APIs of Azure DevOps to work with a release pipeline.我正在使用 Azure DevOps 的 REST API 来处理发布管道。
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.此任务使用 ARM 模板文件和参数文件将 VM 部署到 Azure。 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.到目前为止,我可以使用 Azure DevOps REST API 成功执行发布管道,并使用以下代码成功部署 VM。

snippet of the GUI GUI 片段

Using the GUI I can also successfully customize the release pipeline when executed.使用 GUI,我还可以在执行时成功自定义发布管道。 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.但是,在搜索之后,我在 Microsoft Azure DevOps REST API 文档中没有看到有关在执行发布管道时如何访问任务中的字段的任何信息。

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);
    });
};

If you need to get the the fields in a release pipeline task.如果您需要获取发布管道任务中的字段。 You might need to use get release rest api您可能需要使用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.您可以从 Release ui 地址栏获取 release id 和 environment id。 see below:见下文:

在此处输入图像描述

Then you will find the fields of the task in response ( deployPhasesSnapshot --> workflowTasks --> inputs )然后你会在响应中找到任务的字段( deployPhasesSnapshot --> workflowTasks --> inputs

在此处输入图像描述

Update:更新:

If you want to update the storageName parameter.如果要更新 storageName 参数。 Please follow below steps:请按照以下步骤操作:

1, create a pipline variable named storageName . 1、创建一个名为storageName的管道变量。 And check Settable at release time .并检查Settable at release time See below screenshot:请看下面的截图:

在此处输入图像描述

2, Set the override template parameters field of the deployment task as below: 2、设置部署任务的override template parameters字段如下:

在此处输入图像描述

3, If you want to create a new release. 3、如果你想创建一个新版本。 You can call create release rest api可以调用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.然后更新请求正文中的管道变量storageName See below:见下文:

Request Body :请求正文

{
    "definitionId": 7,

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

Then the storageName parameter will be overrode to "foo".然后storageName参数将被覆盖为“foo”。

Update 2:更新 2:

If you want to update a existing release.如果要更新现有版本。 You need to call update release environment rest api需要调用更新发布环境 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.首先,您需要将管道变量storageName scope 设置为其阶段。 See below:见下文:

在此处输入图像描述

Then update the storageName value in the request body.然后更新请求正文中的storageName值。 And set status to inProgress to redeploy the stage.并将status设置为inProgress以重新部署该阶段。 So that the overrode storageName will be reflected in the release.这样覆盖的 storageName 将反映在发布中。

{
    "status": "inProgress",


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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM