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

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

我想为一个阶段定义一个 bool 变量,并希望在该阶段的任务之一的另一个脚本中设置它(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"

关于Azure - 创建 bool 变量并在多个任务中使用它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61093897/

相关文章:

.net - Azure 开发运营 : Make Nuget-Package with AssemblyVersion

azure - 如何在 Azure Devops VSO 上进行调试

azure-devops - 在 Azure DevOps Yaml 管道中调试

c# - 当字符串长度超过 32768 个字符时,Azure 存储异常

javascript - 通过将包添加到 Node 包管理器 package.json 文件来安装包?

azure - 将 azure-pipelines.yaml 迁移到单独的存储库,但在其他存储库的代码上运行

azure-devops - 如何检索用于 Azure Dev-Ops Analytics Velocity Dashboard Widget 的 Velocity 数据?

Azure Function App 在发布后无法运行,但可以在本地通过 VS2017 运行

azure - Windows Azure 服务总线队列 : Throttling and TOPAZ

azure - 等待新部署的应用服务在 Azure Devops 发布管道中重新启动