Azure DevOps发布流水线 – 如何确保输出变量的全名保持一致

huangapple go评论67阅读模式
英文:

Azure DevOps Release pipeline - How to ensure full name of output variable stays consistent

问题

I am working on a Release pipeline in Azure DevOps, and trying to pass an output variable between different tasks in the same job.

The pipeline looks roughly like this:

Stage
|- Job A
    |- Task group 1
		|- Task 1.1
		|- Task 1.2 - Set output variable 'foo=bar' in PowerShell task
	|- Task group 2
	|- Task group 3 - Accept variable 'foo' as parameter with value 'bar'
|- Job B
    |- Task group 1
		|- Task 1.1
		|- Task 1.2
		|- Task 1.3 - Set output variable 'foo=abc' in PowerShell task
	|- Task group 2
	|- Task group 3 - Accept variable 'foo' as parameter with value 'abc'

I am able to set the output variable 'foo' to the expected value. However, when trying to list all variables (via Bash task 'env | sort'), in environment variables I can see its name is prefixed with task name like "PowerShell4".

As a result, for task parameter I have to use $(POWERSHELL4.FOO) instead of $(FOO). This is not a reliable name (imagine if a new PowerShell task gets added to the pipeline before my custom task).

Microsoft documentation on this suggests giving a reference name to the task:
> In the Output variables section, give the producing task a reference name. Then, in a downstream step, you can use the form $(<ReferenceName>.<VariableName>) to refer to output variables.

However, I am not able to set reference name ('refName') for the PowerShell task which sets the output variable, because there is no such option in the UI.

This is easy to do using YAML pipeline instead of Classic pipeline, however in this case it is not an option.

Question: Is there a way I can use to reliably get the value of output variable using a fixed name (such as $(MYCUSTOMNAME.FOO)), and pass it as a parameter to any other tasks/task groups in the same job in Classic pipeline?

英文:

I am working on a Release pipeline in Azure DevOps, and trying to pass an output variable between different tasks in the same job.

The pipeline looks roughly like this:

Stage
|- Job A
    |- Task group 1
		|- Task 1.1
		|- Task 1.2 - Set output variable 'foo=bar' in PowerShell task
	|- Task group 2
	|- Task group 3 - Accept variable 'foo' as parameter with value 'bar'
|- Job B
    |- Task group 1
		|- Task 1.1
		|- Task 1.2
		|- Task 1.3 - Set output variable 'foo=abc' in PowerShell task
	|- Task group 2
	|- Task group 3 - Accept variable 'foo' as parameter with value 'abc'

I am able to set the output variable 'foo' to the expected value. However, when trying to list all variables (via Bash task env | sort), in environment variables I can see its name is prefixed with task name like "PowerShell4".

As a result, for task parameter I have to use $(POWERSHELL4.FOO) instead of $(FOO). This is not a reliable name (imagine if a new PowerShell task gets added to the pipeline before my custom task).

Microsoft documentation on this suggests giving a reference name to the task:
> In the Output variables section, give the producing task a reference name. Then, in a downstream step, you can use the form $(<ReferenceName>.<VariableName>) to refer to output variables.

However, I am not able to set reference name (refName) for the PowerShell task which sets the output variable, because there is no such option in the UI.

This is easy to do using YAML pipeline instead of Classic pipeline, however in this case it is not an option.

Question: Is there a way I can use to reliably get the value of output variable using a fixed name (such as $(MYCUSTOMNAME.FOO)), and pass it as a parameter to any other tasks/task groups in the same job in Classic pipeline?

答案1

得分: 0

I implemented a workaround for this issue by removing the task parameter from Classic pipeline task group and picking up the value 'bar'/'abc' directly from environment variables in the target script, like so:

$variable = Get-ChildItem env: |
    Where-Object "Name" -like "*_foo" |
    Where-Object "Value" -ne $null |
    Select-Object -First 1
$foo = $variable.Value
英文:

In the end I implemented a workaround for this issue by removing the task parameter from Classic pipeline task group and picking up the value 'bar'/'abc' directly from environment variables in the target script, like so:

$variable = Get-ChildItem env: `
    | Where-Object "Name" -like "*_foo" `
    | Where-Object "Value" -ne $null `
    | Select-Object -First 1
$foo = $variable.Value

huangapple
  • 本文由 发表于 2023年6月29日 21:02:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76581326.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定