October 24th, 2024

Automate Power Platform Environment Creation & Solution Imports with GitHub Actions

Anuj Goyal
Principal Software Engineer

Automate Power Platform Environment Creation & Solution Imports with GitHub Actions

Automating your CI/CD pipeline for Power Platform solutions can streamline your development process and reduce manual tasks. By integrating GitHub Actions, you can automatically create environments, import solutions, and even clean up environments when issues arise. This blog post will walk you through a step-by-step guide on how to achieve these goals effectively.


Introduction

In this blog, we will explore how to automate the creation of Power Platform environments and the import of solutions using GitHub Actions. This approach not only eliminates repetitive manual tasks but also helps maintain cleaner environments and streamline deployments. By following this guide, you will set up a robust pipeline that dynamically creates new environments and imports your Power Platform solution each time you push a new change to your repository.

Prerequisites

Before diving into the setup, ensure you have the following prerequisites in place:

  • A Power Platform environment: You need admin access to manage environments.
  • Azure Active Directory (Azure AD): An Azure AD app with the necessary permissions to authenticate against Power Platform.
  • GitHub repository: This is where your solutions and workflows will reside.
  • Power Platform CLI: Ensure that the Power Platform Command-Line Interface (CLI) is installed and available in your workflow environment.

GitHub Actions Setup

GitHub Actions allow you to automate software workflows directly within your GitHub repository. To set this up, we will create a workflow file that automatically creates a Power Platform environment and imports a solution whenever new code is pushed.

To start, create a .github/workflows folder in your repository. Inside that folder, create a file called power-platform-cicd.yml.

Key Components

  • actions/checkout@v3: This action checks out your repository so you can access your files easily.
  • Azure login: This action authenticates with Azure AD using secrets stored in GitHub.
  • Power Platform CLI: This CLI manages the creation of Power Platform environments and solution imports.

GitHub Secrets

To authenticate your GitHub Actions workflow with Power Platform, you will need to set up several GitHub secrets.

  1. AZURE_CLIENT_ID: The client ID of your Azure AD application.
  2. AZURE_TENANT_ID: The tenant ID associated with your Azure account.
  3. AZURE_CLIENT_SECRET: The secret key for your Azure AD application.

You can add these in your GitHub repository settings under Settings > Secrets and variables > Actions > Secrets.

Step-by-Step GitHub Actions Workflow

Here’s a breakdown of the workflow file that will automate Power Platform tasks:

name: Power Platform CI/CD

on:
  push:
    branches:
      - main

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Code
        uses: actions/checkout@v3

      - name: Set Environment Name
        run: echo "ENV_NAME=${{ github.ref_name }}-${{ github.run_id }}" >> $GITHUB_ENV

      - name: Authenticate with Azure
        uses: azure/login@v1
        with:
          client-id: ${{ secrets.AZURE_CLIENT_ID }}
          tenant-id: ${{ secrets.AZURE_TENANT_ID }}
          client-secret: ${{ secrets.AZURE_CLIENT_SECRET }}

      - name: Create Power Platform Environment
        run: pac environment create --name $ENV_NAME --region unitedstates --type Production

      - name: Import Solution to Power Platform
        run: pac solution import --path ./solution.zip --environment $ENV_NAME

      - name: Cleanup Power Platform Environment on Failure
        if: failure()
        run: pac environment delete --name $ENV_NAME

Workflow Breakdown

  • Checkout Code: The workflow begins by checking out the latest code from your repository using actions/checkout@v3. This ensures that the most recent solution files are available.
  • Set Environment Name: A unique environment name is generated based on the branch and GitHub Run ID. This prevents conflicts when running parallel workflows.
  • Authenticate with Power Platform: Next, using the [Power Platform CLI], the workflow authenticates with Azure AD, leveraging credentials stored in GitHub Secrets.
  • Create Power Platform Environment: Then, the workflow creates a new environment using the auto-generated name.
  • Import Solution to Power Platform: After that, the solution from your repository is imported into the newly created environment.
  • Cleanup on Failure: If the import fails, the workflow deletes the environment to avoid clutter.

Optional Environment Cleanup

For extra control, this cleanup step runs only if the solution import fails. This approach helps keep your Power Platform tenant clean by removing temporary environments after a failure.

Verifying the Workflow

To ensure the workflow functions correctly, follow these steps:

  1. Commit and Push: Push the changes to your GitHub repository.
  2. Observe Execution: In the GitHub “Actions” tab, you can view the execution of the workflow.
  3. Verify Environment: After completion, visit the [Power Platform Admin Center] to check that a new environment with a unique name has been created. If there was a failure, confirm that the environment has been deleted.

Conclusion

By automating the creation of Power Platform environments and the import of solutions using GitHub Actions, you streamline the CI/CD process and reduce manual intervention. Furthermore, the addition of automatic environment cleanup ensures your tenant remains organized and free of clutter.

This setup is scalable and adaptable to various workflows, providing flexibility and efficiency in deploying Power Platform solutions.


Additional Links:

Author

Anuj Goyal
Principal Software Engineer

0 comments