Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gitlab CI how to ignore directory using rules syntax?

I was able to ignore directory, file changes using the following syntax.

build:
  script: npm run build
  except:
    changes:
      - "*.md"
      - "src/**/*.ts"

With this configuration build job is going to run except git changes include only *.md extension file or *.ts files in src directory. They're ignored.

But then they deprecated this only:changes, except:changes syntax and warned to use rules syntax instead. Unfortunately, I cannot seem to find how to ignore directory, file changes using this new syntax.

like image 222
Shinebayar G Avatar asked Jul 02 '20 04:07

Shinebayar G


People also ask

How do I skip CI GitLab?

Push options for GitLab CI/CD You can use push options to skip a CI/CD pipeline, or pass CI/CD variables. Do not create a CI pipeline for the latest push. Only skips branch pipelines and not merge request pipelines. Provide CI/CD variables to be used in a CI pipeline, if one is created due to the push.

What is Ci_commit_tag?

CI_COMMIT_TAG - The commit tag name. Present only when building tags. Therefore in the variables section CI_COMMIT_TAG is not defined, hence equals to "". So if you want to use CI_COMMIT_TAG use in job where tags are defined. See https://docs.gitlab.com/ee/ci/yaml/README.html#tags.

What is used to restrict when a job is executed on your pipeline GitLab?

Run jobs for scheduled pipelines To configure a job to be executed only when the pipeline has been scheduled, use the rules keyword.


3 Answers

Based on documents of rules: changes, it seems when: never must be used with rules: changes syntax. Like the following:

build:
  script: npm run build
  rules:
    - changes:
      - "*.md"
      - "src/**/*.ts"
      when: never

    - when: always

If changed paths in the repository match above regular expressions then the job will not be added to any pipeline. In other cases the job always will be run by pipelines.

like image 119
AmirMohammad Dadkhah Avatar answered Oct 17 '22 04:10

AmirMohammad Dadkhah


Surprisingly enough, it seems that is currently not possible - neither with except/only, nor with rules. This seems to be the "best" open issue for this requested feature: https://gitlab.com/gitlab-org/gitlab/-/issues/198688

I guess you could only reverse the logic and enable the build when a relevant change has been detected (sample adapted from https://docs.gitlab.com/ee/ci/yaml/#ruleschanges ):

job:
  rules:
    - changes:
        - relevant/path/file
      when: on_success
    - changes:
        - path/to/config/file
      when: never
    - when: on_success
like image 40
Hannes Erven Avatar answered Oct 17 '22 03:10

Hannes Erven


Excuse the brief answer, but I've just had some success with using the unix glob 'not syntax', ie:

    - changes:
      - '[^d][^o][^c][^s]**/**/*'
      - '*' # above matcher does not match files in base folder

This matches changes to all files not in ./docs/ , and seems to correctly trigger the step for any changes outside that folder, but not for chages within that folder.

As a result, other matching rules within the "rules" property will still trigger the job. As a result, my whole rules statement needed to be set up to not trigger the job on any other rule:

  rules:
    - if: $CI_COMMIT_REF_NAME == $CI_DEFAULT_BRANCH     # Run for merges
      when: never
      variables:
        BUILD_ENVIRONMENT: "production"
    - if: $CI_MERGE_REQUEST_ID                          # Run for merge requests
      when: never
      variables:
        BUILD_ENVIRONMENT: "staging"
    - changes:
      - '[^d][^o][^c][^s]**/**/*'
      - '*'

This isn't well tested yet though!

edit:fixed a little, with inspiration from Jan Spets: https://gitlab.com/gitlab-org/gitlab/-/issues/32673

and the gitLab source code for the Changes feature: https://gitlab.com/gitlab-org/gitlab/-/blob/master/lib/gitlab/ci/build/rules/rule/clause/changes.rb

like image 1
Duncan Browne Avatar answered Oct 17 '22 03:10

Duncan Browne