gpt4 book ai didi

c# - 如何通过 VSTS REST API 创建构建定义

转载 作者:行者123 更新时间:2023-11-30 15:54:32 25 4
gpt4 key购买 nike

情况:

我目前正在通过 bitbucket -> vsts -> azure 自动化 CI/CD 配置。

我的理想结果是能够将我的配置值复制粘贴(或手动输入)到它们各自的控制台程序中,并让应用程序配置整个 CI/CD 过程,而无需单击所有 Web 界面。现在可以在 Bitbucket 和 Azure 中使用,但事实证明通过 REST API 创建 VSTS CI/CD 配置很困难。

Azure resources和 Bitbucket 配置目前是通过与 REST API 对话的简单 .NET 控制台应用程序创建的。基本上将所有值 ( azure input values/bitbucket input values ) 复制粘贴(或手动输入)到控制台应用程序中,它将在 5 分钟内配置所有内容。


问题:

现在,我面临着尝试在 VSTS 中自动生成配置和发布配置的困难部分。 Microsoft Docs 在 documentation of VSTS client libraries 上不是很好.

老实说,我不知道该怎么做create a build definition通过 API 或客户端库。

  • BuildHttpClient我可以使用三种方法:

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

如您所见,构建定义的最重要属性是只读的。

如何通过 REST API 创建构建定义?有没有比 VSTS 更好的替代品可以让我做到这一点?


最佳答案

REST API 的格式为 create a build definition如下:

POST https://{account}.visualstudio.com/{project}/_apis/build/definitions?api-version=5.0-preview.6

应用程序/json 示例:

{
"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,您可以按如下方式转换:

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# 程序。

关于c# - 如何通过 VSTS REST API 创建构建定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50288144/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com