How to Run Specific Jobs in .gitlab-ci.yml Based on Which Directories Were Changed
Tags: gitlab, ci, gitlabci, gitlabciyml
Published: 2023-08-14 16:03:37

How to Run Specific Jobs in .gitlab-ci.yml Based on Which Directories Were Changed
----------------------------------------------------------------------------------

**Take into account comment from bcouetil (https://dev.to/bcouetil):
**
**Thanks for sharing... but you are doing it wrong my dear bot 🤖.
rules:changes in branch pipeline will only see the current commit. So if you break your pipeline in a commit, but do not change the same file/folder in the next commit, the failing job will not trigger again, your new pipeline will be green, even if you did not fix anything !
You need to do it on merge request pipeline.**

As a software developer, you may find yourself working on a project with multiple directories and files. When it comes to running continuous integration and deployment pipelines, it can be inefficient to run all jobs for every commit. Luckily, GitLab provides a way to run specific jobs in your `.gitlab-ci.yml` file based on which directories were changed. This feature allows you to optimize your CI/CD process and save valuable time.

To utilize this feature, you need to define rules in your `.gitlab-ci.yml` file that specify which jobs should be executed based on the changes in directories. Let's dive into the steps to accomplish this.

### Step 1: Define Job Names

First, you need to define the names of your jobs in the `.gitlab-ci.yml` file. These job names will be used in the rules section to determine which jobs to run. For example, let's say you have two jobs named `build` and `test`:

 

    
      jobs:
        - build
        - test
      

 

### Step 2: Define Rules

Next, you need to define rules for each job based on the directories that should trigger them. The rules section allows you to specify conditions using regular expressions. For example, if you want the `build` job to run only when changes occur in the `src` directory, you can define the following rule:

 

    
      build:
        script:
          - echo "Building..."
        rules:
          - changes:
              - src/*
      

 

Similarly, you can define rules for the `test` job based on the `tests` directory:

 

    
      test:
        script:
          - echo "Running tests..."
        rules:
          - changes:
              - tests/*
      

 

### Step 3: Enjoy the Efficiency

Now, whenever you make changes to your project, GitLab will automatically run the specific jobs based on the directories that were modified. This means that if you only modify files in the `src` directory, only the `build` job will be triggered. Similarly, if you only modify files in the `tests` directory, only the `test` job will run. This optimization can greatly speed up your CI/CD process.

So, instead of running all jobs for every commit, you can now focus on the specific jobs that are affected by the changes. This not only saves time but also reduces unnecessary resource consumption.

### Conclusion

GitLab's ability to run specific jobs in the `.gitlab-ci.yml` file based on which directories were changed is a powerful feature that allows you to optimize your CI/CD process. By defining rules for each job, you can ensure that only the necessary jobs are executed, resulting in faster pipelines and improved efficiency.

So, the next time you find yourself working on a project with multiple directories, remember to leverage this feature and enjoy the benefits of running specific jobs based on directory changes.

### References

*   [GitLab CI/CD YAML Reference - Rules](https://docs.gitlab.com/ee/ci/yaml/#rules)
*   [GitLab CI/CD YAML Reference - Changes](https://docs.gitlab.com/ee/ci/yaml/#changes)