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

时间:2020-04-08 05:30:59

标签: azure azure-devops azure-pipelines azure-pipelines-yaml

我想为一个阶段定义一个布尔变量,并希望在该阶段中一个任务的另一个脚本中设置它(真/假),并希望将该变量用作其他后续任务中的条件,是吗?

我的azure管道基于YAML。

1 个答案:

答案 0 :(得分:1)

我想为一个阶段定义一个布尔变量,并想要设置 在阶段中的一个任务中的另一个脚本中包含它(真/假) 并希望将该变量用作其他后续条件 任务,我该怎么办?

这里是一个例子:

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是工作范围值。如果您在一个阶段中有多个工作,那将无法正常工作。

Update1:​​

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

我的Test.ps1:

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