尽管 ("$(Resources.TriggeringAlias)" -eq "CCM_LV_Pipeline") 的结果为 true 下面所示的天蓝色管道中的变量“组件”没有更新?
variables:
newrel: "0.4.7"
component: ""
trigger: none
pr: none
pool:
name: CC-DEV
resources:
pipelines:
- pipeline: CCM_LV_Pipeline
source: HYBM_CCM_LV
project: CC-Wolf
trigger:
branches:
include:
- '*'
jobs:
- job: DetermineComponentDirectory
displayName: "Determine Component Directory"
steps:
- checkout: none
- powershell: |
if ("$(Resources.TriggeringAlias)" -eq "CCM_LV_Pipeline") {
Write-Host "CCM_LV_Pipeline in CC-Wolf succeeded"
Write-Output "##vso[task.setvariable variable=component;isOutput=true]$(Build.SourcesDirectory)\assets\ccm-lv"
}
else {
Write-Host "No successful pipeline found in CC-Wolf. Using default component directory."
Write-Output "##vso[task.setvariable variable=component;isOutput=true]$(Build.SourcesDirectory)\assets\default"
}
Write-Host "Component directory set to: $(component)"
displayName: "Set Component Directory"
```
正如您在另一种情况中所澄清的那样,以下命令行是创建一个与现有通用变量不同的输出变量,即使它们具有相同的名称(组件)。他们有不同的表达方式来获取他们的价值观。
Write-Output "##vso[task.setvariable variable=component;isOutput=true]xxxx"
此外,在调用“
setvariable
”命令设置/更新变量的PowerShell任务上,新值不适用于当前PowerShell任务。新值仅适用于后续任务。
如果您只想更新现有通用变量(component)的值并且只想在当前作业中使用新值(DetermineComponentDirectory),您应该使用如下命令。
variables:
newrel: "0.4.7"
component: "$(Build.SourcesDirectory)/assets/default" # The default component directory.
resources:
pipelines:
. . .
jobs:
- job: DetermineComponentDirectory
displayName: "Determine Component Directory"
steps:
- task: PowerShell@2
displayName: "Set Component Directory"
inputs:
targetType: inline
script: |
if("$(Resources.TriggeringAlias)" -eq "CCM_LV_Pipeline") {
Write-Host "CCM_LV_Pipeline in CC-Wolf succeeded"
Write-Output "##vso[task.setvariable variable=component;]$(Build.SourcesDirectory)/assets/ccm-lv"
}
else {
Write-Host "No successful pipeline found in CC-Wolf. Using the default component directory."
}
- task: PowerShell@2
displayName: 'Print component directory'
inputs:
targetType: inline
script: Write-Host "Component directory set to $(component)"
如果您还想将更新后的值传递给同一管道中的后续作业或阶段,您可以坚持将其设置为输出变量。关于输出变量,可以看到“使用任务中的输出变量”。
variables:
newrel: "0.4.7"
resources:
pipelines:
. . .
stages:
- stage: A
jobs:
- job: DetermineComponentDirectory
displayName: "Determine Component Directory"
steps:
- task: PowerShell@2
displayName: 'Set Component Directory'
name: setCompDir
inputs:
targetType: inline
script: |
if("$(Resources.TriggeringAlias)" -eq "CCM_LV_Pipeline") {
Write-Host "CCM_LV_Pipeline in CC-Wolf succeeded"
Write-Output "##vso[task.setvariable variable=component;isOutput=true]$(Build.SourcesDirectory)/assets/ccm-lv"
}
else {
Write-Host "No successful pipeline found in CC-Wolf. Using the default component directory."
Write-Output "##vso[task.setvariable variable=component;isOutput=true]$(Build.SourcesDirectory)/assets/default"
}
- task: PowerShell@2
displayName: 'Print component directory'
inputs:
targetType: inline
script: Write-Host "Component directory set to $(setCompDir.component)"
# Pass the output variable to another job within the same stage.
- job: A2
dependsOn: DetermineComponentDirectory # Must dependsOn the previous job which generates the output variable.
variables:
ComponentDirectory: $[ dependencies.DetermineComponentDirectory.outputs['setCompDir.component'] ]
steps:
- task: PowerShell@2
displayName: 'Print component directory'
inputs:
targetType: inline
script: Write-Host "The Component directory is $(ComponentDirectory)"
# Pass the output variable to another stage within the same pipeline.
- stage: B
dependsOn: A # Must dependsOn the previous stage which generates the output variable.
variables:
ComponentDirectory: stageDependencies.A.DetermineComponentDirectory.outputs['setCompDir.component']
jobs:
- job: B1
steps:
- task: PowerShell@2
displayName: 'Print component directory'
inputs:
targetType: inline
script: Write-Host "The Component directory is $(ComponentDirectory)"