如何在Azure Pipelines中参数化启用任务的属性

时间:2020-09-12 17:42:53

标签: azure-pipelines

由于最近的SonarCloud中断,我决定终于可以为管道中的SonarCloud任务添加一个true / false标志。因此,我正在检查是否有可能在enabled:属性中进行检查,并在其中传递IsSonarEnabled这样的变量,该变量将为真或为假。

谢谢, 德文

1 个答案:

答案 0 :(得分:0)

所以我能够做到这一点,但是不能通过enabled:属性,而不能直接使用变量值。

首先,我不得不使用condition:,因为我认为enabled:不能很好地与运行时变量配合使用。

另外,我决定使用全局变量,因为我有12条管道,而在我的情况下,关闭sonarcloud的用例仅是在停机期间,因此我没有太多用在将参数放入各个管道中。因此,我向库中添加了一个变量组- FeatureFlags ,并添加了变量IsSonarEnabled_Global,并在我的管道YML中对其进行了引用。

  variables:
  - group: FeatureFlags

我还发现变量的结果是字符串,所以我不得不做一个字符串比较,所以我使用了 eq(variables.IsSonarEnabled_Global, 'true')

最后,我的管道看起来像这样

trigger: 
  tags:
    include: 
    - dev1000

jobs:

- job: Build
  timeoutInMinutes: 120

  pool:
    vmImage: 'windows-latest'

  variables:
  - group: FeatureFlags

  steps:

  - task: SonarCloudPrepare@1
    condition: eq(variables.IsSonarEnabled_Global, 'true')
    ## stripped for brevity ##
    
  - task: SonarCloudAnalyze@1
    condition: eq(variables.IsSonarEnabled_Global, 'true')

  - task: SonarCloudPublish@1
    condition: eq(variables.IsSonarEnabled_Global, 'true')
    inputs:
      pollingTimeoutSec: '300'

参考:https://docs.microsoft.com/en-us/azure/devops/pipelines/process/conditions?view=azure-devops&tabs=yaml