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.
Related
I have several projects that uses a key, which changes every so often. Is there a way to trigger a rebuild of those projects when I change the key variable?
I'm trying to use the rules:changes keyword:
run:
script:
- echo $KEY
rules:
- if: $KEY
changes:
- $KEY
when: always
No, You cannot. GitLab Runners will read your .gitlab-ci.yaml scripts and execute them on demand. That means the GitLab Runner will be invoked on demand and it is not actively listening to the change of a particular variable.
But as an alternate solution, you can write a script and place it somewhere (in your local or in the runner machine or anywhere). and the script should check if any changes in the variable and if yes, trigger the pipeline remotely using GitLab CI APIs . Then you can add your script in corn job to run the script in particular interval and check for changes in variable
I'd like to access one of the gitlab CI/CD pre-defined variables, but when I try to use it, I just get the literal string of my attempt to access it. Per Gitlab's documentation, the correct syntax is "$CI_COMMIT_BRANCH", but this isn't working. I also tried creating my own variables, but the same thing is happening. Here are some screenshots of my .gitlab-ci.yml file and the results of it running:
You are using the cmd batch shell. As described in the documentation, you must use %VARNAME% instead of $VARNAME in your script: sections.
script:
- echo %CI_COMMIT_BRANCH%
All depends on the pipeline flow and the version of the runner and even it's OS.
I see that you have a Windows build so you should use POSH/Batch commands not *nix shell ones.
The commit branch name. Available in branch pipelines, including pipelines for the default branch. Not available in merge request pipelines or tag pipelines.
https://docs.gitlab.com/ee/ci/variables/predefined_variables.html
If I remember correct if it can't interpolate "a variable" i.e. if it doesn't exist in the scope of the script at execution time, it just treats it as a string and prints to screen everything after echo which is the behavior you are seeing.
I solved this by calling the variable the same way as one would in the CMD:
echo %CI_COMMIT_BRANCH%
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
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 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.