Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

passing pipeline variable as argument into Powershell build step not working in Azure DevOps

I want to be able to pass a pipeline variable into a Powershell build step in Azure DevOps Server. I'm trying to accomplish this by passing the variable as an argument.

Here is my pipeline YAML file:

- task: PowerShell@2
  displayName: 'Detect Subfolder Changes'
  name: setvarStep
  inputs:
    targetType: 'filePath'
    filePath: $(System.DefaultWorkingDirectory)detectchanges.ps1
    failOnStderr: true
  
- task: PowerShell@2
  displayName: 'Get Version Number'
  name: getVersion
  inputs:
    arguments: >
      - packagepath $(setvarStep.changedPackage)
    targetType: 'filePath'
    filePath: $(System.DefaultWorkingDirectory)getversion.ps1
    failOnStderr: true

Here is the Powershell script (getversion.ps1) where I pass the 'packagepath' argument:

param($packagepath)

Write-Host "packagepath is: $packagepath"

$xml = [Xml] (Get-Content $packagepath)
$version = [Version] $xml.Project.PropertyGroup.Version

"##vso[task.setvariable variable=packageVersion;isOutput=true]$version"

The Writ-Host command prints out 'packagepath is: -'. I turned on debugging and the argument value does show up in the log as: ##[debug]INPUT_ARGUMENTS: '- packagepath dotnet/TestPackage/*.csproj'

Instead of passing the variable as an argument I also tried inserting the variable into the script like this: $packagepath = $($env:SETVARSTEP.CHANGEDPACKAGE) but it didn't work.

I did try adding double quotes to the argument parameter in the build step like this: - packagepath "$(setvarStep.changedPackage)". It didn't work either.

The argument value is getting to the Powershell script, getversion.ps1 but for some reason the value is not displaying.

question from:https://stackoverflow.com/questions/65906620/passing-pipeline-variable-as-argument-into-powershell-build-step-not-working-in

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

@g.sulman Thanks for the suggestion. I changed - packagepath to -packagepath and it worked. I figured it was something small like the syntax.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...