Automate Mass Approvals in Azure DevOps Pipelines with Canvas Apps and Power Automate
Streamline Azure DevOps Approvals: Automate Multi-Tenant Pipeline Deployments with Canvas Apps and Power Automate
If you're using Azure DevOps (ADO) pipelines, chances are you're leveraging Azure DevOps environments for gate approvals. This setup provides excellent control over when to promote a new version to the next environment.
However, Azure DevOps' UI lacks the capability to approve multiple pipelines simultaneously. Recently, I faced a situation where I had to deploy to several environments across multiple tenants, leading to nearly 60 approvals. This quickly became tedious, making it a perfect candidate for automation.
Below an explanation how I managed this in about an hour.
Azure DevOps pipeline approvals
To streamline the approval process, I developed a solution using two cloud flows in Power Automate and a Canvas App. I didn’t spend much time on UI or UX. It just had to work.
Cloud Flows
I use two cloud flows to manage the approvals for the pipelines:
Fetch the Azure DevOps pipeline approvals
Approve the Azure DevOps pipeline
1. Fetch the Azure DevOps pipeline approvals
We can retrieve pipeline approvals using Azure DevOps' REST API with the "Send an HTTP request to Azure DevOps" action. Here's how to configure it:
Create a flow and name it ‘Fetch Azure DevOps pipeline approvals’
Use the Power Apps V2 trigger to initiate the flow from the Canvas App.
Add the "Send an HTTP request to Azure DevOps" action to fetch approvals.
Set the organization and project names accordingly.
Choose GET as the method and use the following relative URI:
https://dev.azure.com/<organization>/<project>/_apis/pipelines/approvals?api-version=7.1
Change <organization> and <project> to your DevOps organization and project. You can fetch the exact name of your organization and project from your URL bar, don’t forget that some characters such as spaces are encoded to become ‘
%20
’ so check the exact name in your url.Add the ‘Response’ step to respond to the Canvas app with the information from the request to ADO.
-
Leave the Status Code ‘200’
For the body, set the body of the ‘Send an HTTP request to Azure DevOps’ step.
For the Response Body JSON Schema you can use the following schema:
{ "type": "object", "properties": { "count": { "type": "integer" }, "value": { "type": "array", "items": { "type": "object", "properties": { "id": { "type": "string" }, "steps": { "type": "array" }, "status": { "type": "string" }, "createdOn": { "type": "string" }, "lastModifiedOn": { "type": "string" }, "executionOrder": { "type": "string" }, "minRequiredApprovers": { "type": "integer" }, "blockedApprovers": { "type": "array" }, "_links": { "type": "object", "properties": { "self": { "type": "object", "properties": { "href": { "type": "string" } } } } }, "pipeline": { "type": "object", "properties": { "owner": { "type": "object", "properties": { "_links": { "type": "object", "properties": { "web": { "type": "object", "properties": { "href": { "type": "string" } } }, "self": { "type": "object", "properties": { "href": { "type": "string" } } } } }, "id": { "type": "integer" }, "name": { "type": "string" } } }, "id": { "type": "string" }, "name": { "type": "string" } } } } } } } }
Otherwise, it is always easiest to use the ‘Generate from sample’ button and fill in the result of a previous test run of the HTTP request to Azure DevOps.
That it for fetching the Azure DevOps approvals. Your flow should look like this now:
Expanded flow:
2. Create a Canvas application to add the flow
Follow these steps to integrate the flow into a Canvas App:
Create a Canvas application and add a new screen with a Header and Footer container to save some time. Call it Pipeline Approvals.
Add the flow you just created by clicking on the three dots on the left and finding the Power Automate menu.
Place a button at the top to run our flow and fetch our approvals
- Place the code on this button to run the flow. Be sure to wrap a global variable (
colApprovals
) around it to collect the approvals.
- Put a gallery in the MainContainer and place the colApprovals variable on the gallery. Also put some labels in to show .name, .status and .CreatedOn
3. Create the Azure DevOps pipeline approvals flow
To release the pipeline and set its status to "Approved," we send another HTTP request to Azure DevOps. This time we use a patch though, and we give a small JSON array along in the body. This JSON array has to include the Approval ID which we can fetch from our previous flow run results.
Create a flow and name it ‘Approve Pipeline Run’
Use the Power Apps V2 trigger to initiate the flow from the Canvas app again.
This time add an Text input with the name ‘ApprovalID’
Add a compose action and call it varApprovePipeline.
Add the following JSON array in this compose action.
[ { "approvalId": "triggerBody()['text']", "comment": "Approving", "status": "approved" } ]
The triggerBody()[‘text’] is your Text input on the Power Apps V2 trigger
Choose the ‘Send an HTTP request to Azure DevOps’ again.
Set the organization name to the same organization name you used in the previous flow.
As a method use PATCH this time.
For the relative URI use the following URI:
https://dev.azure.com/<organization>/<project>/_apis/pipelines/approvals?api-version=7.1
Change <organization> and <project> to your DevOps organization and project just like before.
Put the outputs of the compose action from before in the body.
Your flow should like like this:
Expanded flow:
4. Add the Approve Pipeline Run flow to your canvas application
Finally, integrate the approval flow into the Canvas App by triggering it through a button placed in the gallery. To streamline the process further, add a ForAll loop to approve all pipelines simultaneously.
Add the Approve Pipeline Run like we did with the previous flow
Add a button in the gallery called ‘Release’ and trigger the flow on the OnSelect property of the button using
ApprovePipelineRun.Run(ThisItem.id)
Lastly, add a button to approve all pipeline runs and use the following code to approve all pipelines listed in the gallery.
ForAll( galApprovals.AllItems, ApprovePipelineRun.Run(ThisRecord.id) );
Mission accomplished!
By leveraging a Canvas App and Power Automate, you can automate and simplify the tedious task of approving multiple Azure DevOps pipelines. This solution not only saves time but also reduces the risk of human error, enhancing your deployment process's efficiency and reliability.