【问题标题】:How to specify an ssh key for the jenkins git plugin如何为 jenkins git 插件指定 ssh 密钥
【发布时间】:2017-01-30 18:51:05
【问题描述】:

我正在使用git plugin,据说它集成了“Jenkins 凭据管理功能”:

凭据:用于连接到存储库的凭据(除非允许匿名访问),使用 Jenkins 凭据管理功能。使用的凭证类型取决于底层协议。对于 SSH 连接,仅支持私钥身份验证。

太好了,“Jenkins 凭据管理功能”对我有用:我能够运行“来自 SCM 的管道脚本”。

那么,如何在 git 插件中使用这些凭据? git 插件文档和网络中都没有示例。这是我的 Jenkinsfile 的相关部分,带有 git 步骤:

node {
    stage('Checkout') {
        git url: 'ssh://git@5.6.7.8:5999/my/repo.git', branch: 'wip'
    }
    ...
}

【问题讨论】:

  • 尝试转到Jenkins > Credentials > System > Global credentials > Add Credentials,然后选择SSH Username with private key?然后在您的 Jenkins 工作中,您只需在设置工作时引用这些凭据。您不必使用全局凭据,您可以使用不同的域。
  • @Harmelodic:如前所述,添加了凭据,并且它们正在工作。我只是不知道如何在运行管道的 Jenkinsfile 的 Checkout 阶段从 git 命令中引用它们。
  • 我没有对此进行测试,但根据documentation(滚动到最后),您可以使用GenericSCMStep 并提供一个credentialIdcheckout scm: [$class: 'MercurialSCM', source: 'ssh://hg@bitbucket.org/user/repo', clean: true, credentialsId: '1234-5678-abcd'], poll: false。或者你有this sample使用命令行git客户端和Credentials Binding Plugin,也未经测试。
  • @delavnog 你能解决这个问题吗?我也无法让它使用我的 ssh 凭据

标签: git jenkins ssh


【解决方案1】:

他们似乎同时添加了一些文档: https://jenkins.io/doc/pipeline/steps/git/

因此,对于您的示例,以下内容应该有效:

checkout([$class: 'GitSCM', branches: [[name: '*/wip']],
    userRemoteConfigs: [[url: 'ssh://git@5.6.7.8:5999/my/repo.git',
    credentialsId: 'your-credentials-id']]])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-25
    • 2015-08-31
    • 2021-03-02
    • 2011-01-27
    • 1970-01-01
    • 2022-09-23
    相关资源
    最近更新 更多