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
Since you have set
payrollCurrentVersionNumber
as an output variable usingisOutput=true
, you need to refer the variable by including its task name.So you can use
$(myTask.myVar)
in theNuGetCommand@2
taskRead more about the Levels of output variables
The issue is that the Write-Host cmdlet in
PowerShell
is not actually setting the value of thepayrollCurrentVersionNumber
variable. It is just printing the value to the console. To set the variable topayrollCurrentVersionNumber,
you can use the following command:Using this command, the value will be stored in
payrollCurrentVersionNumber
, and same you can use it in NuGetHere is the updated
Yaml
script.You can use the
payrollCurrentVersionNumber variable
in theNuGet Pack
task by setting theversionEnvVar
parameter topayrollCurrentVersionNumber
.