Search code examples
azure-devopsazure-pipelinesazure-pipelines-release-pipelineazure-devops-self-hosted-agent

Azure Devops - Azure powershell task find release/deployment directory


I'm running Azure devops where i have a pipeline and a release (running on a self hosted agent), and the release is set to send up to an azure app service. The deployment works fine, the only issue is that i'd also like to be able to (based on some of my release variables) edit the web.config of the site AFTER it has already been deployed to the azure website.

I'm using the Azure Powershell task ( https://learn.microsoft.com/en-us/azure/devops/pipelines/tasks/deploy/azure-powershell?view=azure-devops ) ,and i can't find anywhere in the release variables ( https://learn.microsoft.com/en-us/azure/devops/pipelines/release/variables?view=azure-devops&tabs=batch ) that shows the directory of where the site exists. Looking through Kudu, it's pretty basic, like d:\home\site\wwwroot\ , but using that doesn't work at all.

Is this post config that i'm looking for not really possible, or should i be approaching it a different way?


Solution

  • I think you can use powershell task to call Kudu api to get the deployed web.config and edit it using Magic Chunks task or File Transform task in your release pipeline. Then using Kudu api to upload the changed web.config to azure website again.

    1, Below script shows how to get web.config from azure website.

    $srcResGroupName = "Test"
    $srcWebAppName = "tstest12"
    $outwebconfig="$(System.DefaultWorkingDirectory)\tempfolder\web.config"
    
    # Get publishing profile for SOURCE application
    $srcWebApp = Get-AzWebApp -Name $srcWebAppName -ResourceGroupName $srcResGroupName
    [xml]$publishingProfile = Get-AzWebAppPublishingProfile -WebApp $srcWebApp
    # Create Base64 authorization header
    $username = $publishingProfile.publishData.publishProfile[0].userName
    $password = $publishingProfile.publishData.publishProfile[0].userPWD
    
    $base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $username,$password)))
    
    $apiBaseUrl = "https://$($srcWebApp.Name).scm.azurewebsites.net/api/vfs/site/wwwroot/web.config"
    
    # Download the web.config file to $outwebconfig
    
    Invoke-RestMethod -Uri "$apiBaseUrl" `
                        -Headers @{UserAgent="powershell/1.0"; `
                         Authorization=("Basic {0}" -f $base64AuthInfo)} `
                        -Method GET `
                        -OutFile $outwebconfig
    

    Above script will download the web.config from the azure website and save it to $(System.DefaultWorkingDirectory)\tempfolder\web.config, where you can edit it with transform task later.

    Above scripts get the username and password with scripts, you can also get them in the publish profile by going to the Overview blade on your App Service, clicking ...More at the top of the blade, and then clicking Get publish profile

    2, Then you can add a config transform task to change your web.config according.

    3, Last add a powershell task to upload the changed web.config to azure website

    $srcResGroupName = "Test"
    $srcWebAppName = "tstest12"
    $webconfig="$(System.DefaultWorkingDirectory)\tempfolder\web.config"
    
    # Get publishing profile for SOURCE application
    $srcWebApp = Get-AzWebApp -Name $srcWebAppName -ResourceGroupName $srcResGroupName
    [xml]$publishingProfile = Get-AzWebAppPublishingProfile -WebApp $srcWebApp
    # Create Base64 authorization header
    $username = $publishingProfile.publishData.publishProfile[0].userName
    $password = $publishingProfile.publishData.publishProfile[0].userPWD
    
    $base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $username,$password)))
    
     $apiUrl = "https://$($srcWebApp.Name).scm.azurewebsites.net/api/vfs/site/wwwroot/web.config";
    
    Invoke-RestMethod -Uri $apiUrl -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -UserAgent $userAgent -Method PUT -InFile $webconfig -ContentType "application/xml";
    

    For more usage of Kudu api you can check here.