I use Gitlab CI for checking a code quality of my project. Sometime I want to check only new code ( new commit ).
How can I get diff from Gitlab CI ?
How can I get the commit diff in Gitlab CI?
A diff from what to what, exactly? A CI pipeline need not be for a Merge Request. –
Yaelyager
@JonathonReinhart It would be just commit from local:master to origin:master, I want get diff from last commit and previous –
Rockaway
I found out solution, it was obvious, but not for me
diff:
script:
- git diff ${CI_COMMIT_SHA} master
except:
- master
Can we replace master by the branch for which the MR is created? –
Clemente
You can. Set "only: - merge_requests" setting for the job. Variables can be found here docs.gitlab.com/ee/ci/variables/predefined_variables.html .They start with prefix CI_MERGE_REQUEST. –
Laurettelauri
I did something similar but for long lasting builds in a bash script with this approach
git diff --quiet $LAST_COMMIT_HASH HEAD path/of/interest1/ # optionally add more paths like file/of/interest2.txt
if [[ $? -ne 0 ]]; then
echo rechecking code quality
# ...
fi
$LAST_COMMIT_HASH could be HEAD~1 for simplest (stupid) use case
or you could try to read it out fron an artifacted/cached file like "last_code_check_commit_hash.txt" that you generate during your code quality check job. Sth. like
git rev-parse HEAD > last_code_check_commit_hash.txt
© 2022 - 2025 — McMap. All rights reserved.