Automate Dynamics 365 Solution Import with Azure DevOps YAML

Introduction

Importing solutions into a Dynamics 365 or Power Platform environment is a critical process for deploying updates, new features, and bug fixes. This blog post will guide you through the process using a YAML pipeline that automates the packing and importing of solutions into your environments. This pipeline supports multiple solutions, ensuring a streamlined and efficient deployment process.

Unique Features of the Pipeline

  1. Trigger Mechanism
    • Trigger: The pipeline triggers changes to the dev branch and paths under Solutions/.
    • Advantage: Automatically initiates the pipeline when relevant changes are detected, ensuring timely deployments.
  2. Multi-Solution Support
    • Parameters: Dynamics365Solutions allows the inclusion of multiple solutions.
    • Advantage: Handles multiple updates simultaneously, reducing deployment time and effort.
  3. Environment-Specific Import
    • Parameters: The environment parameter customizes the pipeline for different environments (e.g., QA, Prod).
    • Variables: PowerPlatformSPNQA, AzureDevopsEnvironmentQA, PowerPlatformSPNProd, and AzureDevopsEnvironmentProd define environment-specific service connections.
    • Advantage: Ensures solutions are correctly imported into the appropriate environment with the right settings.
  4. Upgrade Management
    • Parameters: isSolutionImportAsUpgrade specifies if the solution import is an upgrade.
    • Advantage: Facilitates smooth transitions and updates by handling upgrade scenarios automatically.
  5. Automated Packing and Validation
    • Templates: d365-solutions-pack.yml handles packing and validation of solutions.
    • Advantage: Ensures solutions are correctly packaged and validated before import, minimizing errors.
  6. Reusable Templates
    • Templates: Uses separate templates for packing (d365-solutions-pack.yml) and importing (d365-solutions-import.yml).
    • Advantage: Promotes reusability and consistency across different projects and environments.
  7. Artifact Management
    • Steps: DownloadBuildArtifacts manages and downloads build artifacts.
    • Advantage: Ensures the correct versions of solutions are deployed, reducing the risk of errors.
  8. Comprehensive Variables Management
    • Variables: SolutionsFolderName, PackedSolutionsFolderName, and DeploymentSettingsFilePath manage folders and paths.
    • Advantage: Enhances flexibility and control over the deployment process with specific configurations and paths.

Pipeline YAML

The provided YAML pipeline includes stages for packing and validating Dynamics 365 solutions, followed by importing them into the QA environment and more. This approach ensures that the solutions are correctly packaged and validated before deployment, reducing the risk of errors and issues in the production environment.

Here's the YAML pipeline that accomplishes the above tasks.

trigger:
  branches:
    include:
      - dev
  paths:
    include:
      - Solutions/

# Parameters
parameters:
#  Dynamics365SolutionParameter: Name of the Dynamics 365 solution considered for the export
- name: Dynamics365Solutions
  displayName: Dynamics 365 solutions
  type: object
  default:
  - solutionone
  - solutiontwo
  - solutionthree
# isSolutionImportAsUpgrade: Parameter that indicate if the solutions need to be imported as upgrade or not
- name: isSolutionImportAsUpgrade
  displayName: Is solution import as upgrade?
  type: boolean
  default: true

# Variables
variables:
   # Name of the folder form root level which contains all Dynamics 365 Solution
  - name: SolutionsFolderName
    value: 'Solutions'
  # Name of the folder form root level where will store the packed solution 
  - name: PackedSolutionsFolderName
    value: 'PackedSolutions'
   # Name of the Power Platform service connection for QA associated to the considered Dynamics 365 environment for the import of the solution
  - name: PowerPlatformSPNQA
    value: 'Project EO - Dynamics 365 - qa'
    # Name of the Power Platform service connection for Prod associated to the considered Dynamics 365 environment for the import of the solution
  - name: AzureDevopsEnvironmentQA
    value: 'Dynamics 365 - QA'
   # Name of the Power Platform service connection for QA associated to the considered Dynamics 365 environment for the import of the solution
  - name: PowerPlatformSPNProd
    value: 'Project EO - Dynamics 365 - prod'
    # Name of the Power Platform service connection for Prod associated to the considered Dynamics 365 environment for the import of the solution
  - name: AzureDevopsEnvironmentProd
    value: 'Dynamics 365 - Prod'
    # Path to Deployment Settings of all Solutions
  - name: DeploymentSettingsFilePath
    value: 'Configurations\DeploymentSettings'
   
# Definition of the Build.BuildNumber variable value
name: $(Date:yyyyMMdd)$(Rev:r) # Ex: 202301013

stages:
# Pack Dynamics 365 solutions
- stage: 'PackSolutions'
  displayName: 'Pack and validate Dynamics 365 Solutions'
  condition: succeeded()
  jobs:
    - job: 'PackSolutions'
      displayName: 'PackSolutions Job'
      pool:
        vmImage: 'windows-latest' 
      steps:
        # Deploy the package with the Dynamics 365 solutions
        - template: ./Templates/d365-solutions-pack.yml
          parameters:
              Dynamics365Solutions: ${{ parameters.Dynamics365Solutions }}

