With the changes
keyword, you can watch for changes to certain files or folders for a job to execute. GitLab uses the output from git’s diffstat to determine files that have changed and match them against the array of files provided for the changes
rule. A use case is an infrastructure project that houses resource files for different components of an infrastructure, and you want to execute a terraform
plan when changes are made to the terraform files.
job: script: - terraform plan rules: - if: $CI_PIPELINE_SOURCE == "merge_request_event" changes: - terraform/**/*.tf
In this example, the terraform
plan is executed only when files with the .tf
extension are changed in the terraform
folder and its subdirectories. An additional rule ensures the job is executed for merge request pipelines.
The changes
rule, as shown below, can look for changes in specific files with paths
:
job: script: - terraform plan rules: - if: $CI_PIPELINE_SOURCE == "merge_request_event" changes: paths: - terraform/main.tf
Changes to files in a source reference (branch, tag, commit) can also be compared against other references in the Git repository. The CI/CD job will only execute when the source reference differs from the specified reference value defined in rules:changes:compare_to. This value can be a Git commit SHA, a tag, or a branch name. The following example compares the source reference to the current production branch, refs/head/production
.
job: script: - terraform plan rules: - if: $CI_PIPELINE_SOURCE == "merge_request_event" changes: paths: - terraform/main.tf compare_to: 'refs/head/production'