Hey Friends! Happy Sunday 🤗
Environment variables and connection references are those difficult objects we have to fiddle around with when working with deployments through Azure DevOps for solutions on the Power Platform…
In my last couple of posts we looked at how to create build and release pipelines for deploying solutions we build with Low Code on the Power Platform. In this post, we’ll take this a little further and look at how we can resolve some issues around populating environment variables and replacing connection references through some changes in our pipeline!
Previous content
Did you check out my previous posts
How can we achieve it?
So, let’s first discuss how we can actually make this work. When we use the Power Platform Build Tools to import a solution during a release pipeline, we have the option to use a deployment settings file.
This file will be a JSON file containing the environment variables and connection references in the solution, as well as values that should be applied to the variables, and connections that should be used in the connection references on deployment!
Let’s take a look at an example of what this JSON looks like…
{
"EnvironmentVariables": [
{
"SchemaName": "tst_Deployment_env",
"Value": ""
},
{
"SchemaName": "tst_EnvironmentType",
"Value": ""
}
],
"ConnectionReferences": [
{
"LogicalName": "tst_sharedtst5fcreateuserandjob5ffeb85c4c63870282_b4cc7",
"ConnectionId": "",
"ConnectorId": "/providers/Microsoft.PowerApps/apis/shared_tst-5fcreateuserandjob-5ff805fab2693f57dc"
},
{
"LogicalName": "tst_SharepointSiteURL",
"ConnectionId": "",
"ConnectorId": "/providers/Microsoft.PowerApps/apis/shared_sharepointonline"
},
{
"LogicalName": "tst_AzureDevopsConnRef",
"ConnectionId": "",
"ConnectorId": "/providers/Microsoft.PowerApps/apis/shared_visualstudioteamservices"
},
{
"LogicalName": "tst_GHConn",
"ConnectionId": "",
"ConnectorId": "/providers/Microsoft.PowerApps/apis/shared_github"
}
]
}
This is an example from Microsoft at Microsoft Learn. You can see the file contains an object, with two nested arrays, each with an object for every connection reference or environment variable in the solution. There is an array for environment variables and an array for connection references. We need to generate this file for our solution, which we can do using the Power Platform CLI.
Generating the deployment settings file using the Power Platform CLI
We won’t need to connect to an organisation to do this, but we will need to have the solution exported and downloaded as a .zip file on our local directory somewhere.
The command that we’ll need to run in the CLI is as follows…
pac solution create-settings --solution-zip <solution_zip_file_path> --settings-file <settings_file_name>
You’ll need to replace ‘<solution_zip_file_path>’ with the file path of your solution’s .zip file on your local directory. Then replace ‘<settings_file_name>’ with the name you’d like to give your deployment settings file. Make sure to add the .json extension on the end.
So, let’s run this command for my Call a Clinician solution which I’ve been using in the past two posts to build my pipelines.
The CLI has created my deployment settings file for me which looks like this…
{
"EnvironmentVariables": [
{
"SchemaName": "lrdb_AuthorisationURL",
"Value": ""
},
{
"SchemaName": "lrdb_PresenceConnectorClientID",
"Value": ""
},
{
"SchemaName": "lrdb_PresenceConnectorHost",
"Value": ""
},
{
"SchemaName": "lrdb_PresenceConnectorResourceURL",
"Value": ""
},
{
"SchemaName": "lrdb_PresenceConnectorSecret",
"Value": ""
},
{
"SchemaName": "lrdb_TenantID",
"Value": ""
}
],
"ConnectionReferences": [
{
"LogicalName": "lrdb_CallaClinicianMicrosoftTeams",
"ConnectionId": "",
"ConnectorId": "/providers/Microsoft.PowerApps/apis/shared_teams"
}
]
}
As you can see at the moment, all of the environment variable values, and the connection for the connection reference are empty. I need to populate the values in this file with the values that should be applied in my target environment and then I will use this file in the release pipeline stage for that environment which I’ve populated the values for.
Getting the information to populate the file with
So now we need to populate the environment variable values and connectionid’s in our deployment settings file.
Environment variables
Environment variable values should be fairly simple and these will just be the values that we’d populate our environment variables with during deployment. Make sure you populate any environment variable values which have to be populated for deployment to be successful such as secret variables, where in this case I’m using one that’s being referenced in a custom connector.
Connection references
For connection references, getting the connection id’s is going to be a little bit more tricky but not difficult, and that’s what we’ll take a look at now…
First, you’ll need to login to make.powerapps.com using the account you’d like to have own the connections, or that has access to the connections you’d like to have used in your target environment.
Now head to the connections page in the environment and select the connection needed for the reference needed to be populated.
Then you’ll be able to get the ID from the URL.
In the connection screenshot above, we need to take the ID as the guid after the forward slash which follows ‘shared_teams’. Here I’m just populating a single connection reference for a Microsoft Teams reference which I’ll use this connection in my target environment for.
Simply populate the connection id property within your JSON file for the relevant connection reference, with this id.
Here’s my example…
Now for the environment variables, simply populate all the values as they should be populated in your target environment.
Upload the file
Now let’s upload the file to our Azure DevOps repository in the project where our pipeline is…
I’ll very simply upload this, and then I’ll need to head to my build pipeline and make a small change there so that my file becomes accessible in my release pipeline.
Now I’ll select the three dots next to file or directory path and I’ll navigate to the file that I need from my repository.
Now add an artifact name and set the publish location as Azure Pipelines.
Reference the deployment settings file
Now we’ll head to the release pipeline we’ve built for the solution so we can reference the artifact we’ve published during the build pipeline run. I’m going to edit this pipeline.
Select the three dots to the right of the input box for the deployment settings file, and navigate to the pipeline artifact you published for the deployment settings JSON file.
Now save the release pipeline.
Access checks
Now finally we need to do a couple of access checks. You’ll need to ensure the service principal you’re using to deploy the solution i.e. the one you created for the environments service connection in DevOps, has access to the connections you’re specifying to be used. Head to the connections and share them with the service principals.
If you’re using anything like a secret environment variable, you’ll need to ensure you’ve added access for the service principal in the Azure Key Vault, to ensure it can read the secret, required for a successful deployment.
Test it out!
Finally, run a new build just so that the deployment settings file gets published as an artifact, then create a new release referencing the published artifacts and run the release to the environment you’ve configured and watch it all happen!
Upcoming content
That’s all there is to it! So now we’ve looked at how to create build and release pipelines, and we’ve solved the issue of connection references and environment variables needing to be updated on a deployment by using a deployment settings file!
Subscribe to continue to get my upcoming content on the Power Platform Developer Tools covering all sorts like the Power Platform CLI, the ALM developer tools, the Dataverse developer tools and much more! 📩💖
Subscribe