Skip to main content

How do I use the when: changed section in a pipeline as code?

To use the when: changed section in a GitLab pipeline as code, you can specify a job or a step to run only when there are changes detected in specific files or directories. This is useful when you want to optimize your pipeline by running certain jobs only when relevant changes occur.

Here's an example of how you can use the when: changed section:

lint:
stage: test
script:
- echo "Running linter..."
only:
changes:
- '*.js'
when: changed

In the above example:

The lint job is defined in the test stage of the pipeline.

The script section includes the commands for running a linter.

The only section specifies the file patterns to monitor for changes. In this case, it is set to *.js to trigger the job when JavaScript files are modified.

The when: changed section indicates that the job should run only when there are changes detected in the specified files.

When this pipeline is executed, the lint job will run only if there are changes in JavaScript files (*.js). If there are no changes in the specified files, the job will be skipped.

Using the when: changed section helps optimize your pipeline by selectively running jobs based on relevant changes, reducing unnecessary execution and saving time.

If you have further questions or need assistance with implementing the when: changed section or optimizing your pipelines, feel free to reach out to Cloud-Runner. We specialize in helping developers maximize the efficiency and performance of their CI/CD workflows. Visit cloud-runner.com to learn more about our services and how we can support your pipeline automation needs.