Dynamically adjust file in Windows executor

You may have a situation where you need to dynamically change or update a file within the Windows executor. Usually, this would to update a config file. If the file is in XML, the following is an example of how such an update would be made:

jobs:
  build:
    machine:
      image: windows-server-2019-vs2019:stable
    shell: powershell.exe
    environment:
      NEW_URL: "https://newurl.org"
    steps:
      - checkout
      - run:
          name: Update config file
          command: |
            Write-Host("Displaying Original Config");
            type C:\Users\circleci\project\test.config
            [xml]$XmlDocument = Get-Content -Path C:\Users\circleci\project\test.config
            $url = $XmlDocument.SelectSingleNode("configuration/Test/add[@value='https://something.com/']");
            $url.value = "$env:NEW_URL";
            $XmlDocument.Save("C:\Users\circleci\project\test.config")
            Write-Host("Displaying Updated Config");
            type C:\Users\circleci\project\test.config

The above run command is adjusting the URL present in the following test.config file:

Screen_Shot_2021-02-03_at_2.27.02_PM.png

The Write-Host commands are present just to verify the change and can be removed as needed.

Was this article helpful?
1 out of 1 found this helpful