Search code examples
gitlab-cigitlab-autodevops

Set variables/config that apply for all jobs in the CI config


How can I set variables or config that will apply for all jobs/steps in the pipeline ? I am using Auto Devops.

Currently, I am using as below but I’d like to apply the re-try config to all jobs in the pipeline instead of specifying it for each job as below. Is there a way to do this ?

build:
  retry:
    max: 2
    when:
      - runner_system_failure
      - stuck_or_timeout_failure
      - scheduler_failure
      - stale_schedule
      - unknown_failure

review:
  retry:
    max: 2
    when:
      - runner_system_failure
      - stuck_or_timeout_failure
      - scheduler_failure
      - stale_schedule
      - unknown_failure

Solution

  • if you want to use a variable, you can apply it at the base level of the yaml, and it will apply to all the jobs within that CI/CD. If you want to apply something more generic (like the retry in your example), use a yml anchor with the "extends" keyword. Here is an example of both things:

    variables:
      EVERY_JOB_VARIABLE: "hello world"
    
    .anchor_retry:
      retry:
        max: 2
        when:
          - runner_system_failure
          - stuck_or_timeout_failure
          - scheduler_failure
          - stale_schedule
          - unknown_failure
    
    build:
      extends: .anchor_retry
      script:
        - echo $EVERY_JOB_VARIABLE # prints "hello world"
    
    review:
      extends: .anchor_retry
      script:
        - echo $EVERY_JOB_VARIABLE # prints "hello world"