0

I am using jenkins, so iam trying to execute below code in 'execute shell'

Below is the script:

ssh user@prod-server
cd /myfolder
pwd

if [ ! -d myproj ]; then
  git clone http://prod-server/bbb/myproj.git
else
  cd myproj
  pwd
  git pull
fi

In 'execute shell', iam trying as below but getting syntax errors while trying to build:

ssh prod-server  'cd /myfolder && pwd && if [ ! -d myproj ]; then git clone http://prod-server/bbb/myproj.git else cd myproj  pwd  git pull  fi'

Here is the syntax error:

Syntax error: end of file unexpected (expecting "fi")

So, please tell me what to modify?

user3815806
  • 243
  • 5
  • 15

1 Answers1

2

The syntax errors are to be expected, as the single commands in the else tree are not really separated. How should your shell (bash I presume) know where a command starts or ends? Try the following:

ssh prod-server  'cd /myfolder && pwd && if [ ! -d myproj ]; then git clone http://prod-server/bbb/myproj.git else cd myproj; pwd; git pull; fi'

Alternatively, you can also separate them by && instead of ; and thus execute the next command only when the former one succeeded.

Ralf
  • 116
  • 2
  • Answer by Ralf is correct (so I won't make another competing answer). As an alternative, you can also consider a "here doc" syntax, described in second answer to this question: http://stackoverflow.com/questions/305035/how-to-use-ssh-to-run-shell-script-on-a-remote-machine?lq=1 – Slav Apr 15 '15 at 13:21