Search code examples
tfsbuildtfs-2015

TFS 2015 build definition to find the artifact location of another build


I am creating a promote build definition which takes the output [artifact] from the last successful Continuous Integration build and deploys the same to a higher environment. This is TFS 2015 so I am using BuildHttpClient

VssConnection connection = new VssConnection(new Uri(accountUrl), new VssAadCredential());
var projectClient = connection.GetClient<ProjectHttpClient>();
var project = projectClient.GetProject("project.name").Result;
var buildClient = connection.GetClient<BuildHttpClient>();
var builds = buildClient.GetBuildsAsync(project.Id, maxBuildsPerDefinition: 1, ResultFilter: BuildResult.Succeeded).Result;
    foreach (var build in builds)
    {
     if (build.Definition.Name == "CI BuildDefinition Name")
                            {

this is where I am trying to read the path to artifact for this build. but Build class does not have an artifact or artifact path property.

However I did find that there is an ArtifactResource class that has Data that I may use.

so I am trying to do something like:

ArtifactResource art = buildClient.GetArtifactAsync...
string PathToArtifact =art.Data;

and I am missing it. I also realize that maxBuildsPerDefinition: 1 in builds

does not seem to work, there are no compile errors but what would I need to do if I want the latest successful build?


Solution

  • GetBuildsAsync calls Get Builds api which does not include build artifacts information. So you need to use GetArtifactAsync to get the information for artifact. You may need to use GetArtifactsAsync if there are several artifacts in one build.

        BuildArtifact ba = buildClient.GetArtifactAsync("ProjectName", buildId, "artifactname").Result;
        string downloadurl = ba.Resource.DownloadUrl;
        string localpath = ba.Resource.Properties["localpath"];
        string artifacturl = ba.Resource.Url;