我正在尝试创建一个输出变量,稍后将用于其他作业。然而,负责创建它的作业有可能被跳过。为了从这种情况中恢复,我尝试将“占位符”附加到前面提到的变量中。然而,后期似乎没有正确评估不相等的条件
#这是基本管道:
trigger: none
pool: '<?>'
parameters:
- name: 'String'
type: string
displayName: 'String'
- name: 'skipA'
type: boolean
displayName: 'Skip stage A'
default: false
stages:
- stage: A
jobs:
- job: A1
condition: ne('${{ parameters.skipA }}', true)
steps:
- task: PowerShell@2
displayName: 'Run script 1'
name: script1
inputs:
pwsh: true
filePath: 'outputVariables\scripts\simpleScript.ps1'
arguments: >-
-inputValue ${{ parameters.String }}
- stage: B
variables:
myVar: $[ stageDependencies.A.A1.outputs['script1.myVar'] ]
dependsOn: A
jobs:
- job: B1
steps:
- task: PowerShell@2
displayName: 'Run script 1'
name: script1
inputs:
pwsh: true
filePath: 'outputVariables\scripts\simpleScript.ps1'
arguments: >-
-inputValue "placeholder,$(myVar)"
- job: B2
steps:
- template: 'templateFile.yml'
parameters:
somethingElse: '$(myVar),placeholder'
#templateFile.yml:
parameters:
- name: 'somethingElse'
type: string
steps:
- task: PowerShell@2
condition: ne('${{parameters.somethingElse}}', ',placeholder') #NOT EVALUATED CORRECTLY
displayName: 'Run script 3'
name: script1
inputs:
pwsh: true
filePath: 'outputVariables\scripts\simpleScript.ps1'
arguments: >-
-inputValue "${{parameters.somethingElse}}"
- script: echo step 2.1
#ps1 脚本:
param (
[Parameter(Mandatory=$false)]
[string]$inputValue = "Def val"
)
# Print the input value to the screen
Write-Output "You entered: $inputValue"
Write-Host "##vso[task.setvariable variable=myVar;isOutput=true]$inputValue"
我希望 B2 作业不运行 simpleScript
根据描述,您希望在管道
运行时的脚本中生成变量
myVar
的值,并将其作为参数传递到templateFile.yml
。但是,我们不应该评估模板表达式${{parameters}}
中的参数值,这些值在编译时、运行时之前进行处理。请参阅此文档来帮助我们理解变量语法
相反,我们应该选择在运行时表达式
newVar
中定义 $[variables]
并在步骤的 condition
属性中评估其值。这是可供您参考的工作 YAML 语法。
basePipeline.yml
trigger: none
pool: 'Default'
parameters:
- name: 'String'
type: string
displayName: 'String'
default: ''
- name: 'skipA'
type: boolean
displayName: 'Skip stage A'
default: false
stages:
- stage: A
jobs:
- job: A1
condition: ne('${{ parameters.skipA }}', true)
steps:
- task: PowerShell@2
displayName: 'Run script 1'
name: script1
inputs:
pwsh: true
filePath: 'outputVariables\scripts\simpleScript.ps1'
arguments: >-
-inputValue "${{ parameters.String }}"
- powershell: |
Write-Host "MyVar is $(script1.myVar)"
displayName: 'Check output'
- stage: B
variables:
myVar: $[ stageDependencies.A.A1.outputs['script1.myVar'] ]
stageDeps: $[ convertToJson(stageDependencies) ]
dependsOn: A
jobs:
- job: B1
steps:
- task: PowerShell@2
displayName: 'Run script 2'
name: script2
inputs:
pwsh: true
filePath: 'outputVariables\scripts\simpleScript.ps1'
arguments: >-
-inputValue "placeholder,$(myVar)"
- powershell: |
Write-Host "myVar output in A1 is $(myVar)"
Write-Host "myVar output in B1 is $(script2.myVar)"
Write-Host "stageDeps is $(stageDeps)"
displayName: 'Check output'
- job: B2
variables:
newVar: $[ format('{0}{1}', variables['myVar'], ',placeholder') ]
steps:
- template: 'templateFile.yml'
parameters:
somethingElse: '$(newVar)'
templateFile.yml
parameters:
- name: 'somethingElse'
type: string
steps:
- task: PowerShell@2
condition: ne(variables['newVar'], ',placeholder') # Evaluation the variable value generated during pipeline runtime
displayName: 'Run script 3'
name: script3
inputs:
pwsh: true
filePath: 'outputVariables\scripts\simpleScript.ps1'
arguments: >-
-inputValue "$(newVar)"
- powershell: |
Write-Host "newVar defined in B2 is $(newVar)"
condition: always()
displayName: 'Check output'