MicrosoftPremier / VstsExtensions

Documentation and issue tracking for Microsoft Premier Services Visual Studio Team Services Extensions
MIT License
56 stars 14 forks source link

How to configure "Task Variables"? #184

Closed Bouke closed 2 years ago

Bouke commented 2 years ago

The documentation has a section on Task Variables. From the provided documentation it is not clear to me how I can configure these Task Variables. Can you provide an example of how to do that?

ReneSchumacher commented 2 years ago

Hi @Bouke,

there are basically two types of task variables: the ones you linked in your comment are used to change the behavior of the task and must be set either in your pipeline definition itself or during queue time. The other type is task output variables that are created by the task to access some of the task results (see here for the warnings policy and here for the code coverage policy).

Behavior Variables

BQC.ForceNewBaseline (boolean) can be used to make the policy pass even though the policy value is lower than the current baseline value. Usually, you want to set this variable at queue time (see also here) or through a parameter in YAML like this:

parameters:
  - name: ForceNewBaseline
    displayName: 'Force new baseline'
    type: boolean
    default: false

variables:
  BQC.ForceNewBaseline: ${{ parameters.ForceNewBaseline }}

This renders a checkbox in the pipeline queue dialog that lets you easily force a new baseline for policies if needed like this: image

BQC.LogRawData (boolean) is only needed for diagnostics purposes. Usually, I ask people to set the variable when there is an issue and I need to see the raw JSON data that is returned by the Azure DevOps APIs we call from the task. It must always be set together with the System.Debug variable since raw data is only logged if debug output is enabled. You usually set both at queue time and only when needed.

BQC.Statistics.MaxAddedWarnings and BQC.Statistics.MaxRemovedWarnings (both numeric values) define the number of added and/or removed warnings displayed in warnings statistics. You usually set these variables in your pipeline definition like this:

variables:
  BQC.Statistics.MaxAddedWarnings: 50
  BQC.Statistics.MaxRemovedWarnings: 0

The example limits the number of added warnings displayed in statistics to 50 and does not show removed warnings at all.

Finally, BQC.DisableVariableCreation (boolean) is only used on Team Foundation Server 2017 to disable the creation of output variables. If you're using a newer version of server or services, just ignore this.

Output variables

Output variables can be used to run or configure other tasks in your pipeline based on the result of the Build Quality Checks task. Here's an example in YAML:

steps:
  - task: BuildQualityChecks@8
    name: BQC
    displayName: 'Ensure that code coverage is not decreasing'
    inputs:
      checkCoverage: true

  - pwsh: |
      $currentCoverage = $(BQC.CodeCoveragePolicy.Percentage.Covered)
      if ($currentCoverage -lt 50) {
        Write-Host "Ah, come on, you can do better than that!"
      }
      if (($currentCoverage -ge 50) -and ($currentCoverage -lt 80)) {
        Write-Host "Good job! Keep the unit tests coming!"
      }
      if ($currentCoverage -ge 80) {
        Write-Host "Yeah! You're the code coverage champ!"
      }
    displayName: 'Make a smart remark'

As you can see, the Build Quality Checks task is called and associated with the name BQC. When it completes, its output variables can be used by any subsequent task. In my case, I'm using a PowerShell script to create some log output based on the current code coverage percentage value.

Hope that helps.

Bouke commented 2 years ago

Thank you for this extensive write-up, this really helps! Our CI builds are started automatically, so I haven't really worked with changing variables when queueing the build.