Monday, July 11, 2022

[FIXED] How to modify the git -commit messages using jenkins?

Issue

I'm using a Jenkins server to check build & merge on master. Now I want to implement it so that it modifies the commit messages with a custom string. Concrete example : let's say I made 3 commits on a branch ;

commit #3
commit #2
commit #1

What I want to do is change these messages so that they look like

ISSUE-XX commit #3
ISSUE-XX commit #2
ISSUE-XX commit #1

where XX will be completed by the user triggering the jenkins build. Is there any way to do this? I haven't been able to find an answer online so far.

I already thought about implementing git-hooks but that's not exactly what I want and I also thought about automatic rebasing but I don't quite understand it.


Solution

Solved with squashing, using the script below. The solution squashes the last (NUMBER_OF_COMMITS) into a single one given as a Build Parameter and sets a custom commit message, also given as a build parameter.

    ::Rebasing to modify commit messages

    :: Reset the current branch to the commit just before the last 12:
    git reset --hard HEAD~%NUMBER_OF_COMMITS%

    :: HEAD@{1} is where the branch was just before the previous command.
    :: This command sets the state of the index to be as it would just
    :: after a merge from that commit:
    git merge --squash HEAD@{1}

    :: Commit those squashed changes.
    git commit -m "%ISSUE%"


Answered By - Petrut Jianu
Answer Checked By - David Marino (PHPFixing Volunteer)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.