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 ?
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
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
Related
I am setting up an Azure pipeline and need to get a specific step to run multiple times based on the defined environments. I also need to replace a placeholder in a file with variable values and I need to have different value for each environment. What is the most straight forward way to achieve that?
If you are using classic build pipeline or release pipeline, you can use task groups.
A task group allows you to encapsulate a sequence of tasks, already defined in a build or a release pipeline, into a single reusable task that can be added to a build or release pipeline, just like any other task. You can choose to extract the parameters from the encapsulated tasks as configuration variables, and abstract the rest of the task information. For more info about task group, please refer to Task groups for builds and releases (classic)
If you are using YAML pipeline, you can use templates. Templates let you define reusable content, logic, and parameters. Templates function in two ways. You can insert reusable content with a template or you can use a template to control what is allowed in a pipeline. For more info about template, please refer to Template types & usage.
If you want to pass different values to the template according to different environments, you can use Runtime parameters.
You can configure your task to run conditionally (when environmentname contains TEST, a certian variable is set to true etc...). Here you can see how to configure this -> https://www.bizstream.com/blog/custom-task-conditions-in-azure-pipelines/
I assume you are running a release-Pipeline? If so you can create a variable with the same name multiple times but set different stages where the variable will apply to. See here for example -> https://crmchap.co.uk/working-with-variables-in-an-azure-devops-release-pipeline/ There is a variable 'MySQLDBPassword' defined with "Scope"="Release". You can add a second variable with the same name but set the scope to the Stage/Environment-Name you want this to work with
When you run a pipeline from the Gitlab UI, you are open to add variables to the run:
From the overview of finished pipelines, is there a way to find the set variables and values of specific runs?
As far as I know, these are one time variables you set by running a pipeline manually. You cannot view the the variables in the UI. Instead, like #Origin said you can echo the variable in the job.
example:
script:
- '& echo "$MY_VARIABLE"'
I would recommend you to save your variables in the default CI/CD Settings. (Settings -> CI/CD -> Variables). There you can set, save, protect (from other user roles) and view them for sure. These variables will be used in any pipeline run you do.
I am trying to create a JS utility to version stamp a VSTS build with details about the branch and commit id.
I have been using git-rev-sync which works fine locally. However, when the code is checked out using a VSTS build definition, the repo is detached, and I am no longer able to determine from the git repo itself which branch the current code belongs to.
git-rev-sync reports something along the lines of:
Detatched: 705a3e89206882aceed8c9ea9b2f412cf26b5e3f
Instead of "develop" or "master"
I may look at the vsts node SDK which might be able to pick up VSTS environmental variables like you can with Powershell scripts.
Has anyone done this or solved this problem in a neater way?
The build variables will be added to current process’s environment variables, so you can access Build.SourceBranchName build-in variable from environment variable:
PowerShell:
$env:BUILD_SOURCEBRANCHNAME
NodeJS:
process.env.BUILD_SOURCEBRANCHNAME
Shell script:
$BUILD_SOURCEBRANCHNAME
Batch script:
%BUILD_SOURCEBRANCHNAME%
You also can pass it through argument of task ($(Build.SourceBranchName)), for example, using Replace Tokens task to replace variable value to a file, then you can read the value from the file (replace %{BUILD_SOURCEBRANCHNAME}%).
I am confused about where to use a variable and where to use a parameter in ARM templates. How do we make this call ?
The referenced script uses both. I am more curious of the justification of using variables.
Reference
Sample Service Fabric Azure Deploy Script
https://raw.githubusercontent.com/azure/azure-quickstart-templates/master/service-fabric-oms/azuredeploy.json
In the Azure template json file:
parameters:Values that are provided when deployment is executed to customize resource deployment.
variables:Values that are used as JSON fragments in the template to simplify template language expressions.
More information please refer to this official document:Understand the structure and syntax of Azure Resource Manager templates.
I am more curious of the justification of using variables.
Based on my experience, if you only use the variable once, you don't need use variables. But if you want to use the variable multiple times, you had better use variables. Using variable can simplify your template to avoid duplication of content.
For example, if you don't use supportLogStorageAccountName more than once, you can just do:
"name": "[toLower(concat('sf', uniqueString(resourceGroup().id),'2'))]"
However if you use provide variable supportLogStorageAccountName several\many times, you can use variable to avoid duplication.
ARM templates are usually used to create a set of close to identical environments. The parameters are what differs them. This is commonly used for environment type (prod, dev, test) and performance/cost related parameters.
Variables are used to create unique names for services based on or calculated from the parameters.
An example of this would the name of a storage account. This is usually done by concatenating a common name like _storage and an environment name parameter like “test” and store it in a variable. When you create another environment all you must do is change the environment type parameter.
I can't find out how to access variables in a build-script provided by the gitlab-ci.yml-file.
I have tried to declare variables in two ways:
Private Variables in the Web-Interface of GitLab CI
Variable overrides/apennding in config.toml
I try to access them in my gitlab-ci.yml-files commands like that:
msbuild ci.msbuild [...] /p:Configuration=Release;NuGetOutputDir="$PACKAGE_SOURCE"
where $PACKAGE_SOURCE is the desired variable (PACKAGE_SOURCE) but it does not work (it does not seem to be replaced). Executing the same command manually works just as expected (replacing the variable name with its content)
Is there some other syntax required i am not aware of?
I have tried:
$PACKAGE_SOURCE
$(PACKAGE_SOURCE)
${PACKAGE_SOURCE}
PS: Verifying the runner raises no issues, if this matters.
I presume you are using Windows for your runner? I was having the same issue myself and couldn't even get the following to work:
script:
- echo $MySecret
However, reading the Gitlab documentation it has an entry for the syntax of environment variables in job scripts:
To access environment variables, use the syntax for your Runner’s shell
Which makes sense as most of the examples given are for bash runners. For my windows runner, it uses %variable%.
I changed my script to the following, which worked for me. (Confirmed by watching the build output.)
script:
- echo %MySecret%
If you're using the powershell for your runner, the syntax would be $env:MySecret
In addition to what was said in the answer marked as correct above, you should also check whether your CI variables in the gitlab settings are set as "protected". If so, you may not be able to use them in a branch that's not protected.
"You can protect a project, group or instance CI/CD variable so it is only passed to pipelines running on protected branches or protected tags." -> check it https://docs.gitlab.com/ee/ci/variables/index.html#protect-a-cicd-variable
Be aware that.. in certain cases SOME of the variables Gitlab CI CD offer are not always available..
In my case I wanted to use ${CI_COMMIT_BRANCH} but if you read the doc
https://docs.gitlab.com/ee/ci/variables/predefined_variables.html
The commit branch name. Available in branch pipelines, including pipelines for the default branch. Not available in merge request pipelines or tag pipelines.