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

3

21

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:

  1. Private Variables in the Web-Interface of GitLab CI
  2. 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.

Soinski answered 22/7, 2015 at 11:6 Comment(0)
F
38

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

Ferrotype answered 24/7, 2015 at 23:36 Comment(3)
This seems very legit. I'll try this next week when im back at work. Thank you in advance!Soinski
Confirming it's working. Also, if you're using artifacts:name setting, its variable interpolation is ALSO platform dependent.Hoeve
I had defined env vars outside the scope of the job in which i was accessing them.Wrongdoer
G
1

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

Graecoroman answered 13/4, 2022 at 17:12 Comment(0)
G
0

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.

Galvanic answered 9/9, 2022 at 13:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.