在Jenkins中,您可以使用withCredentials
步骤来安全地存储和使用SSH私钥进行认证
withCredentials
步骤来引用刚刚添加的SSH私钥凭证。以下是一个示例: pipeline {
agent any
stages {
stage('Example') {
steps {
script {
withCredentials([sshUserPrivateKey(credentialsId: 'your-credentials-id', keyFileVariable: 'SSH_KEY_FILE')]) {
sh '''
# 使用SSH私钥执行远程命令
ssh -i $SSH_KEY_FILE user@remote-host "your-command"
'''
}
}
}
}
}
}
请将your-credentials-id
替换为您在步骤2中创建的凭证ID,将user@remote-host
和your-command
替换为您的实际SSH登录凭据和要执行的远程命令。
现在,当您运行此Pipeline时,Jenkins将使用提供的SSH私钥进行认证,并在远程主机上执行指定的命令。
领取专属 10元无门槛券
手把手带您无忧上云