Issue
We are using below command to find out the last commit to the git
{ git log -1 --pretty=format:'%an'; echo "@xyzcompany.com, developer@xyzcompany.com"; } | xargs -I{} echo {} | sed 's/\n//'
Note: this command is working in CLI in jenkins workspace project.
How to inject this command in jenkins pipeline script??
Solution
You can just use an sh
to execute the command. If you are using declarative syntax (starting with pipeline
instead of node
) I'd suggest to do that in the environment
, so you can read the result in all stages of your pipeline:
environment {
COMMIT = sh(script: '{ git log -1 --pretty=format:\'%an\'; echo "@xyzcompany.com, developer@xyzcompany.com"; } | xargs -I{} echo {} | sed \'s/\n//\'', returnStdout: true).trim()
}
Or –if you use scripted syntax– you just declare a variable:
def commit = sh(script: '{ git log -1 --pretty=format:\'%an\'; echo "@xyzcompany.com, developer@xyzcompany.com"; } | xargs -I{} echo {} | sed \'s/\n//\'', returnStdout: true).trim()
Answered By - Michael