1

The following conditional works as expected when run from the bash command line on my Jenkins server:

if (($BRANCH_COUNT > 0)); then ..... 

But when running in the context of a Jenkins pipeline step (calling sh ''' .... '''), it writes an error and always evaluates to false. The error message (below) suggests maybe it's interpreting the value of $BRANCH_COUNT (in this case it is 1) as a command, which is then not found?

/var/jenkins/workspace/deploy-config-db-update@tmp/durable-baa54bb3/script.sh: 16: 
/var/jenkins/workspace/deploy-config-db-update@tmp/durable-baa54bb3/script.sh: 1: not found

Is there something different about the bash environment when run by the Jenkins sh command? Some special escaping or quoting needed?

Mike Kantor
  • 1,400
  • 4
  • 24
  • 45

2 Answers2

1

if you use sh'''...''', the pipeline script variable will not be expanded in your shell script. You need to use sh """..."""

Patrice M.
  • 4,209
  • 2
  • 27
  • 36
  • I think it's something else. Other $VARIABLEs are being expanded within the triple-single-quoted script. – Mike Kantor Jul 23 '20 at 15:02
  • 1
    Groovy (i.e. pipeline syntax) won't expand variables in (triple) single-quotes, but it will in (triple) double quotes. Of course, it you want bash to expand you variable,and not the pipeline script, then you want single quotes. – Patrice M. Jul 23 '20 at 19:50
1

Moving William Pursell's comment into an answer, because it solved the problem.

Using (()) is a bashism. Try if test "$BRANCH_COUNT" -gt 0; then ...

Mike Kantor
  • 1,400
  • 4
  • 24
  • 45