Search code examples
continuous-integrationgitlabyamlgitlab-cigitlab-pipelines

GitLab CI: avoid duplication of skip-ci for each job


Currently, I'm duplicating the information about skip-ci in every single job, like so

job1:
  except:
    variables:
      - $CI_COMMIT_MESSAGE =~ /skip-ci/
    ...
job2:
  except:
    variables:
      - $CI_COMMIT_MESSAGE =~ /skip-ci/
    ...
job3:
  except:
    variables:
      - $CI_COMMIT_MESSAGE =~ /skip-ci/
    ...

Is there any way to write it only once and it'd apply for all the jobs?


Solution

  • There are two ways to do this in GitLab:

    Job Inheritance

    This is the recommended approach, since it's more readable than YAML anchors and you can extend from multiple jobs if you need to. In the following example, the period in front of the job name causes GitLab to hide the job so the template job doesn't get executed on its own.

    .skip-ci:
      except:
        variables:
          - $CI_COMMIT_MESSAGE =~ /skip-ci/
    
    job1:
      extends: .skip-ci
        ...
    job2:
      extends: .skip-ci
        ...
    job3:
      extends: .skip-ci
        ...
    

    YAML Anchors

    I've included this approach for completeness, but generally it's almost always better to use extends.

    .skip-ci: &skip-ci
      except:
        variables:
          - $CI_COMMIT_MESSAGE =~ /skip-ci/
    
    job1:
      <<: *skip-ci
        ...
    job2:
      <<: *skip-ci
        ...
    job3:
      <<: *skip-ci
        ...