Feedback

Chat Icon

Cloud Native CI/CD with GitLab

From Commit to Production Ready

Conditional Jobs and Pipelines
65%

Conditional Includes: Combining Conditions with Includes

As seen in previous parts of this guide, the include keyword allows you to add configurations from other files (local or remote) to your .gitlab-ci.yml file. You can also combine the include keyword with the rules keyword to include or exclude jobs based on conditions.

Let's practice with an example. We are going to create 3 files and include them based on the branch name. Here are the files we are going to create:

  • job1.yml: This file should be included only when the branch name is main.
  • job2.yml: This file should be included only when the branch name is dev.
  • job3.yml: This file should always be included.

Here are the commands to create the files:

# Create a folder to store the files
# and create the first file
mkdir -p $HOME/todo/app/.includes && \
cat <$HOME/todo/app/.includes/job1.yml
job-1:
  stage: build
  script:
    - echo "running job-1"
EOF

# Create the second file
cat <$HOME/todo/app/.includes/job2.yml
job-2:
  stage: build
  script:
    - echo "running job-2"
EOF

# Create the third file
cat <$HOME/todo/app/.includes/job3.yml
job-3:
  stage: build
  script:
    - echo "running job-3"
EOF

In the main .gitlab-ci.yml file, we can include the files based on the branch name. Run the following command to test the execution and trigger the pipeline:

Cloud Native CI/CD with GitLab

From Commit to Production Ready

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