- 1 year ago
How to use last commit message in Jenkins Pipeline
In a Jenkins Pipeline, you can access the last commit message using the GIT_COMMIT
environment variable. Here's an example of how you can use the last commit message in a Jenkins Pipeline:
groovypipeline {
agent any
stages {
stage('Build') {
steps {
// Checkout source code
checkout scm
// Access last commit message
script {
def lastCommitMessage = sh(
returnStdout: true,
script: 'git log -1 --pretty=format:"%s" $GIT_COMMIT'
).trim()
echo "Last Commit Message: ${lastCommitMessage}"
// Use the last commit message for further processing
// ...
}
}
}
}
}
In the above example, the Pipeline script includes a Build
stage. Within the Build
stage, the source code is checked out using the checkout scm
step. The last commit message is then obtained by executing a shell command (git log
) inside a Jenkins script
block. The GIT_COMMIT
environment variable provides the commit hash of the current build, which is used to retrieve the last commit message.
The last commit message is stored in the lastCommitMessage
variable and can be used for further processing within the Pipeline. In the example, it is echoed to the Jenkins console, but you can customize its usage as per your requirements.
Note that to execute shell commands, you need to ensure that the Jenkins agent has Git installed and properly configured.