有没有办法在Azure DevOps中编写重复性任务的脚本? [英] Is there a way to script repetitive tasks in Azure DevOps?

查看:73
本文介绍了有没有办法在Azure DevOps中编写重复性任务的脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每次在项目中创建一个新的GIT存储库时,我们都会执行许多任务,我想知道是否有一种方法(PowerShell或任何其他方法)来编写这些脚本.例如所有这些都是我们每次创建新存储库时要遵循的步骤

We have a number of tasks that we carry out every time we create a new GIT repository in our project, and I would like to know if there's a way to script (PowerShell or any other method) these out. for e.g. every these are the steps we follow everytime we create a new repo

  1. 创建一个新的GIT存储库
  2. 创建一个构建管道以在以下期间进行构建验证拉取请求
  3. 向Master添加分支策略,包括使用上述构建验证构建的步骤
  4. 为发布创建构建管道
  5. 创建发布管道

推荐答案

是否可以在Azure DevOps中编写重复性任务的脚本?

Is there a way to script repetitive tasks in Azure DevOps?

当然可以!正如Daniel在评论中所说,仅使用REST API就可以实现所有这些.但是,由于您要实现的步骤很少,因此脚本可能复杂.

Of course yes! As Daniel said in comment, just use REST API can achieve these all. But since the steps you want to achieve are little much, the script might be little complex.

  • 创建一个新的GIT存储库

如果您还想使用API​​完成此步骤,则需要完成3个步骤(由于doc中没有对此进行记录,因此我将对其进行详细介绍):

If you also want to use API to finish this step, it needs 3 steps to finish that( Since this does not be documented in doc, I will described it very detailed ):

第1步:创建导入存储库的验证

Step1: Create the validation of importing repository

POST https://dev.azure.com/{org name}/{project name}/_apis/git/import/ImportRepositoryValidations?api-version=5.2-preview.1 

请求正文:

{
  "gitSource":
     {
      "url":"${ReposURL}",
      "overwrite":false
     },
    "tfvcSource":null,
    "username":"$(username}"/null,
    "password":"${pw}"/"${PAT}"/null
}

第2步:创建新的存储库名称

Step2: Create the new repos name

POST https://dev.azure.com/{org name}/{project name}/_apis/git/Repositories?api-version=5.2-preview.1

请求正文:

{
  "name":"${ReposName}",
  "project":
  {
    "name":"{project name}",
    "id":"{this project id}"
  }
}

第3步:导入存储库

POST https://dev.azure.com/{org name}/{project name}/_apis/git/repositories/{the new repos name you create just now}/importRequests?api-version=5.2-preview.1

请求正文:

{
  "parameters":
  {
   "deleteServiceEndpointAfterImportIsDone":true,
   "gitSource":
    {
     "url":"${ReposURL}",
     "overwrite":false
    },
    "tfvcSource":null,
    "serviceEndpointId":null
}
}

在这些脚本中,您可以在 Variable 标签中设置变量,然后使用 $ {} 在脚本中获取变量.

In these script, you can set variables in Variable tab, then use ${} to get them in the script.

  • 为请求请求期间的构建验证创建构建管道

此步骤最好手动完成,因为您可以配置有关任务的更多信息并使用UI进行触发.如果仍要使用API​​,请参阅此文档:

This step you'd better finish manually, because you can configure more about tasks and trigger with UI. If still want use API, refer to this doc: create build definition. There has detailed sample you can try with.

向Master添加分支策略,包括使用上述构建验证构建的步骤

Add branch policies to Master including a step to validate build using the above build

此API仍记录在文档中:

This API still be documented in doc: create build policy. Just refer to that, and ensure use the correct policy type and the corresponding buildDefinitionId.

  • 为发布创建构建管道

这仍然建议您手动完成,与您提到的步骤3相同.

This still recommend you finish manually, same with the step3 you mentioned.

创建发布管道

请参阅此文档:创建发行版.

注意:对于将要多次使用的某些参数,您可以将其设置为变量.对于需要从先前的API响应中获取的参数,您可以定义一个变量以获取其值,然后将此变量传递给下一个要使用的API.:

Note: For some parameter which will be used many times, you can set it as variable. For the parameter which need get from previous API response, you can define a variable to get its value then pass this variable into the next API to use.For e.g. :

$resultT= $result.Headers.ETag
Write-Host  "##vso[task.setvariable variable=etag;]$resultT"

现在,您可以直接在下一个API中使用 $(etag).

Now, you can directly use the $(etag) in the next API.

这篇关于有没有办法在Azure DevOps中编写重复性任务的脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