I've previously blogged about my architecture patterns for delegated deployments for connections using oAuth. This pattern allows you to change the connections of a connection reference during a deployment to another account. The benefit of this is that you can force which account using oAuth (Dataverse, SharePoint etc) is used in your downstream environments. While I talked about the pattern and it's benefits, I didn't show how to accomplish this, that's what this post is for.
Trigger your flow with the "When an action is performed" trigger. You can add additional filter triggers if you have multiple pipelines triggers from the same custom host, or you can add a switch statement in, to handle it separately.
After, we get the deployment run, this gives us details of the deployment. Get the Stage Run ID from the trigger and pass that to a get row action for the "Deployment Stages Runs" table.
Next we have a Do-Until Loop that loops through and checks to see if the solution artifacts have been exported and saved to the Dataverse table "Deployment Artifacts". This loops through and there is a delay step which delays at 1 minute intervals which prevents unnecessary API calls with loops that aren't needed.
In the Do-Until loop, I used an Empty() function to check to see if it contains data.
Next a Check Condition to see if the Deployment Settings Contains data.
If it doesn't, go down the True path (this is the way my brain works, switch the logic if you want ) and just approve the deployment from the unbound action.
Else we will be re-writing the connections, so lets take a look at the False path.
Start off with your array of connections in your target environment. This could be a Environment Variable, which would make sense, especially if you will be moving solutions through UAT, Test, Prod etc and need to change the connections based on the Environment. In this example, I just create an array and hard code the values of the connections that I want to map my importing connection references to. This is actually a template JSON of the deployment settings, the array I refer to is the array of connection references.
The below is a sample JSON from this step. The environment variable array is blank as I do not intend to populate that, but you also can do. The connection references include a Logical Name, Connection ID and Connector ID
What we want to do is ignore the Logical Name, use the Connector ID to filter and then swap the Connection ID.
To do this, I first pass this step through a Parse JSON action to get the dynamic content easier.
Again for ease, Parse the JSON of the Deployment Setting JSON from the Get Deployment Run step.
Next we do the complicated part, we have to merge the arrays in a way that builds it from the parts that we want from the two arrays. First create a loop based on the connection references of deploying connections Parsed JSON.
Then we filter the array of connectors. This is where the magic happens.
After this, we create an array of the connections, because this loop and filter, arranges the data in a way we can now shape.
Next, outside the loop we add a step to create the Deployment Settings JSON. Here I am using the Environments Variables from the Parse JSON step of the deploying solutions deployment settings. As mentioned previously, you can change them in a similar way to what we are doing with connection references. We also reference the combine array step from the array.
Using Pieters Method described above, we get the combined array of connection references that are now mapped to the new connections in the target environments. Update the Deployment Settings in the record and deploy using the unbound action.
And that's it. Bit complicated but once it's setup, it's good to go. These are the detailed instructions but I've also exported this as a template and uploaded it to my GitHub and you can download it here:
<<Download the Template Flow>>
There are notes in the flow and what each step does, so hopefully between those and this, you shouldn't have any issues setting this up.
Ciao for now!
MCJ
Original Post https://www.mattcollinsjones.co.uk/single-post/change-connections-during-deployments-power-platform-pipelines