How can I access the artefact from a pull request for which a build was run?

Viewed 882

I'm using azure for a windows app and so I don't really need to go as far as CD as that isn't really relevant. We eventually plan to move to the cloud but for now that is not the case.

I have now got my .net (c#) build running as a build pipeline and I have a develop branch which Pull Requests are used to merge in changes. What I want is for a tester to be able to pick up the build artefact that was created for a particular bug or product backlog item when the pull request was successfully completed. Is this possible without having a Release Pipeline? I don't currently have a subscription that would allow me to create a release pipeline.

3 Answers

How can I access the artefact from a pull request for which a build was run?

Indeed, just as Lucas said, if we are starting from the pull request to solve this problem, it is really difficult. But we could try reverse thinking to start with the build pipeline.

Azure devops provided us some predefined variables, like Build.BuildId, System.PullRequest.PullRequestId.

So, we could use the REST API Pull Requests - Update in the build pipeline to update the comment with the link to the artifact.

PATCH https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/pullrequests/{pullRequestId}?api-version=5.1

Since the current build is triggered by a pull request, we could use the predefined variable System.PullRequest.PullRequestId to get the pullRequestId directly.

Now, we just need to get the link of the artifact, we could use the Artifacts - Get to get the artifact info:

GET https://dev.azure.com/{organization}/{project}/_apis/build/builds/{buildId}/artifacts?artifactName={artifactName}&api-version=4.1

We could get the buildId by the predefined variable Build.BuildId, then we will get the download URL:

enter image description here

So the idea of summing up my solution is to use REST API Pull Requests - Update in the build pipeline to update the comment of the pull request, which contains the download path of the artifact.

Note: You could also add custom conditions in the REST API task in the build pipeline:

and(succeeded(), eq(variables['Build.Reason'], 'PullRequest'))

So, with this setting, only the build triggered by the pull request will execute this rest api task.

Hope this helps.

The pull request build that you defined can publish artifacts, see this documentation to see how to do it.

Once those artifacts are published, your tested can browse/download them on the build run page (click on header pa> "Related" > click on "#number of artifacts you publish# published").

Alternatively, you could add a task to copy your artifacts to an Azure Blob Storage, but that would require more configuration.

One can find build id with branchName filter :

GET https://dev.azure.com/{organization}/{project}/_apis/build/builds?api-version=5.0&reasonFilter=pullRequest&definitions={buildDefinitionId}&branchName=refs/pull/{pullRequestId}/merge

Once builder id has been retrieved, get artifact by name :

GET https://dev.azure.com/{organization}/{project}/_apis/build/builds/{buildId}/artifacts?artifactName={artifactName}&api-version=6.0
Related