azure-powershellazure-yaml-pipelines

Inline yaml pipeline scripts work separately, but not together


The following two inline yaml-pipeline powershell scripts:

- pwsh: (get-content -path $(versionHeader)) | foreach-object {$_ -replace "2.0.0.0", '$(major).$(minor).$(patch).0'} | set-content -path $(versionHeader)
  displayName: 'Update build number in header file'

- pwsh: (get-content -path $(versionHeader)) | foreach-object {$_ -replace "20200101000000", (get-date -f 'yyyyMMddhhmmss')} | set-content -path $(versionHeader)
  displayName: 'Update date in header file'

are meant to take these two lines

[assembly: MyApp.Net.Attributes.AssemblyVersion("2.0.0.0")]                                   
[assembly: MyApp.Net.Attributes.BuildDateAttribute("20200101000000")]

and turn them into these two lines (i.e. put new values in the quotes)

[assembly: MyApp.Net.Attributes.AssemblyVersion("2.0.185.0")] 
[assembly: MyApp.Net.Attributes.BuildDateAttribute("20200724013502")]

(The replacement values vary)

And either script works fine by itself. But when I try to use both scripts, one after the other, the second value comes out messed up.

[assembly: MyApp.Net.Attributes.AssemblyVersion("2.0.209.0")]                                // correct
[assembly: MyApp.Net.Attributes.BuildDateAttribute("202.0.209.000000")] // ?????

Obviously they are somehow interfering with each other but I don't know how. Can someone tell me what I'm doing wrong?


Solution

  • The problem is in powershell's -replace - it interprets wildcards, and it happens that 20200101000000 matches 2.0.0.0:

    PS> "20200101000000" -replace "2.0.0.0", "zzzzzz"
    20zzzzzz00000