0

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

  • 1
    Have Git spill out the whole thing, then use a regular expression matcher (sed, grep, awk, whatever) to match and print only the part you want. Jenkins *may* have this built in, though; I'm no Jenkins expert. – torek Mar 17 '22 at 09:57

2 Answers2

0

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

Monish Khatri
  • 820
  • 1
  • 7
  • 24
  • Thank you. I want only those numbers from commit message 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. I am not able to save it in a variable – harshitha P Mar 17 '22 at 08:36
0
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}"
    }
  }
}
bilcy
  • 168
  • 1
  • 12