Change the web.config values directly in the app service using yaml

180 Views Asked by At

I tried so much to search on this but I was still unclear and there is no post with the requirement as I have. I'm doing the swap slot using AzureAppServiceManage@0 but after this action, I want to change the values of a few parameters in the web.config, is there a good way of dealing with this? If I have to trigger a powershell task, it shows the path doesn't exist, how can I directly get to the app service and change the value?

- job: swapone
    steps:
      - task: AzureAppServiceManage@0
        inputs:
          azureSubscription: 'subscrip1'
          Action: 'Swap Slots'
          WebAppName: 'xxx-one-swapslottest-67'
          ResourceGroupName: 'xxx-one-rgtest-67'
          SourceSlot: 'staging'
          SwapWithProduction: true
          PreserveVnet: true
1

There are 1 best solutions below

5
On

how can I directly get to the app service and change the value?

You can use Kudi VFS api to PUT the file to app service kudu. Need to pass If-Match: "*" header in the script.

enter image description here

For example, I have a web.config on app service kudu:

enter image description here

I put the newweb.config in repo, and would like to update the dll file name(add test) in argument: enter image description here

In DevOps pipeline, add a azure powershell task with sample script below, note to get the newweb.config with -Raw to keep the xml format.

- task: AzurePowerShell@5
  inputs:
    azureSubscription: 'ARMConnTest1'
    ScriptType: 'InlineScript'
    Inline: |
      $ResGroupName = "{yourRG}"
      $WebAppName = "{yourappservice}"
      $webConfigFilePath = "newweb.config"
      
      # Get publishing profile for web application
      $WebApp = Get-AzWebApp -Name $WebAppName -ResourceGroupName $ResGroupName
      [xml]$publishingProfile = Get-AzWebAppPublishingProfile -WebApp $WebApp
      
      # Create 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)))
      #$authHeader = @{Authorization=("Basic {0}" -f $base64AuthInfo); If-Match="*"}
      $authHeader = @{
      'Authorization' = ('Basic {0}' -f $base64AuthInfo)
      'If-Match'      = '*'
       }
      #$authHeader = @{Authorization=("Basic {0}" -f $base64AuthInfo)}
      
      # Get the Kudu API URL
      $kuduApiUrl = "https://$($webAppName).scm.azurewebsites.net/api/vfs/site/wwwroot/web.config"
      
      # Read the new web.config content from the file using the Get-Content cmdlet with -Raw
      $webConfigContent = Get-Content -Path $webConfigFilePath -Raw
      
      # Invoke the PUT request to update the web.config file
      Invoke-RestMethod -Uri $kuduApiUrl -Headers $authHeader -Method PUT -Body $webConfigContent -ContentType "text/xml"
    azurePowerShellVersion: 'LatestVersion'

enter image description here

You can find similar answer in ticket here.