Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Global environment variables for gitlab CI runner

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?

like image 877
TristanV Avatar asked May 24 '17 21:05

TristanV


2 Answers

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 -
like image 83
domson Avatar answered Sep 17 '22 15:09

domson


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.

like image 25
VonC Avatar answered Sep 17 '22 15:09

VonC