首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

Jenkins,使用withCredentials,我如何使用SSH私钥认证?

在Jenkins中,您可以使用withCredentials步骤来安全地存储和使用SSH私钥进行认证

  1. 首先,确保您已经在Jenkins中安装了"Credentials Binding"插件。如果没有安装,请转到"Manage Jenkins" > "Manage Plugins",然后搜索并安装该插件。
  2. 在Jenkins中添加SSH私钥凭证。转到"Manage Jenkins" > "Manage Credentials",然后点击"Global credentials (unrestricted)"。点击"Add Credentials",选择"SSH Username with private key"类型,然后输入您的SSH用户名和私钥。保存更改。
  3. 在您的Jenkins Pipeline脚本中,使用withCredentials步骤来引用刚刚添加的SSH私钥凭证。以下是一个示例:
代码语言:javascript
复制
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-hostyour-command替换为您的实际SSH登录凭据和要执行的远程命令。

现在,当您运行此Pipeline时,Jenkins将使用提供的SSH私钥进行认证,并在远程主机上执行指定的命令。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

领券