'Fetch a specific string from Git commit message in Jenkins

I am fetching git commit message from my Jenkin freestyle project by cloning the git repo, by using the below command.

git log -1 --pretty=%B ${GIT_COMMIT}

I am able to get Git commit message using this, but I want to fetch only the specific message from that commit line . For example: [pqr9022827] ABAP Unit -> is the commit message I am obtaining, I need to know how to obtain only those string within the square brackets i.e., pqr9022827 and store it in some environment variable



Solution 1:[1]

You can get all the logs according to your commit message git log --all --grep='<your commit message>'

Solution 2:[2]

stage ("Git Log") {
  steps{
    script {

      GIT_LOG = sh (
       script: "git log -1 --pretty=%B ${GIT_COMMIT}",
       returnStdout: true
      ).trim()

      EXTRACTED_GIT_LOG = sh (
       script: "echo ${GIT_LOG} | cut -d '[' -f2 | cut -d ']' -f1",
       returnStdout: true
      ).trim()

      echo "${EXTRACTED_GIT_LOG}"
    }
  }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Monish Khatri
Solution 2 bilcy