【问题标题】:Pass Jenkins credentials to Docker build for Composer usage将 Jenkins 凭据传递给 Docker 构建以供 Composer 使用
【发布时间】:2020-06-28 13:46:54
【问题描述】:

我在我们公司的 BitBucket 私有存储库中有一个作曲家包。要访问它,我需要使用存储在 Jenkins 中的凭据。目前整个构建基于声明式管道和 Dockerfile。要将凭据传递给 Composer,我需要在 build 阶段将这些凭据传递给 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 是空的。

【问题讨论】:

  • 我没有使用 Docker 代理,所以我只是在猜测 - 你不应该使用双引号在 additionalBuildArgs 行中进行字符串插值吗?

标签: docker jenkins composer-php jenkins-pipeline jenkins-docker


【解决方案1】:

TL;DR 使用 jenkins withCredentials([sshUserPrivateKey()]) 并将私钥回显到容器中的 id_rsa 中。

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

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

  1. 如果您不小心,可能会在构建日志中暴露您的私钥(以下是小心的,但很容易忘记)。 (尽管考虑到这一点,对于任何有顽皮意图的人来说,提取 jenkins 凭据似乎是 extremely easy?)

所以谨慎使用...

在我的(旧版)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

然后在我的 Jenkinsfile 中:

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 文件中运行? ¯_(ツ)_/¯

【讨论】:

  • 这似乎比我的回答更准确。赞成。
  • 我想我只是被警告 #2 中的问题所困扰(以 root 身份运行)。我的构建清理现在因一些权限问题而失败(构建工作正常,然后突然开始)。所以目前正在考虑在 Dockerfile 中创建一个与 jenkins 用户具有相同 UID 的用户。不知道这是否会有所帮助...
【解决方案2】:

有一个similar issue,据说固定在PR 327,带有管道模型定义-1.3.9

所以开始检查你的插件的版本吧。

但也要注意Dockerfile warning:

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

Using buildkit 和 --secret 是一种更好的方法。

【讨论】:

  • 这解决了一个相关问题,但不是问题中的问题,即如何将凭据作为构建参数传递。我也有这个问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-02-02
  • 1970-01-01
  • 2018-03-09
  • 2021-05-26
  • 2022-01-06
  • 2020-12-04
  • 2016-09-24
相关资源
最近更新 更多