将Jenkins凭据传递给Docker build以供编写器使用

将Jenkins凭据传递给Docker build以供编写器使用,docker,jenkins,composer-php,jenkins-pipeline,jenkins-docker,Docker,Jenkins,Composer Php,Jenkins Pipeline,Jenkins Docker,我在公司的私人存储库BitBucket上有一个composer软件包。要访问它,我需要使用Jenkins中存储的凭据。目前,整个构建基于声明性管道和Dockerfile。要将凭据传递给Composer,我需要在buildstage中将这些凭据传递给Dockerfile 我怎样才能做到呢 我试过: // Jenkinsfile agent { dockerfile { label 'mylabel' filename '.docker/php/Dockerf

我在公司的私人存储库BitBucket上有一个composer软件包。要访问它,我需要使用Jenkins中存储的凭据。目前,整个构建基于声明性管道和Dockerfile。要将凭据传递给Composer,我需要在
build
stage中将这些凭据传递给Dockerfile

我怎样才能做到呢

我试过:

// Jenkinsfile
agent {
    dockerfile {
        label 'mylabel'
        filename '.docker/php/Dockerfile'
        args '-v /net/jenkins-ex-work/workspace:/net/jenkins-ex-work/workspace'
        additionalBuildArgs '--build-arg jenkins_usr=${JENKINS_CREDENTIALS_USR} --build-arg jenkins_credentials=${JENKINS_CREDENTIALS} --build-arg test_arg=test'
    }
}

// Dockerfile
ARG jenkins_usr
ARG jenkins_credentials
ARG test_arg
但是
args
是空的。

有一个管道模型定义-1.3.9,据说是固定的

因此,开始检查插件的版本

但也要注意:

不建议使用构建时变量传递机密,如github密钥、用户凭据等。
使用
docker history
命令,图像的任何用户都可以看到构建时间变量值

使用
--secret
是一种更好的方法。

TL;DR 使用jenkins
和凭据([sshUserPrivateKey()])
并将私钥回显到容器中的id\u rsa中

编辑:删除了“以root身份运行”步骤,因为我认为这会导致问题。相反,在docker容器中创建一个jenkins用户,其UID与构建docker容器的jenkins用户相同(不知道这是否重要,但我们需要一个具有home dir的用户,以便创建~/.ssh/id\u rsa)

对于那些像我一样受苦的人。。。我的解决方案如下。这并不理想,因为:

  • 如果你不小心的话,就有可能在构建日志中暴露你的私钥(下面的内容很小心,但很容易忘记)。(尽管考虑到这一点,提取詹金斯证书似乎适合任何有淘气意图的人?)
  • 所以要小心使用

    在我的(遗留)git项目中,一个简单的php应用程序,具有内部基于git的编写器依赖项,我有

    Dockerfile.build

    FROM php:7.4-alpine
    # install git, openssh, composer... whatever u need here, then:
    # create a jenkins user inside the docker image
    ARG UID=1001
    RUN adduser -D -g jenkins -s /bin/sh -u $UID jenkins \
        && mkdir -p /home/jenkins/.ssh \
        && touch /home/jenkins/.ssh/id_rsa \
        && chmod 600 /home/jenkins/.ssh/id_rsa \
        && chown -R jenkins:jenkins /home/jenkins/.ssh
    USER jenkins
    # I think only ONE of the below are needed, not sure.
    RUN echo "Host bitbucket.org\n\tStrictHostKeyChecking no\n" >> /home/jenkins/.ssh/config \ 
        && ssh-keyscan bitbucket.org >> /home/jenkins/.ssh/known_hosts
    
    然后在我的Jenkins文件中:

    def sshKey = ''
    
    pipeline {
        agent any
    
        environment {
            userId = sh(script: "id -u ${USER}", returnStdout: true).trim()
        }
        
        stages {
            stage('Prep') {
                steps {
                    script {
                        withCredentials([
                            sshUserPrivateKey(
                                credentialsId: 'bitbucket-key',
                                keyFileVariable: 'keyFile',
                                passphraseVariable: 'passphrase',
                                usernameVariable: 'username'
                            )
                        ]) {
                            sshKey = readFile(keyFile).trim()
                        }
                    }
                }
            }
            
            stage('Build') {
                agent {
                    dockerfile {
                        filename 'Dockerfile.build'
                        additionalBuildArgs "--build-arg UID=${userId}"
                    }
                }
                steps {
                    // Turn off command trace for next line, as we dont want to log ssh key
                    sh '#!/bin/sh -e\n' + "echo '${sshKey}' > /home/jenkins/.ssh/id_rsa"
                    // .. proceed with whatever else, like composer install, etc
    

    公平地说,我认为docker容器中的一些RUN命令甚至不是必需的,或者可以从jenkins文件中运行?¯_(ツ)_/“

    我没有使用Docker代理,所以我只是猜测-您是否应该使用双引号在与
    additionalBuildArgs
    的行中进行字符串插值?这解决了一个相关的问题,但不是问题中的问题,即如何将凭据作为构建参数传递。我也遇到了这个问题。这似乎更精确我想我只是被警告2(以root身份运行)中的问题所困扰。我的构建清理现在由于一些权限问题而失败(构建工作正常,然后突然开始)。因此,目前正在考虑在Dockerfile中创建一个用户,其UID与jenkins用户相同。不知道这是否会有帮助。。。