finspin
finspin

Reputation: 4071

Using wildcard with symlink command creates wrong links when run from Jenkins

I can't wrap my head around this issue. I run symlink command over SSH from my Jenkins job. In my Jenkins pipeline I have these 2 steps (among others).

sh "ssh ubuntu@${host} sudo ls -al /etc/nginx/sites-available"
sh "ssh ubuntu@${host} sudo ln -sv /etc/nginx/sites-available/* /etc/nginx/sites-enabled/ -f"

Here's the relevant part of the log:

[Pipeline] sh
[my_job] Running shell script
+ ssh [email protected] sudo ls -al /etc/nginx/sites-available
total 12
drwxr-xr-x 2 root   root   4096 Sep 18 17:27 .
drwxr-xr-x 6 root   root   4096 Aug 30 12:27 ..
-rw-r--r-- 1 ubuntu ubuntu  467 Sep 18 17:27 my-nginx-config
[Pipeline] sh
[my_job] Running shell script
+ ssh [email protected] sudo ln -sv /etc/nginx/sites-available/default /etc/nginx/sites-available/jenkins /etc/nginx/sites-enabled/ -f
'/etc/nginx/sites-enabled/default' -> '/etc/nginx/sites-available/default'
'/etc/nginx/sites-enabled/jenkins' -> '/etc/nginx/sites-available/jenkins'

Symlinks are created on my remote host but instead of the my-nginx-config symlink, the default and jenkins files (which are on my jenkins host at /etc/nginx/sites-available) are symlinked.

If I don't use wildcard and run this, it works as expected:

sh "ssh ubuntu@${host} sudo ls -al /etc/nginx/sites-available"
sh "ssh ubuntu@${host} sudo ln -sv /etc/nginx/sites-available/my-nginx-conf /etc/nginx/sites-enabled/ -f"

Upvotes: 1

Views: 2228

Answers (2)

finspin
finspin

Reputation: 4071

The remote commands need to be quoted (although most of them work without quotes):

sh "ssh ubuntu@${host} 'sudo ln -sv /etc/nginx/sites-available/* /etc/nginx/sites-enabled/ -f'"

Upvotes: 1

Adam Katz
Adam Katz

Reputation: 16236

Sometimes remote ssh commands (and locations for scp) need extra escaping. I know you've already quoted your query, but you may need one additional level of escapes (and because the escape would be interpreted by your double-quotes rather than being passed to the SSH command, you need one more.

Try double-escaping that wildcard:

sh "ssh ubuntu@${host} sudo ls -al /etc/nginx/sites-available"
sh "ssh ubuntu@${host} sudo ln -sv /etc/nginx/sites-available/\\* /etc/nginx/sites-enabled/ -f"

You can also combine those into one SSH call:

sh "ssh ubuntu@${host} sudo ls -al /etc/nginx/sites-available; sudo ln -sv /etc/nginx/sites-available/\\* /etc/nginx/sites-enabled/ -f"

I'm not a Jenkins expert. If this doesn't work, I'd try yet another pair of escapes, changing your original …/sites-available/* to …/sites-available/\\\\*

Upvotes: 1

Related Questions