Mustaque Ali
Mustaque Ali

Reputation: 167

Azure Build - How to get the user details who triggered the current build run and use that details for Git config

As a solution suggested for D365 - Azure Devops - Git Repo - How to check the history of a file which is stored in separate folders in the Main branch, I have created a Master folder and manually copying the patch files to this backup repo and committing in ADO GIT to track the history.

Currently using my PAT and every check-in shows my user ID.

I would like to get the user details who triggers the build and use their name, mail ID and PAT and use them for GIT Config, Commit and Push commands.

The AZ Pipelines Build or Runs command line help with an already completed run where I had to pass the run ID.

az pipelines runs show --id 13134 --org $orgname --project $projectname

Do we have options to take the user details for the current run?

Upvotes: 2

Views: 5646

Answers (1)

danielorn
danielorn

Reputation: 6147

There are a few different predefined system variables in Azure Devops Pipelines that holds various identities.

  • Build.QueuedBy (and Build.QueuedById)
  • Build.RequestedFor (and Build.RequestedForId)

The value depends on what caused the build, the following table can be found in the docs under How are identitiy variables set?

If the build is triggered... Then the Build.QueuedBy and Build.QueuedById values are based on... Then the Build.RequestedFor and Build.RequestedForId values are based on...
In Git or TFVC by the Continuous integration (CI) triggers The system identity, for example: [DefaultCollection]\Project Collection Service Accounts The person who pushed or checked in the changes.
In Git or by a branch policy build. The system identity, for example: [DefaultCollection]\Project Collection Service Accounts The person who checked in the changes.
In TFVC by a gated check-in trigger The person who checked in the changes. The person who checked in the changes.
In Git or TFVC by the Scheduled triggers The system identity, for example: [DefaultCollection]\Project Collection Service Accounts The system identity, for example: [DefaultCollection]\Project Collection Service Accounts
Because you clicked the Queue build button You You

Based on your questionyou are looking for the user who requested the build (or generated the action that queued the build rather than the account (usually a service account) that did the actual queueing). Thus you should look into the Build.RequestedFor variable for the username or Build.RequestedForId for the ID of the user.

The predefined system variables are injected as environment variables when your pipeline runs:

System and user-defined variables also get injected as environment variables for your platform. When variables are turned into environment variables, variable names become uppercase, and periods turn into underscores. For example, the variable name any.variable becomes the variable name $ANY_VARIABLE.

Upvotes: 9

Related Questions