This plugin allows you to provide SSH credentials to builds via a ssh-agent in Jenkins.
This is convenient in some cases. See alternatives below.
You need to have the ssh-agent
executable installed on the agent.
First you need to add some SSH Credentials to your instance:
Jenkins | Manage Jenkins | Manage Credentials
Note that only Private Key based credentials can be used.
From a Pipeline job, use the sshagent
step.
steps {
sshagent(credentials: ['ssh-credentials-id']) {
sh '''
[ -d ~/.ssh ] || mkdir ~/.ssh && chmod 0700 ~/.ssh
ssh-keyscan -t rsa,dsa example.com >> ~/.ssh/known_hosts
ssh user@example.com ...
'''
}
}
Alternately, you can use the generic withCredentials
step to bind an SSH private key to a temporary file
and then pass that to commands that require it,
for example using the -i
option to ssh
or scp
:
withCredentials([sshUserPrivateKey(credentialsId: 'github', keyFileVariable: 'PK')]) {
sh 'git -c core.sshCommand="ssh -i $PK" submodule update --init'
}
(Compare gitUsernamePassword
binding.)
Or you can even use ssh-agent
, but without this plugin, useful especially if you need to customize options in any way:
withCredentials([sshUserPrivateKey(credentialsId: 'github', keyFileVariable: 'PK')]) {
sh '''
eval `ssh-agent -s`
trap "ssh-agent -k" EXIT
ssh-add "$PK"
# rest of script…
'''
}
Then configure your build to use the credentials:
And then your build will have those credentials available, e.g.
For new versions, see GitHub releases.
For old versions, see the old changelog.