Auto-Populate Azure Pipeline Variables and Connections

Introduction

Efficient deployment processes are crucial for any software development lifecycle. Automating deployment tasks not only saves time but also minimizes the chances of human error. One powerful tool in this regard is the deployment settings file, which allows developers to automate the population of environment variables and connection references during the deployment pipeline import solution task. In this article, we will explore the significance of a deployment settings file and provide a step-by-step guide on how to generate and utilize it.

Use Cases

  • Setting Up Environments: With a deployment settings file, developers can easily handle settings that are specific to different environments without having to do it manually. This is really useful when you're deploying solutions in places like development, testing, and production.
  • Maintain Consistency: Making sure that every deployment is done in the same way is crucial for a dependable application. The deployment settings file helps in keeping a standard way of setting things up, making it less likely to have problems during deployment.

Step by Step process to implement this automation

Step 1. Generate the Deployment Settings file

Pre-requisites

Open Windows Command Prompt and run the below command to generate the deployment settings file.

C:\> pac solution create-settings --solution-zip <file path of solution zip file exported from source environment> --settings-file <deployment settings file name with .json extension>

After successful execution, the deployment settings file will be generated in the current location.

Sample run output

Sample run output

Below are the file contents generated from the exported solution, which contains two connection references and an environment variable.

{
  "EnvironmentVariables": [
    {
      "SchemaName": "sak_EnvVar1",
      "Value": "",
      "Name": {
        "Default": "Env Var1",
        "ByLcid": {
          "1033": "Env Var1"
        }
      }
    }
  ],
  "ConnectionReferences": [
    {
      "LogicalName": "sak_MicrosoftDataverse",
      "ConnectionId": "",
      "ConnectorId": "/providers/Microsoft.PowerApps/apis/shared_commondataserviceforapps"
    },
    {
      "LogicalName": "sak_Sharepoint",
      "ConnectionId": "",
      "ConnectorId": "/providers/Microsoft.PowerApps/apis/shared_sharepointonline"
    }
  ]
}

Step 2. Upload the environment-specific deployment settings file to Azure Repos.

First, we need to update the complete deployment settings file by providing value in environment variables and ConnectionId in Connection References.

In our solution, the environment variable value = Environment Name(DEV, UAT), and it can be retrieved from the current value of the environment variable as below.

DEV

ConnectionId can be retrieved by navigating to Connections in the maker portal, as shown below.

Connections

Now open the connection record and copy ConnectionID from the URL as shown below:

ConnectionID

After retrieving the environment variable values and ConnectionIds, our deployment settings will look as follows:

{
  "EnvironmentVariables": [
    {
      "SchemaName": "sak_EnvVar1",
      "Value": "DEV",
      "Name": {
        "Default": "Env Var1",
        "ByLcid": {
          "1033": "Env Var1"
        }
      }
    }
  ],
  "ConnectionReferences": [
    {
      "LogicalName": "sak_MicrosoftDataverse",
      "ConnectionId": "ee6093d8a3a34056b43f8c5467c9e7b6",
      "ConnectorId": "/providers/Microsoft.PowerApps/apis/shared_commondataserviceforapps"
    },
    {
      "LogicalName": "sak_Sharepoint",
      "ConnectionId": "b561152a8c744c6685a4be4ef16f354d",
      "ConnectorId": "/providers/Microsoft.PowerApps/apis/shared_sharepointonline"
    }
  ]
}

Similarly, we need to clone this file and create a deployment settings file for the target environment. But environment variable values and ConnectionIds have to be retrieved from target environment.

Note. ConnectionIds have to be created for first deployment. Automated pre-populate of ConnectionIds is done in future deployments.

The folder structure will look as follows in Azure Repos.

Azure Repos

Step 3. Run the YAML script below to import the solution with the deployment settings file in the target environment.

# Define pipeline triggers (none specified in this case)
trigger:
- none

# Define the agent pool where the jobs will run
pool:
  vmImage: windows-latest

# Define jobs to be executed in the pipeline
jobs:
- job: Deployment_Settings
  displayName: Deployment_Settings
  # Specify the agent pool for the specific job
  pool:
    vmImage: windows-latest
  # Define steps to be executed within the job
  steps:
  - task: PowerPlatformToolInstaller@2
    inputs:
      # Install the Power Platform tools using the default version
      DefaultVersion: true
    displayName: 'Power Platform Tool Installer '

  - task: PowerPlatformImportSolution@2
    inputs:
      # Configure the Power Platform solution import task with deployment settings and solution file from repos
      authenticationType: 'PowerPlatformSPN'
      PowerPlatformSPN: 'UAT'
      SolutionInputFile: 'PowerAppsSolution_1_0_0_1_managed.zip'
      UseDeploymentSettingsFile: true
      DeploymentSettingsFile: 'DeploymentSettings/UatDeploymentSettings.json'
      AsyncOperation: true
      MaxAsyncWaitTime: '60'
      PublishCustomizationChanges: true

Sample Output for successful pipeline run.

Sample output for successful pipeline

Below are screenshots from the target environment where connections and environment variable values are successfully updated based on the UatDeploymentSettings.json file.

For Connection Reference

Microsoft Dataverse

For Environment Variable

Environment Variable

Conclusion

The use of a deployment settings file significantly streamlines the deployment process, offering a flexible and efficient way to manage environment-specific configurations. By following the step-by-step guide outlined in this article, developers can enhance the reliability, security, and consistency of their deployment pipelines. Adopting such practices contributes to a more robust and automated deployment process, ultimately leading to a more reliable and scalable application infrastructure. Please feel free to reach out with any concerns.


Similar Articles