Skip to content

Make environment variables and pre/post scripts of runners globally configurable

  • Please check this box if this contribution uses AI-generated content (including content generated by GitLab Duo features) as outlined in the GitLab DCO & CLA

What does this MR do?

Make environment and pre/post scripts for runners globally configurable

Add setting to config.toml where one can define pre and post get-sources scripts, pre and post build scripts, as well as environment variables that a runner uses if it doesn't set those itself. Environment variables are merged, where an equally named variable in the runner config overwrites the global value. Allow users to define these settings once per runner host and have them be applied to all runners registered on the host.

Why was this MR needed?

This is for pre- and post-get-sources or build actions that every job on a runner's host might need to do or environment variables that all runner jobs might need to be set. Copy-Pasting these actions and variables to every [[runners]] config would make for duplication. Examples:

  • importing certificates
  • logging custom job metadata
  • cleanups
  • HTTP proxy environment variables

What's the best way to test this MR?

Steps

  1. Register two runners, one with tags runner-tag-1, the other with runner-tag-2, respectively
  2. edit the config.toml and add this toml-table:
...
[global_runner_config]
  environment = ["SCOPE=global", "GLOBAL_VAR=foo"]
  pre_get_sources_script = "echo running pre_get_sources_script from global runner config, with env of scope $SCOPE"
  post_get_sources_script = "echo running post_get_sources_script from global runner config, with env of scope $SCOPE"
  pre_build_script = "echo running pre_build_script from global runner config, with env of scope $SCOPE"
  post_build_script = "echo running post_build_script from global runner config, with env of scope $SCOPE"

[[runners]]
  name = "runner using global config"
  ...

[[runners]]
  name = "runner using runners config"
  ...
  environment = ["SCOPE=runners", "RUNNER_VAR=bar"]
  pre_get_sources_script = "echo running pre_get_sources_script from runner config, with env of scope $SCOPE"
  post_get_sources_script = "echo running post_get_sources_script from runner config, with env of scope $SCOPE"
  pre_build_script = "echo running pre_build_script from runner config, with env of scope $SCOPE"
  post_build_script = "echo running post_build_script from runner config, with env of scope $SCOPE"
...
  1. create .gitlab-ci.yml:
stages:
- stage

job_1:
  stage: stage
  tags:
  - runner-tag-1
  script: 
  - echo runner config using SCOPE=$SCOPE with defined environment variable GLOBAL_VAR=$GLOBAL_VAR and undefined variable RUNNER_VAR=$RUNNER_VAR 

job_2:
  stage: stage
  tags:
  - runner-tag-2
  script: 
  - echo runner config using SCOPE=$SCOPE with defined environment variable GLOBAL_VAR=$GLOBAL_VAR and defined variable RUNNER_VAR=$RUNNER_VAR

What are the relevant issue numbers?

#37399

Edited by Kirill Sybin

Merge request reports