Feedback

Chat Icon

Cloud Native CI/CD with GitLab

From Commit to Production Ready

Workflows: Controlling Pipelines Execution
65%

Included Multiple Workflows

Let's take another example to show some more advanced cases and pitfalls. Let's say you decided to use workflows to control the execution of your included files. So instead of using it in the main .gitlab-ci.yml file, you decided to set the workflow rules in the included files.

This is what you may have in your main .gitlab-ci.yml file:

# .gitlab-ci.yml
include:
  - local: .includes/job-dev.yml
  - local: .includes/job-main.yml

job-0:
  stage: build
  script:
    - 'echo "$CI_COMMIT_BRANCH"'

The first file (job-dev.yml) contains the following content:

# .includes/job-dev.yml
workflow:
  rules:
    - if: '$CI_COMMIT_BRANCH == "dev"'

job-dev:
  stage: build
  script:
    - 'echo "$CI_COMMIT_BRANCH"'

And the second one (job-main.yml) contains the following content:

# .includes/job-main.yml
workflow:
  rules:
    - if: '$CI_COMMIT_BRANCH == "main"'

job-main:
  stage: build
  script:
    - 'echo "$CI_COMMIT_BRANCH"'

The expected result as described in the YAML files above is to run the job-dev job when the branch name is dev and the job-main job when the branch name is main. However, this is not what will happen. Because you included the files in the main .gitlab-ci.yml file, you included two workflows rules in the same pipeline:

include:
  # This will include the first workflow rules
  - local: .includes/job-dev.yml
  # This will override the first workflow rules
  - local: .includes/job-main.yml

Therefore, the last included workflow rules configuration (job-main.yml) will override the first one (job-dev.yml). This means that the workflow of the pipeline will be controlled by the rules in the job-main.yml file. If you are pushing from the dev branch, no pipeline will run since the rules in the job-main.yml file are not met.

If you switch the order of the included files no pipeline will run when you push from the main branch but the job-dev job will run when you push from the dev branch.

include:
  # This will include the first workflow rules
  - local: .includes/job-main.yml
  # This will override the first workflow rules

Cloud Native CI/CD with GitLab

From Commit to Production Ready

Enroll now to unlock all content and receive all future updates for free.