How to run one release at time on Azure DevOps? - azure-devops

I am to implement pre-deployment gates on my azure devops release.
I am using HTTP request gate to check each time if there is ongoing deployment.
The problem is, when deployment on one release finishes (stage 1 ), I want to prevent the deployment on next release in queue if there is (stage 2) to the pipeline that does not do deployment for example.
The end goal is, no matter how many releases in queue I have and no matter how many agents in my pool, I want to make sure that release start only if there is no other active release in deployment/other stages.

You can add a gate of Invoke REST API: GET to call below Deployments - List rest api and check if there are any releases in deployment. See below steps:
https://vsrm.dev.azure.com/{organization}/{project}/_apis/release/deployments?operationStatus=queued,queuedForAgent,phaseInProgress&api-version=6.1-preview.2
1, Add a Invoke Rest Api Gate
Go to Pre-deployment conditions --> Enable Gate-->Add a Invoke Rest Api Gate-->Click Manage to create a service connection. See below screenshot.
2, Create a service connection shown in above screenshot.
When you Click Manage in above screenshot. You will be forward to service connections page. Click new service connection and select Generic type of service connection:
The server Url: https://vsrm.dev.azure.com/{Org}/{Proj}
Password: Personal Access Token
3, Go back to the Invoke Rest Api Gate. Select the service connection just created: See below
Method: Get
URL suffix and parameters: /_apis/release/deployments?operationStatus=queued,queuedForAgent,phaseInProgress&api-version=6.1-preview.2
Success criteria: eq(root['value'][0], '')
Above Invoke rest api gate will call the deployments list rest api to filter these releases that are inProgress or queued of deployment using operationStatus filter(ie. operationStatus=queued,queuedForAgent,phaseInProgress). If the there is any deloyment is inProgress or queued the Success criteria: eq(root['value'][0], '') will failed.
If you using self-hosted agents and Environments. You can check the Exclusive lock on Environments and Agent pools.

Related

How to trigger my "Incoming Webhook" Service Connection in Azure DevOps (OnPremise)? The Endpoint URL is unknown

