CI/CD pipeline - get list of changed files

$CI_MERGE_REQUEST_DIFF_BASE_SHA is on available in MRs. You can’t use that logic in pipelines running on the default branch.

Just wanted to share what worked for me. In my case I always wanted the diff against the tip of origin/main.

    - LATEST_COMMIT=$( git ls-remote
      HEAD | awk '{ print $1}' )
    - CHANGES=$(git diff-tree --no-commit-id --name-only -r $LATEST_COMMIT -r $CI_COMMIT_SHA);
1 Like

Same thing.

Have you solved it?

If you have set up a shallow clone and/or use GIT_DEPTH: x you might get an unknown revision error because the commit you want to diff against, is not available.

But there’s a workaround that doesn’t require turning off the shallow clone or fetching the whole git repository (which might take minutes with large repos), so it is pretty fast and you can even set the initial clone depth to 1.

    GIT_DEPTH: 1
    # Fetching only up to the closest merge-base to save time
    - git fetch --depth 1 origin $CI_MERGE_REQUEST_DIFF_BASE_SHA
    # Diff only changed files within MR
    - CHANGES=$(git diff-tree --name-only -r $CI_MERGE_REQUEST_DIFF_BASE_SHA $CI_COMMIT_SHA)