TD092854
TD092854

Reputation: 111

Is it possible to conditionally include external yaml in gitlab-ci.yml?

We have a central repository holding all of our CI configuration. Each project we have includes the neccessary files from that central repository. The conditions for running the jobs are present in the external files and make sure the pipeline only runs for specific branches.

Now we have a situation where a few developers are working on a project, but don't have access to the central repository. Whenever they commit Gitlab tries to setup a pipeline but fails because the developers don't have access.

Is there a way to add some logic to the gitlab-ci.yml that executes before including the files from the central repository? And as such effectively stop the pipeline from being created and include the external files.

Upvotes: 6

Views: 7722

Answers (1)

Adiii
Adiii

Reputation: 60114

This feature is live and you can now use conditional include

You can use rules with include to conditionally include other configuration files.

include:
  - local: builds.yml
    rules:
      - if: '$INCLUDE_BUILDS == "true"'
  - local: deploys.yml
    rules:
      - if: $CI_COMMIT_BRANCH == "main"

https://docs.gitlab.com/ee/ci/yaml/includes.html#use-rules-with-include

My use case was, if Canary deployment is enable through variable, then perform canary deployment, otherwise base deployment

include:
  - project: 'path/devops/gitlab-pipeline'
    ref: master
    file: '/modules/deploy/base.yml'
    rules:
      - if: '$CANARY_DEPLOY == "false"'
  - project: 'path/devops/gitlab-pipeline'
    ref: master
    file: '/modules/deploy/canary.yml'
    rules:
      - if: '$CANARY_DEPLOY == "true"'

Addition to your requirements,

Whenever they commit Gitlab tries to set up a pipeline but fails because the developers don't have access.

Not sure if this works in the above case, but working fine in my case for conditional include.

Upvotes: 7

Related Questions