如何使用Jenkins管道输入到docker文件中的git凭据?

Nit*_*esh 8 jenkins docker devops jenkins-pipeline

我正在尝试从SCM加载Jenkins管道脚本.我必须构建一个docker镜像并将其推送到GCR.在docker镜像中,我需要安装私有git存储库.在这里,我试图从Jenkins输入获取git用户名密码.但我不知道如何在Dockerfile中使用它来拉取git repo.这些是我在SCM中的Jenkinsfile和Dockerfile.有什么建议?

詹金斯文件:

node {
def app

stage('Clone repository') {
    checkout scm

    def COMMITHASH = sh(returnStdout: true, script: "git log -n 1 --pretty=format:'%h'").trim()
    echo ("Commit hash: "+COMMITHASH.substring(0,7))
}

stage('Build image') {

    timeout(time: 600, unit: 'SECONDS') { 
        gitUser = input(
           id: 'gitUser', 
           message: 'Please enter git credentials :', 
           parameters: [
           [$class: 'TextParameterDefinition', defaultValue: "", description: 'Git user name', name: 'username'],
           [$class: 'PasswordParameterDefinition', defaultValue: "", description: 'Git password', name: 'password']
        ])
    }

    /* Build docker image */
    println('Build image stage');
    app = docker.build("testBuild")

}

stage('Push image') {
    /* Push image to GCR */

    docker.withRegistry('https://us.gcr.io', 'gcr:***') {
        app.push("${env.BUILD_NUMBER}")
        app.push("latest")
    }
}
}
Run Code Online (Sandbox Code Playgroud)

Dockerfile:

# use a ubuntu 16.04 base image
FROM ubuntu:16.04

MAINTAINER "someuser@company.com"

# Set environment variables
ENV DEBIAN_FRONTEND noninteractive
ENV LC_ALL C.UTF-8

# Upgrade the system
RUN apt-get update && apt-get -y upgrade && apt-get install -y python-software-properties software-properties-common

# Install cert bot and apache
RUN apt-get install -y apache2

#Enable apache modules
RUN a2enmod ssl 
RUN a2enmod headers
RUN a2enmod rewrite

# Create directory for web application
RUN mkdir -p /var/www/myApp


# Expose ssl port
EXPOSE 443
Run Code Online (Sandbox Code Playgroud)

我想在/ var/www/myApp中安装我的私有bitbucket存储库.另外,我想避免使用ssh身份验证.

sai*_*sai 2

您应该在 docker 构建期间将 git 用户名和密码作为环境变量传递,然后在 Dockerfile 中调用这些变量。

Dockerfile 示例 -

FROM test
ARG username
ARG password
RUN git clone https://${username}:${password}@github.com/private-repo-name.git
Run Code Online (Sandbox Code Playgroud)

构建命令:

docker build --build-arg username=$git_username --build-arg password=$git_password -t <your tag> .
Run Code Online (Sandbox Code Playgroud)

  • 谢谢回复。我考虑过这种情况。问题是 Jenkins 在构建过程中在日志中输出“git clone”命令。因此,git 的用户名和密码会在 jenkins 日志中公开,我想避免这种情况,因为其他开发人员可以访问 Jenkins。 (2认同)