Building a CI/CD pipeline in Azure DevOps is a powerful way to automate and streamline your development and deployment process. In this blog, we’ll learn how to leverage the Azure Pipelines to streamline the Power Apps deployment process, saving time and effort.

Prerequisites

  1. Azure DevOps account with Repository access.
  2. Azure Portal access to create Azure AD App registration.
  3. Power App System Administrator access

Create an App registration in the Azure Portal

First, navigate to Azure portal > App registrations and create a new One.

Name it something like Power Apps CI/CD App and select the account type as Accounts in this organizational directory only.

app registration in azure portal

Note down the Tenant ID and Application ID (Client ID), we will need it later.

Now navigate to API permissions and make sure it has the following API permissions –

  1. Azure DevOps (user_impersonation)
  2. Dynamics CRM (user_impersonation)
  3. Microsoft Graph (User.Read)
  4. Power Apps Runtime Service (user_impersonation)
azure app api permissions

Now, navigate to Certificates & secrets and create a new Client Secret. Copy the value of the client secret as it will be shown only once.

azure app client id and secret

Create Application User in Power apps

Navigate to Power Apps admin center. Select your source environment (usually the one where you do development).

Go to Settings > Users + permissions > Application users.

power apps application user settings

Select Add new user from the top bar.

Click Add an app and select the app we have created in Azure in the previous step.

Select the business unit and assign the System Administrator security role to the user.

power apps application user create form

Similarly, create an application user for your target environment using the same app registration.

Create a service account in Azure DevOps

Navigate to Azure DevOps.

Go to your project settings and navigate to Service Connections.

azure dev ops service connection navigation

Click on Create New Connection.

Search for the connection type named Power platform.

If you did not find the connection in the list then you have to install it in your DevOps organization. Go to the marketplace and install this extension.

In the Authentication method select Application ID and Client Secret. In Server URl, enter the URL of your source Power apps environment, for example, https://acme-dev.crm5.dynamics.com.

In Tenant ID, enter the tenant ID from the App registration we have created previously.

Enter the Application Id and Secret from the app registration we have created previously.

Give it a descriptive name such as Sales 365 DEV.

Make sure to check the checkbox to Grant access permission to all pipelines.

service account all pipelines permission

Similarly, follow the same steps to create a service account for your target environment.

Create a Build Pipeline

First, create a repository if it does not exist already.

Navigate to Azure DevOps > Select your project > Pipelines

Make sure the option to create Classic Pipelines is enabled at your organization and project level. For more details read this tutorial.

Create a new pipeline. While creating a pipeline, select the option at the bottom to use the Classic editor.

classic pipeline option

Select your source, in this case, Azure Repos Git, select your project, Repository, and default branch, and select Continue.

select repository azure pipeline

On the next screen, select Start with Empty Job.

start with empty job azure pipeline

Give your pipeline a name.

pipeline name

Select your agent, and give it a name, like Power Apps agent.

pipeline agent name

Now add the first task to your pipeline. Click on the + button on your agent.
Search for a task named Power Platform Tool Installer and click Add.

adding power platform tool installer

Create a new variable named SolutionName for your pipeline. It will be used in further steps.

Go to Variables tab and click Add.

Give it a value of your solution name in power apps (NOT Display Name).

creating a variable in pipeline

Now add the second task to your agent.
Again, click on the + button and search for a task named Power Platform Export Solution.

add export solution step

Choose the Authentication type as Service Principal/Client secret.

Choose your service connection (source) created in the previous section.

In Environment Url, add $(BuildTools.EnvironmentUrl). This will be replaced with the current environment URL of the logged-in user from the previous step.

In the solution name, we will use the environment variable we have created in the previous step. To use variable give it a value like $(SolutionName).

In the solution output file, give it a value something like $(Build.StagingDirectory)\$(SolutionName)_Managed.zip.

Here we are using the staging directory as a temporary directory to store the exported solution.

Learn more about available environment variables in azure pipelines here.

Check the box Export as Managed Solution, if required.

export solution step

Add a new task named Power Platform Unpack Solution.

unpack solution step

In the Solution input file field, give it a path that we have used to store the solution in the previous step $(Build.StagingDirectory)\$(SolutionName)_Managed.zip.

In Target Folder to Unpack Solution field, give it a value of the directory you want to store the solution components in, for example, $(Build.SourcesDirectory)\$(SolutionName)\Managed

Here SourceDirectory is the directory where the repository content is stored.

Choose the type of solution.

Now, add a new task named Publish build artifacts to publish artifacts that can be used later in the release pipeline.

publish build artifacts action

In the Path to Publish field give it the directory where we unpacked the solution in the previous step, for example,
$(Build.SourcesDirectory)\$(SolutionName)\Managed

Give your artifact a name.

Add a new task named Command line.

command line task

We are using this task to commit and push the solution components in the Azure repository.

In the script section add the following script –

Bash
git config user.email dev@power365tips.com
git config user.name "CI/CD Automation"
git checkout -B main
git pull
git add --all
git commit -m "Updated solution components"
git -c http.extraHeader="AUTHORIZATION: Bearer $(System.AccessToken)" push origin main

Make sure that the Allow scripts to access the OAuth token option is checked in your agent configuration. It is required to commit the changes to the repository.

allow scripts to access the OAuth token in pipeline

Save your changes.

Create a release pipeline

Now we will create a release pipeline to import the solution to target environments like UAT, Pre-prod, or Production, etc.

First, navigate to Pipelines > Releases.

azure devops release pipelines nav

Give your pipeline a name.

release pipeline name

Click on Add artifact.

Select the source type as Build.
Select your project.
Select Source (build pipeline) as the pipeline we have created previously.
Keep the default version to the Latest
Name the source alias something like _Power Apps

add artifact

Now click on Add Stage and select an Empty job.

add empty job

Name the stage to something like Release to UAT.

Now click on tasks below the name of the stage. This will open an editor like before to add tasks to your pipeline.

add tasks

Add the first task to the agent named Power Platform tool installer as we have done for the build pipeline previously.

Create the same variable named SolutionName for this stage also.

Add the second task named Power Platform Pack Solution to pack the solution.

task pack solution

In the source folder field, use the three dots on the right side to select the parent folder which contains the solution components, and select Ok. It should be something like $(System.DefaultWorkingDirectory)/_Power Apps/Solution.

For the Solution Output file, set it as $(Build.StagingDirectory)\$(SolutionName).zip.
This is a temporary location where the packed solution will be created for import.

Choose the correct type of solution you want to create.

pack solution configuration

Next, add a task named Power Platform Import Solution.

power platform import solution

Choose the Authentication type as a Service principal.

Select the appropriate target service connection. In the environment URL, keep the default $(BuildTools.EnvironmentUrl).

In the solution input file, set $(Build.StagingDirectory)\$(SolutionName).zip
This is the path we set to pack the solution in previous step.

Now save the pipeline.

Similarly, you can also add more stages to it, like Pre-prod, Production, etc.

optionally add more stages in pipeline

Now, to create a release, go to all pipelines, select your release pipeline, and click Create release.

create a release