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.
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.
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
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With