0

I am trying to update my web config file using the azure powershell task in azure build pipeline. the following is the script i use.

$storageConectionString="DefaultEndpointsProtocol=https;AccountName="+$storageAccountName+";AccountKey="+$value+";EndpointSuffix=core.windows.net" 
$sqlConnectionString ="server=tcp:"+$sqlServerName+";database="+$databasename+";UID=AnyString;Authentication=Active Directory Interactive" 

#file path in azure repo
$configFilePath = "$visualStudioFolder/NRMAPP//NRMAPP/NRMAPP/Web.config"

$myXML = [Xml] (Get-Content $configFilePath)

$sqlConnectionObj = $myXML.configuration.connectionStrings.add
$sqlConnectionObj.connectionString = $sqlConnectionString

write-output $sqlConnectionObj



$storageConnectionObj = $myXML.configuration.appSettings.add | where {$_.Key -eq "StorageConnectionString" }
$storageConnectionObj.value = $storageConectionString

write-output $storageConnectionObj

$myXML.Save($configFilePath)

The build pipeline runs successfully but not making the changes to the config file in azure repo. Any help is appreciated thanks in advance.

aswin
  • 23
  • 9
  • Are you sure about the $configFilePath you're creating? Forward slashed and even a double `//`.. Are you getting any error messages ? If so, please add these to the question. – Theo Apr 05 '20 at 12:43
  • @Theo i am positive the double // is not causing the issue i am able to read the contents of the file using the same path variable – aswin Apr 05 '20 at 12:45
  • Yes, but does the `$myXML.Save($configFilePath)` work without problems with that path? What does $configFilePath contain exactly? Perhaps the file is written, but in some destination you don't expect.. – Theo Apr 05 '20 at 12:47
  • i tried saving it in different path as well but it simply doesn't create a new file – aswin Apr 05 '20 at 12:49
  • What if you hard-code the destination path to your local harddisk. If that works, you will know for sure the path may be acceptable for Powershell (Get-Content), but when it comes to saving with a .NET method, it interprets the path differently. (the current folder path for .NET is different to that of PowerShell and you need to enter an absolute path there) – Theo Apr 05 '20 at 12:52
  • Hi @aswin Did you check out below scripts, how did it go? – Levi Lu-MSFT Apr 09 '20 at 01:23

2 Answers2

1
  • The build pipeline runs successfully but not making the changes to the config file in azure repo.

The code changes made in the pipeline cannot be reflected in the azure repo. Because the pipelines always clone your repo to the local default working folder on the agent machine. And all the changes are done with the local files cloned from your azure repo on the agent machine .

If you want to push the changes to your azure repo. You may have to use another scrip task to run the git commands. Please check out below scripts:

- powershell: | 
        git config --global user.email "your@email.com"
        git config --global user.name "yourUsername"

        #git add filename.ext
        git add .
        git commit -m "message" 

        git push https://$(System.AccessToken)@dev.azure.com/yourOrg/yourProj/_git/repoName HEAD:master -q
  • There are also extension tasks available in the marketplace that you can use directly to update your web config. For example Magic Chunks task. You can check out the example in this thread.
Levi Lu-MSFT
  • 27,483
  • 2
  • 31
  • 43
0

There's no need to write this yourself. The app service deployment task supports deployment-time XML configuration transform via parameters.xml to define transforms and setparameters.xml to define values.

Daniel Mann
  • 57,011
  • 13
  • 100
  • 120