Azure DevOps:如何在PowerShell添加前删除YAML中的Power Apps环境变量?
问题
在Azure DevOps YAML管道中更新Power Platform环境变量前,需要先移除现有变量的旧值。当前使用Power Platform CLI(pac)登录后尝试更新,但pac无法识别要移除的变量,导致操作失败。如何在管道中实现更新变量前清空旧值?
当前使用的YAML管道代码如下:
trigger: branches: include: - main variables: SolutionName: 'test_azure_devops' ArtifactName: '_Gdansk' UnmanagedSolutionFolder: '$(System.DefaultWorkingDirectory)/$(ArtifactName)/$(SolutionName)/Unmanaged' ManagedSolutionOutputFile: '$(Build.StagingDirectory)/$(SolutionName)_managed.zip' stages: - stage: DeploySolution displayName: 'Deploy Power Platform Solution' jobs: - job: ExportUnpackReplaceImport displayName: 'Export, Unpack, Update ENV VARs, Import' pool: vmImage: 'windows-latest' steps: - task: PowerPlatformToolInstaller@2 displayName: 'Install Power Platform CLI' - task: PowerPlatformWhoAmi@2 displayName: 'Login to DEV environment' inputs: authenticationType: 'PowerPlatformSPN' PowerPlatformSPN: 'DEV Env connection' - task: PowerPlatformPublishCustomizations@2 inputs: authenticationType: 'PowerPlatformSPN' PowerPlatformSPN: 'DEV Env connection' AsyncOperation: true MaxAsyncWaitTime: '60' - task: PowerPlatformExportSolution@2 displayName: 'Export Solution from DEV' inputs: authenticationType: 'PowerPlatformSPN' PowerPlatformSPN: 'DEV Env connection' Environment: '' SolutionName: '$(SolutionName)' SolutionOutputFile: '$(Build.ArtifactStagingDirectory)/$(SolutionName).zip' Managed: true - task: PowerShell@2 displayName: 'Prepare unpack folder' inputs: targetType: 'inline' script: | $folder = "$(UnmanagedSolutionFolder)" if (Test-Path $folder) { Remove-Item -Path $folder -Recurse -Force } New-Item -ItemType Directory -Path $folder - task: PowerPlatformUnpackSolution@2 displayName: 'Unpack Solution' inputs: SolutionInputFile: '$(Build.ArtifactStagingDirectory)\$(SolutionName).zip' SolutionTargetFolder: '$(UnmanagedSolutionFolder)' SolutionType: 'Managed' - task: PowerShell@2 displayName: 'Debug: Show solution folder structure' inputs: targetType: 'inline' script: | Write-Host "=== Listing contents of solution folder ===" Get-ChildItem -Path "$(UnmanagedSolutionFolder)" -Recurse - task: PowerShell@2 displayName: 'Create Deployment Settings JSON' inputs: targetType: 'inline' script: | $deploymentSettings = @{ "EnvironmentVariables" = @( @{ "SchemaName" = "new_env_SP" "Value" = "" }, @{ "SchemaName" = "new_env_SP_List" "Value" = "f2dfa3c9-424c-4786-92e4-52b8437277a3" } ) } $json = $deploymentSettings | ConvertTo-Json -Depth 10 $settingsFile = "$(Build.ArtifactStagingDirectory)/deploymentSettings.json" $json | Set-Content -Path $settingsFile -Encoding utf8 Write-Host "✅ Deployment settings JSON created: $settingsFile" Write-Host "Contents of the deploymentSettings.json:" Write-Host $json - task: PowerShell@2 displayName: 'Debug: Show solution folder structure again' inputs: targetType: 'inline' script: | Write-Host "=== Listing contents of solution folder ===" Get-ChildItem -Path "$(UnmanagedSolutionFolder)" -Recurse - task: PowerPlatformPackSolution@2 displayName: 'Re-pack Solution' inputs: SolutionSourceFolder: '$(UnmanagedSolutionFolder)' SolutionOutputFile: '$(ManagedSolutionOutputFile)' SolutionType: 'Managed' - task: PowerShell@2 displayName: 'Check Deployment Settings File' inputs: targetType: 'inline' script: | $settingsFile = "$(Build.ArtifactStagingDirectory)/deploymentSettings.json" if (Test-Path $settingsFile) { Write-Host "✅ Deployment settings file exists: $settingsFile" } else { Write-Host "❌ Deployment settings file not found: $settingsFile" - task: PowerPlatformImportSolution@2 displayName: 'Import Solution to PROD' inputs: authenticationType: 'PowerPlatformSPN' PowerPlatformSPN: 'PROD Env connection' Environment: '' SolutionInputFile: '$(ManagedSolutionOutputFile)' AsyncOperation: true MaxAsyncWaitTime: '60' ConvertToManaged: true PublishCustomizationChanges: true OverwriteUnmanagedCustomizations: true UseDeploymentSettingsFile: true DeploymentSettingsFile: '$(Build.ArtifactStagingDirectory)/deploymentSettings.json'
解决方案
方法1:通过Power Platform CLI直接清空目标环境变量值
在导入解决方案前,添加PowerShell任务,用pac命令直接操作PROD环境的环境变量记录,清空或更新旧值。
示例任务代码(添加在PowerPlatformImportSolution之前):
- task: PowerShell@2 displayName: 'Clear/Update environment variables in PROD' inputs: targetType: 'inline' script: | # 使用SPN登录PROD环境 pac auth create --applicationId $(SPN-ClientId) --clientSecret $(SPN-Secret) --tenant $(TenantId) --url $(PROD-Env-Url) # 处理第一个变量:清空值 $valueRecord = pac data list --entity environmentvariablevalue --filter "environmentvariabledefinitionid/uniquename eq 'new_env_SP'" --fields environmentvariablevalueid,value if ($valueRecord) { $valueId = ($valueRecord | ConvertFrom-Json).value[0].environmentvariablevalueid pac data update --entity environmentvariablevalue --id $valueId --set "value=''" Write-Host "✅ Cleared value for new_env_SP" } # 处理第二个变量:更新为目标值 $valueRecord2 = pac data list --entity environmentvariablevalue --filter "environmentvariabledefinitionid/uniquename eq 'new_env_SP_List'" --fields environmentvariablevalueid,value if ($valueRecord2) { $valueId2 = ($valueRecord2 | ConvertFrom-Json).value[0].environmentvariablevalueid pac data update --entity environmentvariablevalue --id $valueId2 --set "value='f2dfa3c9-424c-4786-92e4-52b8437277a3'" Write-Host "✅ Updated value for new_env_SP_List" }
需提前将SPN的客户端ID、密钥、租户ID、PROD环境URL配置为管道变量。
方法2:修改解包后的环境变量XML文件
直接修改解包后的解决方案中环境变量的XML配置,清空旧值后重新打包,导入时会自动覆盖目标环境的变量。
示例任务代码(添加在PowerPlatformUnpackSolution之后):
- task: PowerShell@2 displayName: 'Modify environment variable values in unpacked solution' inputs: targetType: 'inline' script: | $envVarValuePath = "$(UnmanagedSolutionFolder)/Customizations/environmentvariablevalues" if (Test-Path $envVarValuePath) { Get-ChildItem -Path $envVarValuePath -Filter "*.xml" | ForEach-Object { $xml = [xml](Get-Content $_.FullName) $schemaName = $xml.environmentvariablevalue.environmentvariabledefinitionid.uniquename switch ($schemaName) { "new_env_SP" { $xml.environmentvariablevalue.value = "" $xml.Save($_.FullName) Write-Host "✅ Cleared value for $schemaName" } "new_env_SP_List" { $xml.environmentvariablevalue.value = "f2dfa3c9-424c-4786-92e4-52b8437277a3" $xml.Save($_.FullName) Write-Host "✅ Updated value for $schemaName" } } } } else { Write-Host "⚠️ Environment variable value folder not found: $envVarValuePath" }
方法3:优化部署设置文件
当前部署设置中仅设置空字符串可能无法被正确识别,可尝试添加ClearValue参数(需确认Power Platform工具任务版本支持):
修改部署设置的PowerShell脚本:
$deploymentSettings = @{ "EnvironmentVariables" = @( @{ "SchemaName" = "new_env_SP" "ClearValue" = true # 启用清空值的操作 }, @{ "SchemaName" = "new_env_SP_List" "Value" = "f2dfa3c9-424c-4786-92e4-52b8437277a3" } ) }
内容的提问来源于stack exchange,提问作者Przemek Dabek
相关产品推荐
相关产品推荐

