我正在尝试从SCM加载Jenkins管道脚本。我必须构建一个docker镜像并将其推送到GCR。在docker镜像中,我需要安装私有git存储库。在这里,我试图从Jenkins输入获取git用户名密码。但我不知道如何在Dockerfile中使用它来拉取git repo。这些是我在SCM中的Jenkinsfile和Dockerfile。有什么建议吗?
Jenkinsfile:
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")
}
}
}
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身份验证。
答案 0 :(得分:0)
您应在Docker构建期间将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 <your tag> .
答案 1 :(得分:0)
您是否需要始终提示输入凭据?
如果没有,您可以将它们存储在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> ."
}