11

I am working to set up a gitlab runner for multiple projects, and we want to be able to set up environment variables for all of the projects. I tried to set global variables in the .bashrc for both the gitlab-runner and root users but it did not recognize them during the CI script. What is the correct location to declare global environment variables?

TristanV
  • 111
  • 1
  • 1
  • 4
  • Same issue: Runners run on different machines, which are slightly different. (Let's say some path) I want to use different environment variables for each runners. – betontalpfa Apr 22 '20 at 09:37

7 Answers7

3

You can also inject environment variables to your gitlab-runner directly in the commandline, as the gitlab-runner exec docker --help states:

OPTIONS: .. --env value Custom environment variables injected to build environment [$RUNNER_ENV] ..

Here is a small example how I use it in a script:

Change the declarations as needed:

declare jobname="your_jobname"
declare runnerdir="/path/to/your/repository"

Get the env file into a bash array.

[ -f "$runnerdir/env" ] \
&& declare -a envlines=($(cat "$runnerdir/env"))
declare -a envs=()
for env in "${envlines[@]}"; do
  envs+=(--env "$env")
done

And finally pass it to the gitlab-runner.

[ -d "$runnerdir" ] && cd "$runnerdir" \
&& gitlab-runner exec docker "${envs[@]}" $jobname \
&& cd -
domson
  • 205
  • 2
  • 8
2

You can define environment variables to inject in the runner's config.toml file. See the advanced runner configuration documentation in the [[runners]] section.

There doesn't seem to be a way to specify environment variables in the GitLab UI just for a specific runner.

DarkDust
  • 90,870
  • 19
  • 190
  • 224
1

With GitLab 13.1 (June 2020), you now have:

Instance-level CI/CD variables

GitLab now supports instance-level variables.
With this ability to set global variables, you no longer need to manually enter the same credentials repeatedly for all your projects.

This MVC introduces access to this feature by API, and the next iteration of this feature will provide the ability to configure instance-level variables directly in the UI.

See Documentation and issue.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
0

Consider using an external persistent secret storage service like Vault or Keywhiz

Disclaimer: I am not associated nor used any of the above services

elingerojo
  • 354
  • 2
  • 6
0

I have added export MY_VAR="FOO" to gitlab-runner's .bashrc, and it works.

echo export MY_VAR=\"FOO\" >> /home/gitlab-runner/.bashrc

Check which type of executor do you use? (shell, kubernetes, docker-ssh, parallels...) I use shell executor.

Check what type of shell does gitlab-runner use? (How to determine the current shell I'm working on) And edit the proper rc file for that.

Check the Gitlab CI Runner user.


I suggest dump all environment variables for further debugging, by add env to the .gitlab-ci.yml's script:

#.gitlab-ci.yml

job:
  script: env
betontalpfa
  • 3,454
  • 1
  • 33
  • 65
0

Making changes in ~/.bash_profile NOT ~/.bashrc.

See my answer

Kevin
  • 781
  • 1
  • 9
  • 18
-2

You can easily setup Variables in the GitLab Settings:

Project-level variables can be added by going to your project's Settings > CI/CD, then finding the section called Variables.

To make sure your variables are only used in

See: https://docs.gitlab.com/ee/ci/variables/#variables

Michael Schober
  • 361
  • 1
  • 5
  • 9