【问题标题】:How to correctly pass ssh key file from Jenkins credentials variable into to docker build command?如何正确地将 ssh 密钥文件从 Jenkins 凭据变量传递到 docker build 命令?
【发布时间】:2021-04-01 14:53:22
【问题描述】:

这个问题是对这个问题的跟进 How to pass jenkins credentials into docker build command?

我正在从我的 groovy 管道中的 jenkins 凭证存储中获取 ssh 密钥文件,并且 通过 --build-arg 将它传递给 docker build 命令,这样我就可以从我的 docker 容器中的私有 git repos 签出和构建工件

凭据存储 id :cicd-user,它可以按预期从我的 groovy Jenkinsfile 中检查我的私人作品

checkout([$class: 'GitSCM',
            userRemoteConfigs: [[credentialsId: 'cicd-user', url:'ssh://git@bitbucket.myorg.co:7999/A/software.git']]

我访问它并尝试将其传递给 docker build 命令:

  withCredentials([sshUserPrivateKey(credentialsId: 'cicd-user', keyFileVariable: 'FILE')]) { 
           sh "cd ${WORKSPACE} && docker build -t ${some-name} --build-arg USERNAME=cicd-user --build-arg  PRIV_KEY_FILE=\$FILE --network=host -f software/tools/jenkins/${some-name}/Dockerfile ."
        }

在 Dockerfile 我做

RUN echo "$PRIV_KEY_FILE" > /home/"$USERNAME"/.ssh/id_rsa && \
 chmod 700 /home/"$USERNAME"/.ssh/id_rsa 

运行 echo "Host bitbucket.myorg.co\n\tStrictHostKeyChecking no\n" >> ~/.ssh/config

但我看到以下问题

"加载密钥"/home/cicd-user/.ssh/id_rsa" :(无效格式) “git@Bitbucket.mycomp.co:权限被拒绝(公钥) “致命:无法从远程存储库中读取”

过去我通过像下面这样的 cat'ing 从外部将 ssh priv 密钥作为 --build-arg 传递

--build-arg ssh_prv_key="$(cat ~/.ssh/id_rsa)"

我应该做类似的事情

--build-arg PRIV_KEY_FILE="$(cat $FILE)"

关于可能出了什么问题或我应该在哪里寻找正确调试的任何想法?

【问题讨论】:

    标签: docker jenkins devops credentials jenkins-docker


    【解决方案1】:

    我昨天遇到了同样的问题,我想我想出了一个可行的解决方案。

    这是我采取的基本步骤 - 使用 sshagent plugin 在 Jenkins 作业中管理 sshagent。您也可以使用 withCredentials,尽管这不是我最终获得成功的原因。

    可以使用docker build 命令 --ssh 标志使 ssagent(或密钥)可用于特定的构建步骤。 (Feature reference) 重要的是要注意,要使其工作(在当前时间),您需要设置 DOCKER_BUILDKIT=1。如果您忘记执行此操作,那么它似乎会忽略此配置并且 ssh 连接将失败。一旦设置好了,sshagent

    向下看管道:

    pipeline {
        agent {
            // ...
        }
        environment {
            // Necessary to enable Docker buildkit features such as --ssh
            DOCKER_BUILDKIT = "1"
        }
        stages {
            // other stages
    
            stage('Docker Build') {
                steps {
                    // Start ssh agent and add the private key(s) that will be needed in docker build
                    sshagent(['credentials-id-of-private-key']) {
                        // Make the default ssh agent (the one configured above) accessible in the build
                        sh 'docker build --ssh default .'
                    }
                }
            // other stages
            }
        }
    }
    

    在 Dockerfile 中,有必要明确指定需要它访问 ssh 代理的行。这可以通过在相关的 RUN 命令中包含 mount=type=ssh 来完成。

    对我来说,大致是这样的:

    FROM node:14
    # Retrieve bitbucket host key
    RUN mkdir -p -m -0600 ~/.ssh && ssh-keyscan bitbucket.org >> ~/.ssh/known_hosts
    ...
    # Mount ssh agent for install
    RUN --mount=type=ssh npm i
    ...
    

    通过此配置,npm install 能够通过 sshagent 使用 docker build 中的 SSH 私钥来安装存储在 Bitbucket 上的私有 git 存储库。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-02
      • 1970-01-01
      • 2014-02-19
      • 1970-01-01
      • 2012-07-18
      • 1970-01-01
      • 2021-09-01
      • 2019-01-14
      相关资源
      最近更新 更多