Skip to main content

Add `pac` to the global path

By Betim Beja ·

Recently I have been setting up Azure DevOps pipelines in different projects. One thing I like is to standardize the process using PowerShell scripts, that can be executed by the developer while they are working locally, or by the build pipeline when code is pushed to the repository.

We are using the Power Platform Tools on VS Code and on Azure DevOps.

By executing the following script in Azure DevOps, which uses the Prepend Path functionality, you can execute the pac command in your steps.

Azure DevOps Pipelines Windows Host

prepend-path-windows.yaml
- pwsh: |
    $pac = Join-Path $env:POWERPLATFORMTOOLS_PACCLIPATH pac tools
    echo "##vso[task.prependpath]$pac"
  displayName: 'Add PAC to Path'

Azure DevOps Pipelines Linux Host

prepend-path-linux.yaml
- pwsh: |
    $pac = Join-Path $env:POWERPLATFORMTOOLS_PACCLIPATH pac_linux tools
    echo "##vso[task.prependpath]$pac"
  displayName: 'Add PAC to Path'

A similar result can be achieved by executing the following script in Github Actions. The script uses the Adding a System Path functionality. You can then execute the pac command in your steps.

GitHub Actions Linux or Windows Host

system-path-github.yaml
- name: Add pac to PATH
  shell: pwsh
  run: |
    $pac = (Get-Item "$env:POWERPLATFORMTOOLS_PACPATH").Directory.FullName;
    "$pac" | Out-File -FilePath $env:GITHUB_PATH -Append

This sample PowerShell script can export and unpack a solution from dataverse and also execute canvas unpack for all CanvasApps. This way changes to Canvas Apps can be easily reviewed.

export-solution.ps1
param(
    [string] $Solution = "ALMDemo",
    [string] $Environment = "ALM Demo"
)

try {
    pac org select --environment $Environment

    $zipFile = "$($env:USERPROFILE)\Downloads\$($Solution).zip";
    $managedZipFile = "$($env:USERPROFILE)\Downloads\$($Solution)_managed.zip";
    Write-Warning $zipFile
    Write-Warning $managedZipFile

    pac solution export --path $managedZipFile `
        --name $Solution `
        --managed `
        --async `
        --overwrite

    pac solution export --path $zipFile `
        --name $Solution `
        --async `
        --overwrite

    pac solution unpack --zipFile $zipFile `
        --folder "$($PSScriptRoot)\..\solution\$($Solution)" `
        --packagetype "Both" `
        --allowDelete true `
        --allowWrite true

    If (Test-Path "$($PSScriptRoot)\..\solution\$($Solution)\CanvasApps\" ) { 
        $CanvasApps = Get-ChildItem "$($PSScriptRoot)\..\solution\$($Solution)\CanvasApps\*.msapp"
        foreach ($CanvasApp in $CanvasApps) {
            pac canvas unpack --msapp $CanvasApp.FullName `
                --sources "$($PSScriptRoot)\..\solution\CanvasSource\$($CanvasApp.BaseName)"
        }
    }
}
catch { 
    Write-Error $_.Exception
}

This can be called in the same way from the developers machine or from the build pipeline.

github-step.yaml
- name: Export Solution "ALM Demo" 
run: |
    ./.scripts/Export-Solution.ps1 -Solution "ALMDemo" -Environment "unqfef64e6ac803ee11a66d002248856"