How to read CICD predefined variables - gitlab

I am really new to Git & GitLab. My aim is to create few CICD jobs which should run when a commit happens on a particular branch. I was going through few topics & videos for the last 2 weeks and come up with yaml file which actually runs now and build some files. My current requirement is to get few parameters like the commited branch name, commited files and then download these files to a remote system. I have gone through the CICD variables and tried to echo these variables to see the values.
echo $CI_COMMIT_REF_NAME simply echoes the value $CI_COMMIT_REF_NAME in the CICD job console.
How can we get this value? Any pointers is highly appreciated.
Thanks in advance!

From your .gitlab-ci.yml file, it looks like you're in a Windows environment and should be using a different environmental variable.
Eg, $CI_COMMIT_REF_NAME should in fact be %CI_COMMIT_REF_NAME%
bash/sh $variable
windows batch %variable%
PowerShell $env:variable
See using CI variables in your job script.

Related

Can I trigger a Gitlab CI/CD rebuild if global variable changes?

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

Unable to access any variables in gitlab CI/CD pipeline

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%

Can we detect and get specific directory changed in gitlab CI/CD?

I am a beginner to learn gitlab CI/CD and doing some projects related to it. I have a trouble when using OpenFaaS and build a large number of function.I want to use CI/CD to automatically build functions that have updates, remain functions are not, but I don't know how to get name of specific folder or directory changed in my source to build corresponding functions.
So I wonder if we have a way to get name of specific directory changed in Gitlab CI/CD? Or another tools CI/CD can do that?
only:changes is what you are looking for:
#.gitlab-ci.yml
my job:
script: echo "I am triggered only when changes to some/dir are applied"
only:
changes:
- some/dir/**/*

During a VSTS build, using an npm script, how can I determine the branch that triggered the build?

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}%).

How to access variables in gitlab-ci.yml using gitlab-ci-multi-runner on windows

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.

Resources