CI/CD pipeline - get list of changed files

Hi folks,

On my Gitlab CI/CD pipeline, is there a way I can get a list of the changed files?

Basically, I’ve got some linting set up - but the repository contains a number of separate files which don’t work together to form an application, they’re just individual files hosted in the same place.

I’d like to lint only files changed on commit/push. Is this possible?

Many thanks,
Duncan

1 Like

I have the same question! From what I can tell (below), we’ll have to use the git command: git diff-tree --no-commit-id --name-only -r <commit hash> in our CI scripts to obtain the list of changed files, one per line, and then iterate over that list.

How to get a list of changed files in a commit (GitLab Forum)
How to list all the files in a commit? (Stack Overflow).

2 Likes

Thanks for this @quilty. This worked nicely.

files=(git diff-tree --no-commit-id --name-only -r $CI_COMMIT_SHA)

Can then use a standard Bash for loop to iterate through the files.

1 Like

If there are multiple commits in a merge request, how to get all changed files?

1 Like
git diff

Can work with 2 commits SHAs

Reference:

In case the pipeline runs on merge requests, one option it is to compare it with the target branch.
git diff-tree --name-only --no-commit-id $CI_MERGE_REQUEST_TARGET_BRANCH_SHA

I am running the below command in my pipeline and it throws an error that
git command not found

command:
git diff-tree --no-commit-id --name-only -r

I am trying to capture list of changed files in my pipeline , if there is another way