- stage: 'ImportToQA'
  displayName: 'Import Dynamics 365 Solutions to QA'
  dependsOn: PackSolutions
  condition: succeeded()
  jobs:
    - deployment: 'ImportSolutionsToQA'
      displayName: 'Import Solutions to QA'
      pool:
        vmImage: 'windows-latest'
      environment: '$(AzureDevopsEnvironmentQA)'
      strategy:
        runOnce:
          deploy:
            steps:
            # Deploy the Dynamics 365 Solutions
              - template: ./Templates/d365-solutions-import.yml
                parameters:
                  Dynamics365Solutions: ${{ parameters.Dynamics365Solutions }}
                  PowerPlatformSPN: '$(PowerPlatformSPNQA)'
                  isSolutionImportAsUpgrade: ${{ parameters.isSolutionImportAsUpgrade }}
                  ArtifactName: '$(PackedSolutionsFolderName)'
                  Environment: 'qa'

Pack Solutions Template

# Pipeline template Pack the Solution with the Power Platform solutions

# Parameters
parameters:
- name: Dynamics365Solutions
  displayName: Dynamics 365 solutions
  type: object

steps:
    # Build and validate the package with the Dynamics 365 solutions
    # Install the Power Platform Build Tools
  - task: PowerPlatformToolInstaller@2
    displayName: 'Install Power Platform Build Tools'
    inputs:
      DefaultVersion: true
  # Update the version of the solution
  - ${{ each solution in parameters.Dynamics365Solutions }}:
    # Pack DesjardinsCore solution
    - task: PowerPlatformPackSolution@2
      displayName: 'Power Platform Pack Solution'
      inputs:
        SolutionSourceFolder: '$(Build.SourcesDirectory)\$(SolutionsFolderName)\${{ solution }}'
        SolutionOutputFile: '$(Build.ArtifactStagingDirectory)\${{ solution }}.zip'
        SolutionType: 'Both'
  # Publish Packed Build to Pipeine Artifact
  - task: PublishBuildArtifacts@1
    displayName: 'Publish Artifact Packed Solutions'
    inputs:
      PathtoPublish: '$(Build.ArtifactStagingDirectory)'
      ArtifactName: '$(PackedSolutionsFolderName)'
      publishLocation: 'Container'

Import Solutions Template

# Pipeline to import Dynamics 365 Solutions

# Parameters
parameters:
- name: Dynamics365Solutions
  type: object
- name: PowerPlatformSPN
  type: string
- name: isSolutionImportAsUpgrade # Not boolean because managed in a variable in the pipelines that will call this template
  type: string
  default: 'true'
- name: ArtifactName
  type: string
- name: Environment
  type: string

steps:
  # Checkout repository
  - checkout: self
  # Install the Power Platform Build Tools
  # Download Artifacts Published in Previous Stage
  - task: DownloadBuildArtifacts@1
    inputs:
      buildType: 'current'
      downloadType: 'single'
      artifactName: '${{ parameters.ArtifactName }}'
      downloadPath: '$(System.ArtifactsDirectory)' 
  - task: PowerPlatformToolInstaller@2
    displayName: 'Install Power Platform Build Tools'
    inputs:
      DefaultVersion: true
  # Import the Dynamics 365 Solutions
  - ${{ each solution in parameters.Dynamics365Solutions }}:
    # Import the considered Dynamics 365 solutions
    - task: PowerPlatformImportSolution@2
      displayName: 'Import ${{ solution }} solution'
      condition: succeeded()
      inputs:
        authenticationType: 'PowerPlatformSPN'
        PowerPlatformSPN: '${{ parameters.powerPlatformSPN }}'
        SolutionInputFile: '$(Build.ArtifactStagingDirectory)\${{ parameters.ArtifactName }}\${{ solution }}_managed.zip'
        AsyncOperation: true
        MaxAsyncWaitTime: '60'
        HoldingSolution: ${{ parameters.isSolutionImportAsUpgrade }}
        ConvertToManaged: true
        UseDeploymentSettingsFile: true
        DeploymentSettingsFile: '$(DeploymentSettingsFilePath)\${{ solution }}\DeploymentSettings-${{ parameters.Environment }}.json'
    - task: PowerPlatformApplySolutionUpgrade@2
      displayName: 'Upgrade ${{ solution }} solution'
      condition: and(succeeded(), eq('${{ parameters.isSolutionImportAsUpgrade }}', 'true'))
      inputs:
        authenticationType: 'PowerPlatformSPN'
        PowerPlatformSPN: '${{ parameters.powerPlatformSPN }}'
        SolutionName: ${{ solution }}
        AsyncOperation: true
        MaxAsyncWaitTime: '60'

This will Output,

Output

Azure

Advantages of Using This YAML Pipeline

  1. Efficiency: Automates the entire process of packing and importing solutions, saving time and reducing manual effort.
  2. Consistency: Ensures a consistent approach to deploying solutions across different environments.
  3. Scalability: Can handle multiple solutions and environments, making it suitable for large-scale deployments.
  4. Flexibility: Supports environment-specific settings and upgrade scenarios, catering to diverse deployment needs.
  5. Error Reduction: Automated validation and artifact management reduce the risk of deployment errors.
  6. Reusability: Templates can be reused across different projects, enhancing development efficiency and reducing redundancy.

Conclusion

Automating the import of Dynamics 365 solutions using a YAML pipeline enhances the deployment process's efficiency, consistency, and scalability. This approach not only saves time but also reduces the risk of errors, ensuring smooth and successful deployments. By leveraging the features and advantages of this pipeline, organizations can streamline their Dynamics 365 solution deployments and focus on delivering value to their users.


Similar Articles