Overview: I try to trigger an Azure DevOps 2020 Pipeline (YAML) in the event that a WorkItem of a specific type is created. So my idea I had was to create a connection between "WorkItem" -> Service Hook of Type "Web Hook" -> Service Connection of Type "Incoming Webhook" -> resource:webhook triggers the Pipeline in YAML.
My problem now is, that I could create the serviceendpoint by GUI or by API, but the documentation and the created endpoint does not make sense for me. I am running my server on-premise and the endpoint I got looks like this:
{"count":1,"value":[{"data":{},"id":"1babbef7-1edb-4b01-bf18-b6e3c309caae","name":"TestIncomingWebhook","type":"incomingwebhook","url":"https://dev.azure.com", ...
The url does not make sense to me.
I can alter the url if I use the REST API to create the endpoint by using this format
POST https://{instance}/{collection}/{project}/_apis/serviceendpoint/endpoints
as described in Microsoft Azure API Documentation.
Questions:
Why is the url that the service connection gets pointed to https://dev.azure.com while I am running on-premise from a server on a totally different url? Would that be the url I have to POST to trigger the ServiceConnection?
What must be the correct URL-Format for my local Server? Where should I point that URL to?
How can I trigger the Service Connection from curl? Do I have to use GET with a lot of parameters or POST with a JSON payload?
I would appreciate if someone could help me out on this. Perhaps someone even got a full solution on how to trigger pipelines on workitem events. I searched a lot but could not find a solution, yet.
Thanks, Peter.
I created the WebHook and ServiceConnection from the GUI like this:
WebHook Configuration Screenshot
ServiceHook Configuration Screenshot
My real problem is, that I got no feedback what the correct ServiceHook URL is. I have no idea where to point my WebHook.
Addition:
The correct URL for accessing the Incoming Webhook Service Endpoint in Azure DevOps is https://{instance}/{Collection name}/_apis/public/distributedtask/webhooks/{Service Connection name}?api-version=6.0-preview
This should be related to your machine settings, you could share the detail steps here
Since you are using Azure DevOps Server 2020, the url format for your local Server should be https://{instance}. You could open Azure DevOps Server Administration Console to check it.
It will trigger the webhook via the event you selected.
You could refer to this ticket for more details.
In addition, you could try this Logic Apps then create flow to trigger build when specific type is created
Update1
Create Service connection Incoming WebHook, eg. Web Hooks name is HookTests and Service connection name is TestHook
Create Web Hooks and enter the URL https://{instance}/{Collection name}/_apis/public/distributedtask/webhooks/{Web Hook name eg. HookTests}?api-version=6.0-preview
Create YAML build and add service connection resources in the yaml pipeline see below:
resources:
webhooks:
- webhook: MyWebhookTrigger
connection: {Service connection name, eg.TestHook}
Above steps actually do below things:
New workitem is created-->automatically Trigger Azure pipeline.
In addition, we could also add task power shell and call REST API to trigger another pipeline.

Cancel external service that runs PR status checks when PR is abandoned in Azure DevOps

We have an external service that gets triggered with every pull request to our repo in Azure DevOps, runs checks, and posts a status check back to the PR. This service is very resource heavy. Sometimes PR authors abandon the PR before the service finishes running. We are trying to find a way we can be notified of the PR being abandoned so we can also cancel orchestration and save resources. Is there a mechanism to do that?
We are trying to find a way we can be notified of the PR being abandoned so we can also cancel orchestration and save resources.
You could set the notification in Azure Devops Project Settings -> Notification -> New Subscription.
You could select the A pull request is created or updated and set the filter and target users.
When a pull request is Abandoned, the users will get the notification email.
On the other hand, you can also get the Pull Request Status via the Rest API: Pull Requests - Get Pull Request
GET https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/pullrequests/{pullRequestId}?api-version=6.0
Update:
You could try to create a custom workflow(e.g. Power Automate) to check the Pull Request status.
Here is an example:
Explanation:
1.The Recurrence action is set 10 minutes interval(you could set the suitable interval ). Then the flow will execute every 10 minutes to check the pull request status.
2.The Send Http Request to Azure Devops action is used to run the API above to get the Pull Request status.
3.The Condition action is used to compare the response and the value.

How to modify the service endpoint of an Azure Pipeline via API?

I want to modify the service endpoint of a pipeline in Azure DevOps via API.
Example:
Pipeline "build-a-release" uses my personal service endpoint "hello1". But I want it to use existing service endpoint "my-companys-global-service-endpoint" instead.
The documentation https://learn.microsoft.com/en-us/rest/api/azure/devops/pipelines/pipelines?view=azure-devops-rest-6.0 does not show any "update" function, but I can update the service endpoint of a pipeline in the Azure DevOps GUI.
How can I automate this process via a script and API?
To update a service endpoint, you should use Endpoints - Update Service Endpoint API:
PUT https://dev.azure.com/{organization}/_apis/serviceendpoint/endpoints/{endpointId}?api-version=6.0-preview.4
To update a pipeline defintion, you could use Definitions - Update api to update it. When you capture the network log, you could see there is connectedServiceId in properties of repository parameter. You could update the connectedServiceId to change the service connection.
From my test, something you need to notice:
Make sure the request headers include the following items:
Make sure you have the correct "revision" value (the latest revision) in the body.

In Azure Devops I want my yaml pipeline only to execute after the previous execution of the same pipeline has completed

I tried batch: true setting described here, but it seems to:
ignore commits that are pushed when a pipeline is running. I want the last commit to trigger a pipeline after the current run of that pipeline has finished
be ignored when you publish directly from CI by pressing build
Has someone found a way to configure a pipeline to run as I have described.
You can try adding a Invoke REST API check on the agent pool. The rest api will get the previous build and evaluate its status. So when new build is queued targeting the agent pool. The Invoke REST API will be invoked, this new build will only start when the response of the rest api is evaluated to true.
Please check below steps:
1, create a service connection to your azure devops organization.
Go Project Setting--> Service connections under Pipelines-->Click new service connection--> select Generic to create a generic service connection.
Then Edit the service connection type the information shown in below screenshot. Check here to get a Personal access token.
2, add Invoke REST API check on the agent pool**.
Go Project Setting--> Agent Pools under Pipelines-->Select the agent pool--> Click the 3dots -->Click Approvals and checks.
.
3, Click the "+"--> Choose Invoke REST API
4, Edit the Invokde Rest API
Select the generic service connection to your azure devops created in the first step.
Set the fields as below:
URL suffix and parameters: _apis/build/builds?definitions=the DefinitionId of your pipeline&$top=2&queryOrder=queueTimeDescending&api-version=5.1
Success criteria: eq(root['value'][1]['status'], 'completed')
please check here for more information about build rest api.
Note: Since the Invoke Rest api check is set on agent pool scope. It may have effects on other pipelines that target this agent pool. For example, if the desired yaml pipeline is waiting its previous run to complete. And now another pipeline targeting this same agent pool is triggered, it will have to wait for the previous run of desired yaml pipeline to complete too.

Create service connection and use the same in next stage of azure devops multi stage yaml pipeline

I have two yaml templates defined one for creating a docker registry service connection and second for deploying some stuff via container job. The second template uses the docker registry connection which is being deployed in first template. When I am running both the templates separately then both the stages are successful but when I run them in one azure-pipelines.yaml, it fails :
There was a resource authorization issue: "The pipeline is not valid. A service connection with name shared-stratus-acr-endpoint could not be found. The service connection does not exist or has not been authorized for use. For authorization details, refer to https://aka.ms/yamlauthz."
Is there any way like dependsOn or condition that we can provide in this situation?
It's likely that you only authorized the service connection for the individual template\pipelines when you created them. The workflow is not super friendly.
What if you try and authorize the pipeline that is failing for that service connection explicitly. See docs here
You could also just authorize the service connection for all pipelines depending on your security needs.