Unable to access any variables in gitlab CI/CD pipeline - gitlab

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%

Related

How to pass dynamic (user info) parameters as variable to azure pipelines?

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

Gitlab CI/CD variable substitution in my appSettings.json

I'm using Gitlab CI/CD to deploy my .netcore application to AWS Beanstalk.
How can I update the appSettings.json values in my .netcore application when deploying to different environments using variables defined in my CI/CD pipeline settings?
Azure DevOps has a JSON variable substitution feature which I really liked.
GitHub Actions can also hook into this feature
How can I achieve this with Gitlab CI/CD?
I want to use this method of deployment because
I won't have to store sensitive production config values in my repository. Values will be updated by Masked variables setup in the CI/CD Pipeline.
I don't have to rebuild my artefacts every time I deploy to a new environment.
If this can't be done in Gitlabs, whats the recommended best practice?
thanks
I do something similar here with gitlab and the solution was to build a shell script that replaces some string from variable values before starting the deploy job
something like this
script:
- sed -i 's/STRING_TO_REPLACE/$GITLAB_VARIABLE/g' file.json
Just pay attention to escape json quotes correctly to make this possible
I use the same workaround. I filed an issue about this months ago:
https://gitlab.com/gitlab-org/gitlab-foss/-/issues/78486
and I recently talked about this at the gitlab forum:
https://forum.gitlab.com/t/use-variables-per-branch-in-gitlab/43685
unfortunately there does not seem to be a nice/clean solution at the moment.
so I use the workarround:
1: use environment scopes for variables so the same variable can have different values (for test/prod environments)
2: define the environments and branches in the jobs in gitlab-ci.yml
3: create a bunch of sed one liners to do a search/replace..
call it ugly, call it low level but I searched for a nice/clean/gitlab native solution an did not find it.

How to read CICD predefined variables

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.

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