Search code examples
azureazure-devopsyamlazure-pipelinessonarcloud

How to avoid to execute a task in Yaml template in Azure


I have a generic yaml template which is commonly used for many tasks in Azure, and we have some tasks related to sonarcloud and that is not required for few pipelines. And we are not supposed to update the existing template.

Is there some possible ways, so that we can skip using one particular task without updating template file from an Azure yaml pipeline script.

  - task: SonarCloudPrepare@1
    inputs:
      SonarCloud: 'sonarcloudtoken'
      organization: 'myorgname'
      scannerMode: 'Other'
      extraProperties: |
         sonar.projectKey=${{ parameters.mysonar_projectName }}
         sonar.projectName=${{ parameters.mysonar_projectName }}

Using the below script to call the generic template

jobs: 
  - template: templates/mygenerictemplate.yml

Any inputs would be really appreciated. Thank you !


Solution

  • Define a parameter to your template, and use it in a task condition.

    So the pipeline supplies a parameter to the template:

    jobs: 
      - template: templates/mygenerictemplate.yml
        parameters:
          enableSonarCloud: true
    

    And the template receives the parameter and acts conditionally on it:

    parameters:
      enableSonarCloud: false
    
    steps:
      - task: SonarCloudPrepare@1
        condition: and(succeeded(), ${{ parameters.enableSonarCloud }} )
        inputs:
          SonarCloud: 'sonarcloudtoken'
          organization: 'myorgname'
          scannerMode: 'Other'
          extraProperties: |
             sonar.projectKey=${{ parameters.mysonar_projectName }}
             sonar.projectName=${{ parameters.mysonar_projectName }}