Search code examples
azure-devopsnugetdevopspipeline

Using Build.SourceVersionMessage with ' and " in DevOps pipeline


In a azure DevOps pipeline I would like to use the Build.SourceVersionMessage variable as the releaseNotes in a NuGet.nuspec file I use to publish my artifact.

In the NuGet.nuspec I have this line <releaseNotes>$release_notes$</releaseNotes>.

I found that I need to escape the xml characters by using a powershell script like this.

- task: PowerShell@2
   displayName: 'Modify NuGet.nuspec with release notes'
   inputs:
     targetType: 'inline'
     script: |
       # I can not find a way to pass a value with semicolons via the -Properties to nuget pack. Therefor we modify the NuGet.nuspec file directly.
       $escapedMsg = [System.Security.SecurityElement]::Escape("$(Build.SourceVersionMessage)")
       Write-Host "Escaped release notes: $escapedMsg"
       (Get-Content "NuGet.nuspec") -replace '\$release_notes\$', $escapedMsg | Set-Content "NuGet.nuspec"

However this script still has problems in case the message contains double quotes. The powershell script fails because the string inside the Escape(...) is malformed.

Then I could use single quotes in the script but then it will not work in case the message has single quotes.

Is the any way to get a SourceVersionMessage with any characters into the NuGet.nuspec file ?

Thanks


Solution

  • I found that there is a very simple solution.
    Instead of using $(Build.SourceVersionMessage) that is being replaced before the powershell script is executed. I can use the environment variable BUILD_SOURCEVERSIONMESSAGE.
    Like this:

    - task: PowerShell@2
       displayName: 'Modify NuGet.nuspec with release notes'
       inputs:
         targetType: 'inline'
         script: |
           $escapedMsg = [System.Security.SecurityElement]::Escape($env:BUILD_SOURCEVERSIONMESSAGE)
           Write-Host "Escaped release notes: $escapedMsg"
           (Get-Content "NuGet.nuspec") -replace '\$release_notes\$', $escapedMsg | Set-Content "NuGet.nuspec"