Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GitLab CI/CD: Run jobs only when files in a specific directory have changed

Tags:

I would like to run specific jobs on the .gitlab-ci.yaml if and only if files within specific directories of the repository have changed. Is there a way to do this with gilab's ci/cd tooling or would it be easier just to run a custom build script?

like image 762
Victor Martinez Avatar asked Aug 02 '18 19:08

Victor Martinez


People also ask

Which keyword is used to limit when the jobs should be created?

The limit keyword limits the number of jobs that can run simultaneously in a job stream. This keyword works only if all the jobs in the job stream are defined on workstations that are managed by the same batchman process.

Which keyword is used to make sure that job B runs only when Job A is completed?

By creating a duplicate cleanup task and specifying a needs tag on Job B and when: on_failure , the cleanup task will only run if Job A succeeds and Job B fails.

What is Before_script in GitLab CI?

These are scripts that you choose to be run before the job is executed or after the job is executed. These can also be defined at the top level of the YAML file (where jobs are defined) and they'll apply to all jobs in the . gitlab-ci. yml file.

Do GitLab jobs run in parallel?

GitLab provides a method to make clones of a job and run them in parallel for faster execution using the parallel: keyword. While parallel jobs may not help in reducing the consumption of CI minutes, they definitely help increase work productivity.


2 Answers

Changes policy introduced in GitLab 11.4.

For example:

docker build:
  script: docker build -t my-image:$CI_COMMIT_REF_SLUG .
  only:
    changes:
      - Dockerfile
      - docker/scripts/*
      - dockerfiles/**/*
      - more_scripts/*.{rb,py,sh}

In the scenario above, if you are pushing multiple commits to GitLab to an existing branch, GitLab creates and triggers the docker build job, provided that one of the commits contains changes to either:

  • The Dockerfile file.
  • Any of the files inside docker/scripts/ directory.
  • Any of the files and subdirectories inside the dockerfiles directory.
  • Any of the files with rb, py, sh extensions inside the more_scripts directory.

You can read more in the documentation

like image 95
kelvinji Avatar answered Sep 23 '22 11:09

kelvinji


this is now possible:

rules:
    - changes:
      - package.json
      - yarn.lock
    - exists: 
      - node_modules
      when: never
    - when: always

You can either check for file changes or check existence. Rules Documentation.

like image 39
Pezhvak Avatar answered Sep 22 '22 11:09

Pezhvak