Search code examples
gitshelljenkinsgerrit

Get change id from the commit i just pushed in a Jenkins job?


I need to get the change-id from gerrit from a commit i push in the same job with the following code

git status
git add -A
git commit -m "Jenkins passed job commit"
git push origin HEAD:refs/for/branch_name/Topic_name

The reason is that i need to provide it also a review +1 label and i suppose for the ssh api call i need the change number. Other solutions where it will set the review label for the specific commit are also welcome!

Thanks!


Solution

  • When you execute the "git push" command you receive something like the following output:

    remote: Processing changes: refs: 1, new: 1, done            
    remote: 
    remote: SUCCESS        
    remote: 
    remote:   https://GERRIT-SERVER/c/sandbox/helloworld/+/72712 Do a generic change [NEW]        
    remote: 
    To https://GERRIT-SERVER/a/sandbox/helloworld
     * [new branch]      HEAD -> refs/for/master
    

    You can redirect this output to a file executing:

    git push origin HEAD:refs/for/master 2>&1 | tee output.log
    

    Then you can get the Change-Id with the following command:

    if grep -q "SUCCESS" output.log
    then
        changeid=$(grep "remote:" output.log | grep "GERRIT-SERVER" | cut -d "+" -f 2 | cut -d "/" -f 2 | cut -d " " -f 1)
    fi