Azure Devops build custom conditions -target previous build - azure-devops

Is it possible to target the previous build in a custom condition?
What I try to accomplish:
Post a message to Slack when build fails (this is easy)
Post a message to Slack when a build changes status from failed to success (I don't want all successful builds posted to Slack, only the first one after a failed build)

No, it's not able to target the previous build in a custom condition. Custom condition can only target in the same build. You can add a task to use REST API checking the previous build result and current build result, and determine whether to post a message to Slack.

One way would be to retrieve the latest build and read the build status using the Rest API. The Slack message would then be conditional on previous BuildResult == succeeded & current BuildResult == failed.
You can get information about the previous build using the azure-devops rest API here:
Azure DevOps Services Rest API GET Builds List

Related

Notification when build is fixed in devops

I can enable notifications for completed builds and failed builds but is there a way to
have notification for fixed builds?
That would mean builds that failed in the previous commit but does not fail in the most current one.
According to the About notifications documentation, there are only notifications for completed builds and failed builds.
Looks like the closest we can get out of the box would be to create a Service hook to notify a Service of a successful build.
A solution closer to your needs would be to build your own API, call that from the Service hook and determine if the build has been fixed there.

Is there a way via API to track triggered builds in Azure DevOps?

With the classic build pipelines and classic triggers, it was easy enough to track builds that were triggered by completion of other builds by just polling for builds requested by the same user.
Now, with resource triggers, the requested by property switching to the build service account instead of the original author of the triggering commit.
I have been going through the documentation to try and find another way to see triggered builds from the original build ID but have not found anything.
There is an "Associated Pipelines" tab on the build summary page that at least has the pipelines containing the triggered builds, but I cannot find anything to get that by API either.
According to your description, you can first call the REST API to get all the running build pipelines in the definition, and then use the powershell script to loop check the id in the parameter triggeredByBuild in the request body in the specific build so that you can see triggered builds from the original buildId.
Note: The id marked in the attachment is the original buildId that triggered another build pipeline.

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

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.
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:
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

Service web hook - get artifacts of succeeded build?

We're using Azure Pipelines to build our project. We configured a Service Hook for the Event "Build completed" and everything is working as planned.
However, we don't receive information about the build's artifacts. How to obtain a specific artifact from the build in the web hook?
Unfortunately the information about the build artifacts not passed in the Build Completed JSON.
What you can do it's after you catch the event make a Rest API call in your service to get the artifacts details according the build id (build id you of course got in the service hook json).
The Rest API to get artifacts details is Artifacts - List.

VSTS: How to get release ID triggered by a build using REST API

I have a release definition in VSTS configured to trigger automatically based on when a particular build completes (and publishes an artifact). If I look at the build summary UI page in the browser, there's a section labeled "Deployments" that shows me the release definitions that were triggered due to that build completing. How can I use the VSTS REST API to query for the releases triggered due to a completed build (i.e. the information shown on that web page)?
I tried looking at the results of a REST query to get build details, e.g. https://{account}.visualstudio.com/DefaultCollection/{project-guid}/_apis/build/Builds/7420 but the JSON that comes back doesn't appear to mention the word "release" or "deployment" or "environment" anywhere in it.
From what I can tell there is no way from the build query result to find the triggered release, but you can instead query for releases that were triggered by that build.
E.g. if the build that completed was number 123 then you could find all releases triggered by that build:
https://{account}.vsrm.visualstudio.com/{project}/_apis/release/releases?sourceId={projectID}:{buildDefinitionId}&artifactVersionId=123&api-version=4.1-preview