Home > OS >  Fetch a specific string from Git commit message in Jenkins
Fetch a specific string from Git commit message in Jenkins

Time:03-18

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

CodePudding user response:

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

CodePudding user response:

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}"
    }
  }
}
  • Related