Closed LukasSchauffele closed 1 year ago
@LukasSchauffele would you mind reposting this on https://github.com/microsoft/powerplatform-build-tools? In the meantime it should be easy enough to delete environment variable values in the downstream environment using the deploy-solution-import-pre-hook.yml hook. Some pseudo code below. NOTE: This hasn't been tested or validated writing from memory.
$config = Get-Content '$(DeploymentSettingsPath)' | ConvertFrom-Json
. "$env:POWERSHELLPATH/load-save-pipeline-parameters.ps1"
$parameters = Read-Pipeline-Parameters "$(Agent.BuildDirectory)\deploy-pipeline-parameters.json"
. "$env:POWERSHELLPATH/dataverse-webapi-functions.ps1"
$dataverseHost = Get-HostFromUrl "$parameters.serviceConnectionUrl"
foreach ($var in $config.EnvironmentVariables) {
$odataQuery = 'environmentvariablevalues?$filter=schemaname eq ''' + $var.LogicalName + '''&$select=environmentvariablevalueid'
$response = Invoke-DataverseHttpGet $(SpnToken) $dataverseHost $odataQuery
foreach ($varValue in $response ) {
#Call delete against the API for each $varValue.environmentvariablevalueid
#https://learn.microsoft.com/en-us/power-apps/developer/data-platform/webapi/update-delete-entities-using-web-api#basic-delete
}
}
@LukasSchauffele Looks like issue has been fixed at platform level and I could not reproduce this issue. Can you retest and let us know the outcome?
FYI @mikefactorial
@LukasSchauffele This has been fixed in the latest release. Please install the latest version of the toolkit following the instructions for installing updates. Note that if you do not remove the unmanaged layers as described there you will not receive updates from us.
I know it's been a while since I first opened this issue but I wanted to share my working solution in case someone finds it useful.
First, we need to update the extension point as described in the documentation. This runs before deployment so update the Pipelines/Templates/Hooks/deploy-solution-pre-hook.yml
file and add a path to the new file:
# The step(s) below can be replaced with your custom steps. You must have at least one step in your hook. The step(s) below demonstrate examples of custom hooks.
# Be sure when upgrading to the latest pipelines that you don't overwrite your custom hooks with these samples hooks
# Parameters passed to the parent pipeline (deploy-Solution.yml) can be accessed via the following command.
# . "$env:POWERSHELLPATH/load-save-pipeline-parameters.ps1"
# $parameters = Read-Pipeline-Parameters "$(Agent.BuildDirectory)\deploy-pipeline-parameters.json"
steps:
- script: echo Deploy Solution Pre Hook
displayName: 'Deploy Solution Pre Hook'
enabled: false
- template: CustomTemplates/delete-environment-variables.yml
Second, create the Pipelines/Templates/Hooks/CustomTemplates/delete-environment-variables.yml
file. We first query the environment variable definition before we delete the environment variable values:
# Delete environment variables
steps:
- pwsh: |
$config = Get-Content '$(DeploymentSettingsPath)' | ConvertFrom-Json
. "$env:POWERSHELLPATH/load-save-pipeline-parameters.ps1"
$parameters = Read-Pipeline-Parameters "$(Agent.BuildDirectory)\deploy-pipeline-parameters.json"
. "$env:POWERSHELLPATH/dataverse-webapi-functions.ps1"
$dataverseHost = Get-HostFromUrl "$parameters.serviceConnectionUrl"
foreach ($envVar in $config.EnvironmentVariables) {
# Get Environment Variable Definition
$odataQuery = 'environmentvariabledefinitions?$filter=schemaname eq ''' + $envVar.SchemaName + '''&$select=environmentvariabledefinitionid'
$response = Invoke-DataverseHttpGet $(SpnToken) $dataverseHost $odataQuery
foreach ($envVarDefinition in $response.value ) {
# Get Environment Variable Values
$odataQuery = 'environmentvariablevalues?$filter=_environmentvariabledefinitionid_value eq ''' + $envVarDefinition.environmentvariabledefinitionid + '''&$select=environmentvariablevalueid'
$response = Invoke-DataverseHttpGet $(SpnToken) $dataverseHost $odataQuery
foreach ($envVarValue in $response.value) {
$odataQuery = 'environmentvariablevalues(' + $envVarValue.environmentvariablevalueid + ')'
Invoke-DataverseHttpDelete $(SpnToken) $dataverseHost $odataQuery
}
}
}
displayName: 'Delete Environment Variables'
Finally, the function Invoke-DataverseHttpDelete
doesn't exist yet and we need to create it. Add the below snippet at the end of PowerShell/dataverse-webapi-functions.ps1
@mikefactorial - I would be happy to create a PR for this if you think it could be useful.
<#
This function invokes Dataverse web api DELETE.
#>
function Invoke-DataverseHttpDelete {
param (
[Parameter(Mandatory)] [String]$token,
[Parameter(Mandatory)] [String]$dataverseHost,
[Parameter(Mandatory)] [String]$requestUrlRemainder
)
$headers = Set-DefaultHeaders $token
$requestUrl = Set-RequestUrl $dataverseHost $requestUrlRemainder
$response = Invoke-RestMethod $requestUrl -Method 'DELETE' -Headers $headers
return $response
}
Is your feature request related to a problem? Please describe.
I will add my question/request for the ALM accelerator to start with even though the underlying issue is a bug with the platform.
Is it possible to remove the environment variable values in a downstream environment before importing a solution into an environment? Maybe using hooks?
The reason I want to do this is because I am sometimes getting an error on import when the environment variable in the deploymentSettings.json and the environment variable value in the downstream environment do not match.
I've actually already posted about this error before - #3101 - and raised a ticket with support (2207120030000937 and 2211160030000704) but haven't gotten anywhere. The response from support was to delete the value of the environment variable manually before importing a solution.
This screenshot shows the current value of the environment variable in the downstream environment I am importing in, the value is
8e197461-21d1-429b-9c53-e7792b2ce555
:The value for this environment variable in my deploymentSettings-demo.json matches. The solution successfully imports.
Once I change the value in deploymentSettings-demo.json to something other than what's currently in the environment the import fails (I've added the full error message below).
If I remove the environment variable value manually and then re-run the import pipeline, everything works.
Describe the solution you'd like
Ideally, pac should be able to import into an environment with environment variables specified in the --settings-file without issues. Can you please pass this on to the pac team?
In the mean time, could someone please assist me in writing a hook to clear the environment variable values in the downstream environment? The current process is:
Describe alternatives you've considered
Continue to manually delete environment variables.
Additional context?
Error message: