how to use publish over ssh on Jenkinsfile and scp using groovy SDL on Jenkinsfile?

Kuldeep Darmwal picture Kuldeep Darmwal · Feb 28, 2017 · Viewed 10.3k times · Source

java.lang.NoSuchMethodError: No such DSL method 'publishOverSsh' found among steps [archive, bat, build, catchError, checkout, deleteDir, dir, dockerFingerprintFrom, dockerFingerprintRun, echo, emailext, emailextrecipients, envVarsForTool, error, fileExists, getContext, git, input, isUnix, libraryResource, load, mail, milestone, node, parallel, properties, pwd, readFile, readTrusted, resolveScm, retry, script, sh, sleep, stage, stash, step, svn, timeout, timestamps, tool, unarchive, unstash, waitUntil, withContext, withCredentials, withDockerContainer, withDockerRegistry, withDockerServer,

Answer

Kuldeep Darmwal picture Kuldeep Darmwal · Mar 2, 2017

I used SCP and SSH on remote machine using Jenkins groovy DSL, using Sh:shell script.

Before that use ssh-keygen to create connection between host machine and Jenkins for password less authentication.

For SSH login without password:

1) create ssh public and private key using ssh-keygen with this command:

$ssh-keygen

2) Copy the public key to remote-host in .ssh/authorized_keys file.

3) Now login to remote machine with out password.

Now to publish package to remote server use below command in Jenkinsfile script box :

sh 'scp -r ./tests ubuntu@HOST_IP/URL:/home/ubuntu/'

//to execute commands over ssh on remote host which is written in test-script.sh file

sh 'ssh ubuntu@HOST_IP/URL <  test-script.sh'

// For echo environmental variables BUILD_ID

echo "Running ${env.BUILD_ID} on ${env.JENKINS_URL}"