我有Jenkins在ubuntu机器上的docker上运行。我正在使用以下脚本,首先将脚本从jenkins( docker 镜像)复制到远程主机,然后执行它以修改某些配置文件。
node {
properties([
parameters([
string(name: 'version', defaultValue: '', description: 'Enter the version in x.y.z format')
])
])
def remote = [:]
remote.name = 'testPlugin'
remote.host = 'x.x.x.x'
remote.allowAnyHosts = true
withCredentials([
sshUserPrivateKey(credentialsId: 'ssh-credentials', usernameVariable: 'ssh-user', passphraseVariable: 'ssh-pass')
]) {
remote.user = ssh-user
remote.password = ssh-pass
stage('testPlugin') {
sshPut remote: remote, from: 'myscript.sh', into: '.'
sshScript remote: remote, script: "myscript.sh ${version}"
}
}
}
我可以通过登录运行jenkins的docker镜像来查看myscript.sh文件。但是,当我执行上述代码时,出现以下错误:
java.lang.IllegalArgumentException:/var/jenkins_home/workspace/Cloud-gating/myscript.sh不存在。
任何想法,为什么Jenkins都无法在其工作区中看到该文件。文件(myscript.sh)的所有权是jenkins。执行Jenkins作业的是同一用户。
最佳答案
问题实际上是此脚本的可访问性。
请检查文件是否确实存在:
def exists = fileExists 'myscript.sh'
if (exists) {
echo 'Yes'
} else {
echo 'No'
}
如果没有,请检查您是否对映射到容器目录感到困惑,检查其真实路径和内容:
//inside your pipeline
sh 'pwd'
sh 'ls'
检查哪些主机路径映射到容器内的哪些路径:
docker inspect -f '{{ .Mounts }}' YOUR_CONTAINER_NAME
最后但并非最不重要的一点:您可以在脚本中跳过使用sshPut,sshScript本身会复制文件并远程执行它:
stage('testPlugin') {
sshScript remote: remote, script: "myscript.sh ${version}"
}
干杯。
关于docker - 无法将文件从jenkins推送到远程主机(docker image),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58980632/