How to create Build Definitions through VSTS REST API(如何通过 VSTS REST API 创建构建定义)
问题描述
我目前正在通过 bitbucket -> vsts -> azure 自动化 CI/CD 配置.
I'm currently working on automating CI/CD configurations through bitbucket -> vsts -> azure.
我的理想结果是能够将我的配置值复制粘贴(或手动输入)到它们各自的控制台程序中,并让应用程序配置整个 CI/CD 考验,而无需单击所有 Web 界面.现在可以在 Bitbucket 和 Azure 中使用,但通过 REST API 创建 VSTS CI/CD 配置被证明是困难的.
My ideal result is to be able to copy paste (or manually enter) my configuration values into their respective console programs and have the applications configure the whole CI/CD ordeal without having to click through all the web interfaces. It's now possible in Bitbucket and Azure, but creating the VSTS CI/CD configurations through REST API is proving to be difficult.
Azure 资源 和 Bitbucket 配置目前是通过一个简单的 .NET 控制台应用程序创建的到 REST API.基本上复制粘贴(或手动输入)所有值(azure 输入值/bitbucket 输入值) 到控制台应用程序,它将在 5 分钟内配置所有内容.
Azure resources and Bitbucket configurations are currently created through a simple .NET console application that talks to the REST APIs. Basically copy paste (or manually enter) all the values (azure input values / bitbucket input values)into the console application and it will configure everything within 5 minutes.
现在我面临着尝试在 VSTS 中自动化构建配置和发布配置的困难部分.Microsoft Docs 在 VSTS 客户端库的文档.
Now I face the harder part of trying to automate build configurations and release configurations in VSTS. Microsoft Docs isn't great on the documentation of VSTS client libraries.
老实说,我不知道如何创建构建定义 通过 API 或客户端库.
I'm honestly at a loss for how I can create a build definition through the API or Client Library.
BuildHttpClient 有三种我可以使用的方法:
The BuildHttpClient has three methods I can work with:
public virtual Task<BuildDefinition> CreateDefinitionAsync(BuildDefinition definition, Guid project, int? definitionToCloneId = null, int? definitionToCloneRevision = null, object userState = null, CancellationToken cancellationToken = default(CancellationToken));
public virtual Task<BuildDefinition> CreateDefinitionAsync(BuildDefinition definition, int? definitionToCloneId = null, int? definitionToCloneRevision = null, object userState = null, CancellationToken cancellationToken = default(CancellationToken));
public virtual Task<BuildDefinition> CreateDefinitionAsync(BuildDefinition definition, string project, int? definitionToCloneId = null, int? definitionToCloneRevision = null, object userState = null, CancellationToken cancellationToken = default(CancellationToken));
BuildDefinition 具有以下属性.
namespace Microsoft.TeamFoundation.Build.WebApi
{
[DataContract]
public class BuildDefinition : BuildDefinitionReference
{
public BuildDefinition();
public List<string> Tags { get; }
public PropertiesCollection Properties { get; }
public List<RetentionPolicy> RetentionRules { get; }
public List<Demand> Demands { get; }
public IDictionary<string, BuildDefinitionVariable> Variables { get; }
public List<BuildTrigger> Triggers { get; }
public ProcessParameters ProcessParameters { get; set; }
public BuildRepository Repository { get; set; }
public List<BuildOption> Options { get; }
public List<BuildDefinitionStep> Steps { get; }
public bool BadgeEnabled { get; set; }
public int JobTimeoutInMinutes { get; set; }
public BuildAuthorizationScope JobAuthorizationScope { get; set; }
public string DropLocation { get; set; }
public string Description { get; set; }
public string Comment { get; set; }
public string BuildNumberFormat { get; set; }
public Build LatestBuild { get; }
public Build LatestCompletedBuild { get; }
}
}
如您所见,构建定义的最重要属性是只读的.
As you can see, the most important properties of a build definition are read-only.
如何通过 REST API 创建构建定义?是否有更好的 VSTS 替代方案可以让我这样做?
How do I go about creating a build definition through the REST API? Are there better alternatives to VSTS that will allow me to do this?
推荐答案
REST API 的格式 创建构建定义如下:
The format for the REST API to create a build definition as below:
POST https://{account}.visualstudio.com/{project}/_apis/build/definitions?api-version=5.0-preview.6
应用程序/json 示例:
application/json example:
{
"process": {
"phases": [
{
"steps": [
],
"name": "Phase 1",
"refName": "Phase_1",
"condition": "succeeded()",
"target": {
"executionOptions": {
"type": 0
},
"allowScriptsAuthAccessOption": false,
"type": 1
},
"jobAuthorizationScope": "projectCollection",
"jobCancelTimeoutInMinutes": 1
}
],
"type": 1
},
"repository": {
"properties": {
"cleanOptions": "0",
"labelSources": "0",
"labelSourcesFormat": "$(build.buildNumber)",
"reportBuildStatus": "true",
"gitLfsSupport": "false",
"skipSyncSource": "false",
"checkoutNestedSubmodules": "false",
"fetchDepth": "0"
},
"id": "4ba24767-e5a6-4987-80cc-ebaeca01fdbc",
"type": "TfsGit",
"name": "product1",
"url": "https://marinaliu.visualstudio.com/Git2/_git/product1",
"defaultBranch": "refs/heads/master",
"clean": "false",
"checkoutSubmodules": false
},
"processParameters": {},
"drafts": [],
"queue": {
"id": 324,
"name": "ownPC",
"pool": {
"id": 23,
"name": "ownPC"
}
},
"name": "definitionCreatedByRESTAPI",
"type": "build",
"queueStatus": "enabled"
}
要在 C# 中使用 REST API,您可以进行如下转换:
To use the REST API in C#, you can convert as below:
var personalaccesstoken = "PAT";
var base64Token = Convert.ToBase64String(Encoding.ASCII.GetBytes($":{personalaccesstoken}"));
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", base64Token);
var requestMessage = new HttpRequestMessage(HttpMethod.Post, "https://account.visualstudio.com/project/_apis/build/definitions?api-version=5.0-preview.6");
requestMessage.Content = new StringContent("{"process": { "phases": [{"steps": [], "name": "Phase 1","refName": "Phase_1","condition": "succeeded()","target": { "executionOptions": { "type": 0 },"allowScriptsAuthAccessOption": false, "type": 1 }, "jobAuthorizationScope": "projectCollection", "jobCancelTimeoutInMinutes": 1 }],"type": 1 }, "repository": { "properties": { "cleanOptions": "0","labelSources": "0","labelSourcesFormat": "$(build.buildNumber)", "reportBuildStatus": "true","gitLfsSupport": "false", "skipSyncSource": "false","checkoutNestedSubmodules": "false", "fetchDepth": "0"},"id": "4ba24767-e5a6-4987-80cc-ebaeca01fdbc","type": "TfsGit","name": "product1", "url": "https://marinaliu.visualstudio.com/Git2/_git/product1", "defaultBranch": "refs/heads/master", "clean": "false","checkoutSubmodules": false },"processParameters": {}, "drafts": [],"queue": { "id": 324, "name": "ownPC","pool": {"id": 23, "name": "ownPC"}}, "name": "definitionCreatedByRESTAPI", "type": "build","queueStatus": "enabled"}", Encoding.UTF8, "application/json");
HttpResponseMessage response = client.SendAsync(requestMessage).Result;
response.EnsureSuccessStatusCode();
通过参考博客Accessing TFS/VSTS 2017 programmatically 用于 C#程序.
By referring the blog Accessing TFS/VSTS 2017 programmatically for the C# program.
这篇关于如何通过 VSTS REST API 创建构建定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何通过 VSTS REST API 创建构建定义
基础教程推荐
- 如何激活MC67中的红灯 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30