How to extract the username and password from a jenkins "username with password" parameter?
withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: credentialsId, usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD']]) { // use USERNAME and PASSWORD as envs - ${USERNAME} // credentialId is the credential id from Credentials tab }
Generally speaking if you are using ssh/scp the ssh-agent is what you want. (And usually you also want public key authentication instead of the password, but that is another story)
node { sshagent (credentials: ['your-credential-id']) { sh 'ssh blaa blaa' }}
As mentioned by @jil it would make things a lot easier if you'd use ssh keys for the authentication at your SSH server - assuming your jenkins server is running on a linux / unix machine. You can add the keys to the system user jenkins
which runs Jenkins and copy it to the machine to which you want to connect for your scp / ssh steps. Afterwards you can directly use any ssh / scp command without providing a password.
There are some security considerations though: adding the SSH key to the
jenkins
user enables every job on the Jenkins server to access your SSH server.
If this is not a problem for you - this is a good article on how to use ssh keys.