gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 21:24:08 24 4
gpt4 key购买 nike

我正在尝试从 SCM 加载 Jenkins 管道脚本。我必须构建一个 docker 镜像并将其推送到 GCR。在 docker 镜像中,我需要安装私有(private) 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', 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")
}
}
}

Docker 文件:

# 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 中安装我的私有(private) bitbucket 存储库。另外,我想避免 ssh 身份验证。

最佳答案

您是否需要始终提示输入凭据?如果没有,您可以将它们存储在 Jenkins 凭证存储中并通过 Jenkins Credentials Binding plugin 中的 withCredentials 步骤检索它们.这样,如果您在闭包内进行构建,它们就会隐藏在日志中。

withCredentials([usernamePassword(
credentialsId: 'privateGitCredentials',
usernameVariable: 'USERNAME',
passwordVariable: 'PASSWORD'
)]) {
sh "docker build --build-arg username=$USERNAME --build-arg password=$PASSWORD -t <your tag> ."
}

关于docker - 如何将 Jenkins 管道中的 git 凭据输入到 docker 文件中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48107059/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com