我正在尝试自动下载构建工件。目的是下载一个特定的构建工件。当我调用 BuildClient.GetArtifactContentZipAsync
它抛出以下错误
我使用的是以下代码
private static void DownloadBuildResults(Build StartedBuild, string ArtifactName)
{
//BuildArtifact drop = BuildClient.GetArtifactAsync(StartedBuild.Id, ArtifactName).Result; //get detiled info
//Console.WriteLine("Build location: " + drop.Resource.DownloadUrl);
//string dropFileName = String.Format("{0}_{1}.zip", StartedBuild.Definition.Name, StartedBuild.BuildNumber);
Stream zipStream = BuildClient.GetArtifactContentZipAsync(79414, "abc").Result; //get content
using (FileStream zipFile = new FileStream("abc.zip", FileMode.Create))
zipStream.CopyTo(zipFile);
请注意,我 buildID 和 文物名 是正确的。我已经仔细检查了这些值。上面的代码示例取自 https:/github.comashamraiTFRestApiblobmaster19.TFRestApiAppQueueBuildTFRestApiAppProgram.cs。
这是APISDK中的一个bug吗? 还是我做错了什么?
https:/gist.github.comabhishekgoenka9759256f995e7f7c9cbcb7872c8591c0。
堆栈跟踪
at Microsoft.VisualStudio.Services.WebApi.VssHttpClientBase.<HandleResponseAsync>d__53.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.VisualStudio.Services.WebApi.VssHttpClientBase.<SendAsync>d__51.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.TeamFoundation.Build.WebApi.BuildHttpClientBase.<GetArtifactContentZipAsync>d__10.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at Utils.Dashboard.FrmAzureDashboard.<BtnDownloadKHP_Click>d__13.MoveNext() in C:\code\MetaFXTools\ui\Utils\Dashboard\FrmAzureDashboard.cs:line 118
这是APISDK中的一个错误吗? 还是我做错了什么?
我可以确认 编码从阿沙姆拉伊 是正确的(我用我的VS2019 16.6测试了一下,效果不错。)还有可能是你的变量值有问题。我建议你用 try-catch
呼叫 DownloadBuildResults
函数,这样你就可以在这里分享详细的错误信息。我只是把你上面的代码改了一下。
class Program
{
static readonly string TFUrl = "https://dev.azure.com/YourOrganizationName/";
static readonly string UserPAT = "YourPAT";
static void Main(string[] args)
{
try
{
int buildId = 832; // update to an existing build definition id
string artifactName = "drop"; //default artifact name
ConnectWithPAT(TFUrl, UserPAT);
Stream zipStream = BuildClient.GetArtifactContentZipAsync(buildId, artifactName).Result; //get content
using (FileStream zipFile = new FileStream(@"C:\MySite\test.zip", FileMode.Create))
zipStream.CopyTo(zipFile);
Console.WriteLine("Done");
}
catch (Exception ex)
{
Console.WriteLine("Exception: " + ex.Message);
if (ex.InnerException != null) Console.WriteLine("Detailed Info: " + ex.InnerException.Message);
Console.WriteLine("Stack:\n" + ex.StackTrace);
}
}
}
所以你需要确认一下
1.你有一个有效的尚未过期的PAT,并且这个PAT有访问build artifact的权限。然后直接把pat输入到 static readonly string UserPAT = "xxxxxxxxxxxxx";
. 应该是 "PAT"
而不是 "<PAT>"
.
2.URL(TFUrl)只需要OrganizationName作为一部分。
3.确保选择正确的工件名称。
4.区别于 BuildID
和 BuildDefinitionID
.
buildDefinitionID
编辑管道时。BuildID
当在门户网站上检查管道运行结果时。你应该使用 BuildID
作为输入。
5.另外,你运行的build(BuildID)应该有发布的artifact。
你的流水线应该有 发布构建工件 任务。5.检查日志,确保有东西发布,要发布的文件夹不能是空的。
6.由于你只是通过Download artifact函数来测试代码,所以在你运行代码之前,确保你queuerun build成功。(等流水线运行结束后再运行)。
希望以上的内容对解决你的问题有一定的帮助:)