Azure - 创建布尔变量并在多个任务中使用它

Azure - creating Boolean variables and use it across multiple tasks

我想为一个阶段定义一个布尔变量,并想在该阶段的一个任务的另一个脚本中设置它(True/False),并想将该变量用作其他后续任务的条件任务,我该怎么做?

我的 azure 管道是基于 YAML 的。

I want to define one Boolean variable for a stage and want to set it(True/False) inside another script in one of the task in the stage and want to use that variable as a condition in other subsequent tasks, How can I do it?

这是一个例子:

stages:
- stage: Build
  variables: 
    Key : false
  jobs:
  - job: Build
    continueOnError: true
    steps:
    - script: echo Key:'$(Key)'

    - task: PowerShell@2
      inputs:
        targetType: 'inline'
        script: |
          # Write your PowerShell commands here.

          Write-Host "##vso[task.setvariable variable=Key;]true"

    - task: CmdLine@2
      inputs:
        script: echo Key:'$(Key)'

    - task: CmdLine@2
      condition: and(succeeded(), eq(variables['Key'], 'true'))
      inputs:
        script: |
          echo Write your commands here

          echo Hello world

我有一个阶段变量Key,它的值为false。然后在 PS 任务中,我使用 task.setvariable 覆盖键值。 注意: 这样新值 true 是一个工作范围值。如果您在一个阶段有多个作业,则效果不佳。

更新 1:

- task: PowerShell@2
      inputs:
        filePath: 'Test.ps1'

我的测试。ps1:

Write-Host "##vso[task.setvariable variable=Key;]true"