use custom environment variables in the nuget pack action of the azure pipeline

so I don’t really understand how come the NuGet task tells me it does not find the variable.

That because the task in the pipeline could not directly identify environment variables.

We need use the Logging Command to set this variable in the last line of the Inline powershell task:

Write-Host ("##vso[task.setvariable variable=NugetVersion]$env:NugetVersion")

In this case, the next task could use the this variable.

Now, I could use command line task to show it:

echo $(NugetVersion)

enter image description here

However, another problem is that you could not set the variable as the package version, because it does not meet the specified rules of the package version, and the package version needs to comply with Semantic Versioning 2.0.0. If we use that variable, we will get following error:

The nuget command failed with exit code(1) and error(Version string specified for package reference 'er.637402549809904' is invalid.

enter image description here

So, if the powershell script you provide is your real script, you may also need to reconsider how to get a valid package version.

BTW, for the $env:NugetVersion = $env:BUILD_SOURCEBRANCHNAME;, it seems you are set the branch name rather than a number as nuget version, This seems unreasonable, you may need to confirm this.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top