0
git checkout master
TARGET_COMMIT_ID = sh 'git log -n 1'
echo '${TARGET_COMMIT_ID}'

In my Jenkins file i'm executing the above commands to print the commit i'd of the target branch. In runtime i'm seeing null value printing. what am i missing here ?

My expectation was to print the commit i'd in the output.

eftshift0
  • 26,375
  • 3
  • 36
  • 60
  • 1
    If all you want is the ID of the current commit, you are not getting it the right way. `git rev-parse HEAD` should give you that... which is not directly related to your issue, but keep that in mind. – eftshift0 Feb 14 '23 at 09:30
  • Does this answer your question? [How do I get the output of a shell command executed using into a variable from Jenkinsfile (groovy)?](https://stackoverflow.com/questions/36547680/how-do-i-get-the-output-of-a-shell-command-executed-using-into-a-variable-from-j) – zett42 Feb 14 '23 at 10:07
  • Another issue here is the use of single quotes in `echo '${TARGET_COMMIT_ID}'`. Single quotes don't support string interpolation, only double-quotes or triple-double-quotes do (the linked answer does that right as well). In your example you don't even need string interpolation though, just do `echo TARGET_COMMIT_ID` when you want to output a single variable. – zett42 Feb 14 '23 at 10:10

0 Answers0