Home > Mobile >  Azure pipeline powershell script returns incorrect result
Azure pipeline powershell script returns incorrect result

Time:04-27

I have the following tasks in an azure pipeline:

      - task: PowerShell@2
        displayName: Identiying the version fro the .csproj file
        inputs:
          targetType: "inline"
          script: |
            $xml = [Xml] (Get-Content ./src/MyProject/MyProject.csproj)
            $version = $xml.Project.PropertyGroup.Version
            echo $version
            echo "##vso[task.setvariable variable=version]$version"

      - task: PowerShell@2
        displayName: Checking if the git tag with current version already
        inputs:
          targetType: "inline"
          script: |
            $ver=git tag -l $(version)                
            Write-Host "Project file version is $(version)"
            Write-Host "Received from git tag $ver"
            if ($(version) -eq $ver) {
                    Write-Error "Tag $(version) already exists" 
                }
            else {
              Write-Host "Tag does not exist"
              }

It basically pulls the version from the csproj file, create a pipeline parameter and then calls git tag -l to check if that version already exists as tag. The problem here is that the versions we extract from the .csproj file and the git are the same, yet the if command is not evaluated but rather else is executed. This is the output log of the azure pipeline:

"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Unrestricted -Command ". 'D:\a\_temp\e1cb6bb9-fbd4-4282-a4cc-e74f89ac7660.ps1'"
Project file version is 0.2.5
Received from git tag 0.2.5
Tag does not exist

Why does this happen since they are equal? The result should be Tag 0.2.5 already exists

CodePudding user response:

If $(version) -eq $ver is false, then maybe they are different types somehow? Try manually converting to strings:

if ("$(version)" -eq "$ver") {

in regards to your comment - I'm not really familiar with az pipeline, but maybe you can convert your remote variables like so?

[string]($(version))

types are also objects, so to see the current type as a string, use $var.GetType().name instead

  • Related