加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-16 03:35:59 所属栏目:安全 来源:网络整理
导读:我正在尝试从SCM加载Jenkins管道脚本.我必须构建一个docker镜像并将其推送到GCR.在docker镜像中,我需要安装私有git存储库.在这里,我试图从Jenkins输入获取git用户名密码.但我不知道如何在Dockerfile中使用它来拉取git repo.这些是我在SCM中的Jenkinsfile和Do

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

jenkins文件:

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',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")
    }
}
}

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

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

最佳答案
您应该在docker build期间将git用户名和密码作为环境变量传递,然后在Dockerfile中调用这些变量.

示例Dockerfile –

FROM test
ARG username
ARG password
RUN git clone https://${username}:${password}@github.com/private-repo-name.git

构建命令:

docker build --build-arg username=$git_username --build-arg password=$git_password -t 

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读