Dynamically adjust file in Windows executor

Updating Files

You may have a situation where you need to dynamically change or update a file within the Windows executor. Usually, this would be to update a config file.

 

XML File Change Example

If the file you need to change is written 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.

 

Additional Resources

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

Comments

0 comments

Article is closed for comments.