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.
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.
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
CI_COMMIT_BEFORE_SHA
doesn't seem to be usefully--or tightly--specified enough for this to work all the time. For example, the original question is about raising when a PR/MR is raised, so what you really need to know are the files changed within the MR, not just since the previous commit.
Apr 24, 2019 at 23:04
CI_COMMIT_BEFORE_SHA
is fine as it is defined as: "The previous latest commit present on a branch before a push request."
Jun 13, 2019 at 10:34
CI_COMMIT_BEFORE_SHA
is deceiving: let's say you just submitted version 10 of a series after a long review. Version 10 has only one file changed compared to version 9. In that case CI_COMMIT_BEFORE_SHA points at version 9 and is basically useless (Tested with gitlab 11.10.2)
docker/scripts/*
include docker/scripts/foo/bar
? i.e. do i need **/*
to specify all folders recursively?
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>
fatal: ambiguous argument 'origin/master': unknown revision or path not in the working tree.
Any idea how to fix this? Apparently the runner doesnt recognize what that branch path is. Works fine when I run it localy
May 12, 2020 at 8:30
git fetch origin
and then git branch
. Make sure you have master
branch in origin
.
May 12, 2020 at 9:57
git diff --name-only origin/$CI_MERGE_REQUEST_TARGET_BRANCH_NAME
(but obviously i replace the gitlab CI stuff with a string master
) gives the list of files. When i run this on gitlab's pipeline, it simply says as i wrote above. So inside the CI the term origin/master
is not recognized, as if the CI doesnt know what branches it has and doesnt know what origin/master
means
May 12, 2020 at 10:35
fatal: ambiguous argument 'origin/master': unknown revision or path not in the working tree.
might also be that you use git shallow clone. This is the default since Gitlab 12. Use variables: GIT_DEPTH: 0
to fix it. See docs.gitlab.com/ee/ci/pipelines/settings.html#git-shallow-clone for more information.