i have CI/CD environment in azure devops.i specially want to get the user name who pushed his code to trigger pipeline.i want to use this info as a variable inside the pipeline.do you guys know how can i do that ?
2 Answers
Azure Devops passes that information to pipelines. Check Build.RequestedFor
variable: Build variables (DevOps Services), How are the identity variables set?
In Git or TFVC by the Continuous integration (CI) triggers, the Build.RequestedFor and Build.RequestedForId values are based on the person who pushed or checked in the changes.
How to use variables you can find here: Define variables

- 13,096
- 3
- 24
- 31
Actually we do have a predefined Build variable .
We can directly use $(Build.RequestedFor)
to get the the original user who kick off pipeline
If you want to pass this variable to any other customized variable/parametes.
You could use Logging Commands to set value during your build pipeline. ##vso[task.setvariable variable=testvar
The first task can set a variable, and following tasks in the same phase are able to use the variable. The variable is exposed to the following tasks as an environment variable.
Define and modify your variables in a script
To define or modify a variable from a script, use the task.setvariable logging command. Note that the updated variable value is scoped to the job being executed, and does not flow across jobs or stages. Variable names are transformed to uppercase, and the characters "." and " " are replaced by "_".
For example, Agent.WorkFolder becomes AGENT_WORKFOLDER. On Windows, you access this as %AGENT_WORKFOLDER% or $env:AGENT_WORKFOLDER. On Linux and macOS, you use $AGENT_WORKFOLDER.
More details please take a look at this tutorial Define and modify your variables in a script You could also look at this blog: Use Azure DevOps Variables Inline powershell in build and release pipelines

- 49,478
- 5
- 35
- 62
-
You do not need use `##vso[task.setvariable variable=testvar` to pass user name because `$(Build.RequestedFor)` available in all pipeline tasks. – Shamrai Aleksander Nov 12 '20 at 11:47