microsoft/azure-pipelines-yaml

How to use `Build.SourceVersionAuthor`?

RussKie opened this issue · 3 comments

I need to conditionalise the build based on whether it is a pull-request from a branch name with a specific name submitted by a specific author.

I able to express the first two conditions but I can't figure out the last one.
These work

variables:
  - name: A_IsPullRequest
    value: ${{ eq(variables['Build.Reason'], 'PullRequest') }}
  - name: A_PullRequestSourceBranch
    value: $[ variables['System.PullRequest.SourceBranch'] ]
  - name: A_IsDarcPullRequestSourceBranch
    value: $[ startsWith(variables['System.PullRequest.SourceBranch'], 'darc-main-') ]

  - name: A_SourceVersionAuthor
    value: $(Build.SourceVersionAuthor)

I can use $(Build.SourceVersionAuthor), however, more matter what I do I can't figure out how to apply a condition to it. E.g., none of these work:

variables:
  # These are unset
  - name: A_SourceVersionAuthor2
    value: $[ variables['Build.SourceVersionAuthor'] ]
  - name: A_SourceVersionAuthor3
    value: ${{ variables['Build.SourceVersionAuthor'] }}

  # These are false
  - name: A_IsMaestroAuthor1
    value: ${{ eq(variables['Build.SourceVersionAuthor'], 'dotnet-maestro[bot]') }}
  - name: A_IsMaestroAuthor3
    value: ${{ eq('$(Build.SourceVersionAuthor)', 'dotnet-maestro[bot]') }}
  - name: A_IsMaestroAuthor4
    value: $[ eq(variables['Build.SourceVersionAuthor'], 'dotnet-maestro[bot]') ]
  - name: A_IsMaestroAuthor5
    value: $[ eq('$(Build.SourceVersionAuthor)', 'dotnet-maestro[bot]') ]

image

I can evaluate the whole thing using a script task, but it feels lame...

- pwsh: |
      function Export { param($i); Write-Host "$i"; Write-Host "##$i" }

      $isPullRequest = '${{ eq(variables['Build.Reason'], 'PullRequest') }}';
      $sourceBranchName = "$($env:SYSTEM_PULLREQUEST_SOURCEBRANCH)";
      $sourceBranchAuthor = "$($env:BUILD_SOURCEVERSIONAUTHOR)";
      $isMaestroBuild = $isPullRequest -and $sourceBranchName.StartsWith('darc-main-') -and ($sourceBranchAuthor -eq 'dotnet-maestro[bot]')
      Export "vso[task.setvariable variable=isMaestroBuild]$isMaestroBuild"

Hi @RussKie, Please post your question on the Developer Community to get the right eyes on it, since this repo is mostly for questions about templates located in this repo.

It seems like SourceVersionAuthor is available only in runtime. You can use conditions like this:

- script: |
    echo $(Build.SourceVersionAuthor)
  condition: eq(variables['Build.SourceVersionAuthor'], 'dotnet-maestro[bot]')

Thank you @max-zaytsev. That's a bummer really, I hoped I could craft my pipeline a bit more declaratively.

It'd be great to have this documented in https://learn.microsoft.com/azure/devops/pipelines/build/variables?view=azure-devops&tabs=yaml#build-variables-devops-services too.

Thanks again.

Thank you @RussKie

Yes, I agree. It’s quite difficult to find this information in the documentation.
I actually found this statement on the Template types & usage page page.

Only predefined variables can be used in template expressions.

Since Build.SourceVersionAuthor is not in the list of predefined variables it is an indication that it can’t be used in such expressions.

I couldn't find the information about predefined variables on the Expressions page too. I wish there was some mention of this as well.

I will close this ticket as this issue does not match the current repository and will create an issue in the appropriate repo in order to update the documentation accordingly.

Please let me know if I can help with anything else here.

Thank you!