Jenkins execute shell scp

I have a Jenkins build where I am trying to copy a war file from one vm to another. I want to do this using scp. I’ve tried running this in the shell outside of Jenkins and everything works fine. Trying to automate it is a little tricky. I need to provide a password in order to use sudo and also need to enter a password for the vm I am copying to.

In the Jenkins build logs, I am seeing that the build is failing after I execute the custom shell script.

The command I am running in the Jenkins build is

echo 'password' | sudo -S scp -P 5555 application.war user@15.15.15.15:/JBoss/standalone/deployments

The output is

[sudo] password for jenkins: Sorry, try again.
[sudo] password for jenkins: 
sudo: 1 incorrect password attempt

I don’t have any users setup for Jenkins, so I am not understanding the [sudo] password for jenkins error.

I realize that there are Jenkins plugins to achieve what I am trying to do, but none of them are working for me at the moment, which is probably due to bad server configuration. That is why I want to use scp in a custom shell script.

Answer

echo 'password' | sudo -S scp ...

no, it is not a good idea and it does not work, as discussed many times around SE. SSH flushes input before reading password. You should set up ssh keys.

If keys are not an option, there is also tool called sshpass, which you can use to provide password to ssh/scp. The last possibility is expect script, which can interact with interactive prompts.

Attribution
Source : Link , Question Author : Healforgreen , Answer Author : Jakuje

Leave a Comment