写入docker绑定挂载的文件在jenkins主机上不可见
更新 从日志中我看到写入docker绑定挂载的文件在jenkins主机上不可见,docker,jenkins,jenkins-pipeline,Docker,Jenkins,Jenkins Pipeline,更新 从日志中我看到 21:05:51 Running on ip-10-0-4-23.eu-west-2.compute.internal in /home/jenkins/workspace/myproj ... ... docker stuff happens here ... 21:05:54 Running on ip-10-0-4-9.eu-west-2.compute.internal in /home/jenkins/workspace/myproj 因此,我假设由于主机不
21:05:51 Running on ip-10-0-4-23.eu-west-2.compute.internal in /home/jenkins/workspace/myproj
...
... docker stuff happens here
...
21:05:54 Running on ip-10-0-4-9.eu-west-2.compute.internal in /home/jenkins/workspace/myproj
因此,我假设由于主机不同(注意差异IP),绑定装载在第一个主机上,而不是第二个主机上
有没有办法强制它保持在同一主机/代理上
老问题
我有一个jenkins管道作业,它使用docker容器来完成大部分的重物搬运。我想在docker容器中“做一些事情”,最终生成一堆文件。我希望这些文件在docker容器退出(并被jenkins删除)后在“父”代理上可用,这样我就可以将它们压缩并存档
由于jenkins将启动docker并使用绑定挂载将jenkins工作区链接到容器内的文件夹,我本以为将文件写入docker容器内的该文件夹会导致该文件存在于主机上,但这似乎不起作用
Generate artifact
块中的ls-la
没有显示build-${env.build\u NUMBER}.txt中的文件。我已经用pwd
验证了我在写入/列出文件时是否在正确的目录中
pipeline {
agent any
environment {
userId = sh(script: "id -u ${USER}", returnStdout: true).trim()
}
stages {
stage('Prep') {
// do some stuff on the "any" agent
}
stage('Build') {
agent {
dockerfile {
filename 'Dockerfile.build'
additionalBuildArgs "--build-arg UID=${userId}"
}
}
steps {
sh "touch file-from-build-${env.BUILD_NUMBER}.txt"
sh 'ls -la'
}
}
stage('Generate artifact') {
steps {
sh "ls -la"
}
}
}
}
从日志中,jenkins发布docker时使用了:
docker run -t -d -u 1001:1001 -w /home/jenkins/workspace/myproj -v /home/jenkins/workspace/myproj:/home/jenkins/workspace/myproj:rw,z -v /home/jenkins/workspace/myproj@tmp:/home/jenkins/workspace/myproj@tmp:rw,z
My Dockerfile.build如果有帮助:
FROM php:7.4-alpine
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer --2
RUN apk update && apk upgrade && \
apk add --no-cache git openssh zip
# create a jenkins user inside the docker image (so that I can put some ssh keys for use in it's home folder)
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 \
&& chown -R jenkins:jenkins /home/jenkins/.ssh \
&& chmod 700 /home/jenkins/.ssh/ \
&& chmod 600 /home/jenkins/.ssh/id_rsa
USER jenkins
如果重要的话,我正在docker容器中进行繁重的工作,因为我需要安装一些在普通jenkins代理上没有的工具。据我所知,reuseNode true
指令是做您想做的事情的必要条件,请参阅
在你的情况下是这样的
agent {
dockerfile {
filename 'Dockerfile.build'
additionalBuildArgs "--build-arg UID=${userId}"
reuseNode true
}
}
我不得不说,我已经看到,reuseNode-true
并不总是像预期的那样工作,但我没有时间深入研究这个问题。ahhh。希望我在30分钟前看到你发帖子的时候。我最终使用了customImage=docker.build(…)
和customImage.inside(…)
,这似乎是可行的。现在,我将研究使用dockerfile代理与上述方法相比是否有利弊。谢谢