【问题标题】:Jenkins Scripted Pipeline supply GitHub SSH credentials for a 'sh' build stepJenkins Scripted Pipeline 为“sh”构建步骤提供 GitHub SSH 凭据
【发布时间】:2018-10-31 10:58:16
【问题描述】:

我在脚本化的 Jenkins 管道中有一个 sh 步骤,该步骤反过来要求 git 通过 SSH 克隆一个私有 GitHub 存储库(它是一个依赖管理系统,用于处理托管在 GitHub 上的依赖项)。管道本身通过 GitHub 组织插件执行。它包括一个checkout scm 命令,该命令还访问一个私有存储库,从日志中我看到它利用GIT_SSH 环境变量将登录信息传递给该步骤(工作正常)。有没有办法将相同的信息也传递给另一个步骤,即我的sh "install_dependencies.sh" 步骤?请注意,withCredentials 可以让我通过环境变量访问凭据,但是如何将它们传递给 git? GIT_SSH 是 ssh 包装脚本的路径,我不知道如何使用 withCredentials 生成它。还有另一种通过环境变量将凭据传递给 git 的方法吗?

最小的Jenkinsfile 如下所示:

node {
    checkout scm  // successfully accesses github credentials
    withCredentials([
        // what to put here?
    ]) {
        // install python dependencies
        sh 'pip install -r requirements.txt'
    }
    sh 'pytest'
}

文件requirements.txt 列出了依赖关系,它包含一行表格 -e git+ssh://git@github.com/some_private/repo@dev#egg=egg_name 它指示pip 通过git 获取需求。

可能可以使用另一个 Jenkins checkout scm 步骤手动签出代码,然后从 Jenkinsfile 中安装它。但是,这会破坏将 requirements.txt 作为需求的唯一真实来源的目的。

【问题讨论】:

标签: git jenkins github ssh jenkins-pipeline


【解决方案1】:

我刚刚发现了这个问题,根据文档对我有用的是

        withCredentials([sshUserPrivateKey(credentialsId: 'Credential-id', keyFileVariable: 'CERT')]) {
      sh 'cp -prf $CERT ~/.ssh/id_rsa && chmod 600 ~/.ssh/id_rsa'
}

这会将证书从它所在的位置复制到本地 docker 证书存储。 这比允许我在进行依赖拉动时进行克隆

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-21
    • 2020-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多