Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to list the modified files?

Tags:

gitlab-ci

I am using gitlab-ci to run scripts defined in .gitlab-ci.yml whenever a PR is raised.

I want to get the list of modified files since the last commit.

The use case is to run file-specific integration tests in a large codebases.

like image 426
Gajus Avatar asked Oct 20 '18 17:10

Gajus


People also ask

How do I find recently modified files in Linux?

Finding Files Modified on a Specific Date in Linux: You can use the ls command to list files including their modification date by adding the -lt flag as shown in the example below. The flag -l is used to format the output as a log. The flag -t is used to list last modified files, newer first.


Video Answer


2 Answers

If you do not need to know the paths, but you simply need to run a specific job only when a specific file is changed, then use only/changes .gitlab-ci.yml configuration, e.g.

docker build:
  script: docker build -t my-image:$CI_COMMIT_REF_SLUG .
  only:
    changes:
      - Dockerfile
      - docker/scripts/*

Alternatively, if you need to get paths of the modified scripts, you can use gitlab-ci CI_COMMIT_BEFORE_SHA and CI_COMMIT_SHA environment variables, e.g.

> git diff --name-only $CI_COMMIT_BEFORE_SHA $CI_COMMIT_SHA
src/countries/gb/homemcr.js
src/countries/gb/kinodigital.js
like image 98
Gajus Avatar answered Sep 22 '22 08:09

Gajus


A common use case that some people will find useful. Run the job when a merge request is raised. Specifically, run lint on all the files that are changed w.r.t. target branch.

As one of the answers suggest, we can get the target branch name through CI_MERGE_REQUEST_TARGET_BRANCH_NAME variable (list of predefined variables). We can use git diff --name-only origin/$CI_MERGE_REQUEST_TARGET_BRANCH_NAME to get the list of files that were changed. Then pass them on to the linter via xargs. The example configuration may look like.

code_quality:
  only:
    - merge_requests
  script:
    - git diff --name-only origin/$CI_MERGE_REQUEST_TARGET_BRANCH_NAME | xargs <LINT_COMMAND_FOR_FILE>
like image 23
Ravi Ojha Avatar answered Sep 19 '22 08:09

Ravi Ojha