How can I get the commit diff in Gitlab CI?
Asked Answered
R

2

11

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 ?

Rockaway answered 4/6, 2018 at 13:5 Comment(2)
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 previousRockaway
R
10

I found out solution, it was obvious, but not for me

 diff:
   script:
     - git diff ${CI_COMMIT_SHA} master
   except:
     - master
Rockaway answered 6/6, 2018 at 7:46 Comment(2)
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
F
0

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
Frag answered 12/8, 2024 at 14:43 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.