Service web hook - get artifacts of succeeded build? - azure-devops

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.

Related

How to send SonarQube report to developers whenever the Azure DevOps build succeeded or failed

I have integrated the SonarQube tasks in Azure DevOps build pipeline. I can be able to successfully the scan my application source code using SonarQube.
I want to share the SonarQube report to Developers whenever the Sonar build succeeded or failed.
For notification settings, first of all, you need to have mail server or you can use popular ones (Gmail and etc.). Have a look at the below article for the every step you need to perform ;
https://www.fosstechnix.com/sonarqube-email-notifications/
Another possible way is if you have limitations in the Sonarqube server;
Create quality gate in Sonarqube based on your profile
https://docs.sonarqube.org/latest/user-guide/quality-gates/
Use following plugin to break your pipeline in case of code were not passed the gate
https://marketplace.visualstudio.com/items?itemName=SimondeLang.sonar-buildbreaker
Create mail notification in Azure Devops to send notification when build failed because of Sonarqube quality gate;
https://learn.microsoft.com/en-us/azure/devops/notifications/about-notifications?view=azure-devops#team-and-project-level-notifications
And as shown below you will see the reason about why pipeline build failed in the mail

Unauthenticated API for download of an azure pipeline artifact

The azure pipelines web interface provides a link to download pipeline artifacts on public pipelines regardless of whether your session is authenticated or not. However, the api for getting a download link does not work in this case. HOWEVER however, the download link produced by that API does work if you copy it into an unauthenticated session. How can I obtain a download URL for a public pipeline artifact without authenticating and without scraping the public web interface?
My use case is providing a tool to manually debug CI runs that work locally but fail in CI by replicating the CI environment in a docker container and downloading the exact build used in the failing CI run (a pipeline artifact). The interface I would like to provide for this is a command that takes as an argument a URL of a Github pull request, parses the relevant pipeline parameters out of it, and launches the docker container with the URL of the artifact to download. This should work, and it has worked in the past by scraping the artifact download URL out of the public azure web interface, but this breaks frequently.

After Azure DevOps release pipeline of a Java web app in App Service, URL still shows the default page

I am new to Azure DevOps. I am trying to perform small home lab projects. One of them is to deploy a "Greetings from Spring Boot!" web app in Azure Web App service with Azure DevOps pipelines, build and release.
I got the source code (Spring boot web app) from a YouTube tutorial and performed the tasks in the tutorial but using GitHub actions (Azure Portal/Home/Resources/select my App Service/Deployment Center/Settings/ configure as source a GitHub repository where my code is). This way was fine. The web page https://my-app-name-just-example.azurewebsites.net/ is showing the message in my source code, "Greetings from Spring Boot!".
But when I try to use Azure DevOps build and release pipelines on the same Azure Web App resource, accessing the URL shows the default web page "Hey, Java developers!". Both build and release pipelines show successfully completed.
https://imgur.com/a/thiMfBQ
From the Azure DevOps build release I downloaded the jar file (demo-0.0.1-SNAPSHOT) and ran it locally on my PC (java -jar \path\demo-0.0.1-SNAPSHOT) and http://localhost:8080/ displays the message in my source code. This means the build pipeline was correct. When I configure and run the release pipeline using as source the build from the build pipeline, the release pipeline shows successfully completed but the web app URL still shows the the default web page "Hey, Java developers!".
I checked https://my-app-name-just-example.scm.azurewebsites.net/wwwroot/ and the demo-0.0.1-SNAPSHOT.jar is there. I stopped and started the Azure Web App too.
Any suggestions? Thank you.
#Jason Pan, Thank you very much for the solution.
I used as a solution the startup command in a new Release pipeline configuration. In my case: "java -jar /home/site/wwwroot/demo-0.0.1-SNAPSHOT.jar --server.port=80". I noticed the name of the artifact in my case from the Build pipeline is artifactId+version from the pom.xml file, so demo-0.0.1-SNAPSHOT.jar .
Then I edited my release pipeline with this startup command, saved it, deploy it and I could see the in the Release pipeline /Initialize job logs the message "[PARAMETERS_STARTUPCOMMAND] --> [java -jar /home/site/wwwroot/demo-0.0.1-SNAPSHOT.jar --server.port=80]" . Deployment was successful. And now Web App URL shows the content of the source code, "Greetings from Spring Boot!". Previously it was showing the default content, the "Hey, Java developers!".
Thank you again.
You can add startup command on portal.
Like :
java -jar /home/site/wwwroot/app.jar --server.port=80
For more details, you can reader offical doc and related post.
1. Built-in images
2. Unable to start spring boot application deployed as a jar
3. Deploy a Spring Boot Gradle application to Azure App Service from an Azure 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

Azure Devops build custom conditions -target previous build

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