skip to Main Content

So I have a PowerShell script that generates a version number based on the current day and time and I want to use the result of this process inside a "NuGet Pack" task which takes in an environment variable as the version of the packed NuGet package how can I achieve this?
My YAML code:

trigger:
- master

pool:
  name: HrgWin
  vmImage: 'windows-latest'

variables:
  solution: '**/*.sln'
  buildPlatform: 'x86'
  buildConfiguration: 'ReleaseBeta'
  mainProj: 'PayRollPayRoll.csproj'
  publicProj: 'PublicPayRollDllPublicPayRollDll.csproj'
  reportProj: 'PayRollDllPayRollReportDll.csproj'
  nugConfig: $(NugetConfigPath)
stages:
  - stage: Build
    jobs:
      - job: BuildJob
        steps:
          - task: PowerShell@2
            inputs:
              targetType: 'inline'
              script: |
                # Create an instance of the PersianCalendar
                 $persianCalendar = New-Object System.Globalization.PersianCalendar
                
                 # Get the current date and time in Gregorian calendar
                 $gregorianDate = Get-Date
                
                 # Convert the Gregorian date to Shamsi (Jalali) calendar
                 $shamsiYear = $persianCalendar.GetYear($gregorianDate)
                 $shamsiMonth = $persianCalendar.GetMonth($gregorianDate)
                 $shamsiDay = $persianCalendar.GetDayOfMonth($gregorianDate)
                
                 # Modify the Shamsi year to start with "99"
                 $modifiedShamsiYear = "99" + $shamsiYear.ToString().Substring(2)
                
                 $versionNumber = $modifiedShamsiYear + "." + $shamsiMonth + "." + $shamsiDay
                $patchedVersionNumber = $versionNumber + "." + $gregorianDate.ToString("HHmm")
                Write-Host "##vso[task.setvariable variable=payrollCurrentVersionNumber;isOutput=true]$patchedVersionNumber"
           - task: NuGetCommand@2
             inputs:
             command: 'pack'
             packagesToPack: '$(publicProj)'
             versioningScheme: 'byEnvVar'
             versionEnvVar: 'payrollCurrentVersionNumber'

As you can see I have tried using "##vso[task.setvariable variable=payrollCurrentVersionNumber;isOutput=true]$patchedVersionNumber" and yes I have tried putting the variables in these formats: $(payrollCurrentVersionNumber), payrollCurrentVersionNumber
I also have tired declaring an empty variable with the same name in the YAML file to see if it fills it but no.

2

Answers


  1. Since you have set payrollCurrentVersionNumber as an output variable using isOutput=true, you need to refer the variable by including its task name.

    • Output variables set in the same job without the isoutput parameter. To reference these variables, you’ll use macro syntax.
      Example: $(myVar).
    • Output variables set in the same job with the isoutput parameter. To reference these variables, you’ll include the task
      name. Example: $(myTask.myVar).

    So you can use $(myTask.myVar) in the NuGetCommand@2 task

    Read more about the Levels of output variables

    Login or Signup to reply.
  2. As you can see I have tried using "##vso[task.setvariable
    variable=payrollCurrentVersionNumber;isOutput=true]$patchedVersionNumber" and yes I have tried putting the variables in these formats: $(payrollCurrentVersionNumber),payrollCurrentVersionNumber I also have tired declaring an empty variable with the same name in the YAML file to see if it fills it but no

    The issue is that the Write-Host cmdlet in PowerShell is not actually setting the value of the payrollCurrentVersionNumber variable. It is just printing the value to the console. To set the variable to payrollCurrentVersionNumber, you can use the following command:

    Set-Variable -Name payrollCurrentVersionNumber -Value $patchedVersionNumber
    

    Using this command, the value will be stored in payrollCurrentVersionNumber, and same you can use it in NuGet

    Here is the updated Yaml script.

        stages:
          - stage: Build
            jobs:
              - job: BuildJob
                steps:
                  - task: PowerShell@2
                    inputs:
                      targetType: 'inline'
                      script: |
                        # Create an instance of the PersianCalendar
                        $persianCalendar = New-Object System.Globalization.PersianCalendar
        
                        # Get the current date and time in Gregorian calendar
                        $gregorianDate = Get-Date
        
                        # Convert the Gregorian date to Shamsi (Jalali) calendar
                        $shamsiYear = $persianCalendar.GetYear($gregorianDate)
                        $shamsiMonth = $persianCalendar.GetMonth($gregorianDate)
                        $shamsiDay = $persianCalendar.GetDayOfMonth($gregorianDate)
        
                        # Modify the Shamsi year to start with "99"
                        $modifiedShamsiYear = "99" + $shamsiYear.ToString().Substring(2)
        
                        $versionNumber = $modifiedShamsiYear + "." + $shamsiMonth + "." + $shamsiDay
                        $patchedVersionNumber = $versionNumber + "." + $gregorianDate.ToString("HHmm")
                        Set-Variable -Name payrollCurrentVersionNumber -Value $patchedVersionNumber
        
                  - task: NuGetCommand@2
                    inputs:
                      command: 'pack'
                      packagesToPack: '$(publicProj)'
                      versioningScheme: 'byEnvVar'
                      versionEnvVar: 'payrollCurrentVersionNumber'
    

    You can use the payrollCurrentVersionNumber variable in the NuGet Pack task by setting the versionEnvVar parameter to payrollCurrentVersionNumber.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search