Search code examples
jenkinsjenkins-pipelineshgit-commitcommit-message

How to access git commit message INSIDE sh STEP in a jenkins pipeline?


I need to access commit message of current fetch from git repository, while completing some sh steps of different stages of a pipe in jenkins; but I didn't find any proper solution for it. It would be also nice if I could set the commit message as an environment variable, so that I could access it later on.

Please note that this is not a repetitive question, because none of similar questions on stackoverflow addresses exactly what I need. I need to have commit message in all of sh steps of my pipeline.


Solution

  • You can get the commit message for the fetched commit ID and set it as an environment variables as follows:

    stage('get_commit_msg') {
        steps {
            script {
                env.GIT_COMMIT_MSG = sh (script: 'git log -1 --pretty=%B ${GIT_COMMIT}', returnStdout: true).trim()
            }
        }
    }
    

    You can then use ${GIT_COMMIT_MSG} in Shell scripts in any downstream stage of your pipeline.