“爪哇。尼奥。文件AccessDeniedException:/home/jenkins/workspace/testCases/41/1。在jenkins管道上删除文件时
我在linux主机上有一个jenkins服务器。我尝试从jenkins dashboard中使用jenkins管道删除一些文件。我的管道脚本如下所示:
node(){
int deleteCount=0
while(deleteCount < 10)
{
try{
deleteCount++
sh script:'rm -rf ../testCases/41/*'
boolean fileExist = fileExists '../testCases/41/1/1.in'
if(!fileExist) break
}catch(err)
{
sleep 1
}
}
writeFile file: '../testCases/41/1/1.in', text: 'bookstore'
writeFile file: '../testCases/41/1/1.out', text: 'databaseName:bookstore'
writeFile file: '../testCases/41/1/1.isPublic', text: '1'
writeFile file: '../testCases/41/1/2.in', text: 'bookstore2'
writeFile file: '../testCases/41/1/2.out', text: 'databaseName:bookstore2'
writeFile file: '../testCases/41/1/2.isPublic', text: '1'
writeFile file: '../testCases/41/2/1.in', text: 'book\n5\nid int(11)\nauthor
varchar(255)\ntitle varchar(255)\npublisher varchar(255)\npublihserYear
int(11)'
writeFile file: '../testCases/41/2/1.out', text: 'table:book'
writeFile file: '../testCases/41/2/1.isPublic', text: '1'
writeFile file: '../testCases/41/2/2.in', text: 'book2\n3\nid int(11)\ntitle
varchar(255)\nprice int(11)'
writeFile file: '../testCases/41/2/2.out', text: 'table:book2'
writeFile file: '../testCases/41/2/2.isPublic', text: '1'
........
当我运行这个脚本时,结果是,有时可以成功完成,但有时失败,错误日志如下所示:
1
+ rm -rf ../testCases/41
rm: can't remove '../testCases/41/1/1.in': Permission denied
rm: can't remove '../testCases/41/1/1.out': Permission denied
rm: can't remove '../testCases/41/1/1.isPublic': Permission denied
rm: can't remove '../testCases/41/1/2.in': Permission denied
rm: can't remove '../testCases/41/1/2.out': Permission denied
rm: can't remove '../testCases/41/1/2.isPublic': Permission denied
rm: can't remove '../testCases/41/1': Permission denied
rm: can't remove '../testCases/41/2/1.in': Permission denied
rm: can't remove '../testCases/41/2/1.out': Permission denied
rm: can't remove '../testCases/41/2/1.isPublic': Permission denied
rm: can't remove '../testCases/41/2/2.in': Permission denied
rm: can't remove '../testCases/41/2/2.out': Permission denied
rm: can't remove '../testCases/41/2/2.isPublic': Permission denied
rm: can't remove '../testCases/41/2': Permission denied
rm: can't remove '../testCases/41/3/1.in': Permission denied
.......`1`
[Pipeline] sleep
Sleeping for 1 sec
[Pipeline] writeFile
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
java.nio.file.AccessDeniedException:
/home/jenkins/workspace/testCases/41/1/1.in
at sun.nio.fs.UnixException.translateToIOException(UnixException.java:84)
at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:102)
at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:107)
atsun.nio.fs.UnixFileSystemProvider.newByteChannel(UnixFileSystemProvider.ja
va:214)
.....
at hudson.FilePath$37.invoke(FilePath.java:1912)
at hudson.FilePath$37.invoke(FilePath.java:1908)
at hudson.FilePath$FileCallableWrapper.call(FilePath.java:2739)
at hudson.remoting.UserRequest.perform(UserRequest.java:153)
at hudson.remoting.UserRequest.perform(UserRequest.java:50)
at hudson.remoting.Request$2.run(Request.java:336)
at hudson.remoting.InterceptingExecutorService$1.call(InterceptingExecutorService.java:68)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at hudson.remoting.Engine$1$1.run(Engine.java:94)
at java.lang.Thread.run(Thread.java:745)
at ......remote call to Channel to /10.9.186.111(Native Method)
at hudson.remoting.Channel.attachCallSiteStackTrace(Channel.java:1545)
at hudson.remoting.UserResponse.retrieve(UserRequest.java:253)
at hudson.remoting.Channel.call(Channel.java:830)
at hudson.FilePath.act(FilePath.java:986)
Caused: java.io.IOException: remote file operation failed: /home/jenkins/workspace/testCases/41/1/1.in at hudson.remoting.Channel@63880c2b:Channel to /10.9.186.111
at hudson.FilePath.act(FilePath.java:993)
at hudson.FilePath.act(FilePath.java:975)
at hudson.FilePath.write(FilePath.java:1908)
at org.jenkinsci.plugins.workflow.steps.WriteFileStep$Execution.run(WriteFileStep.java:94)
at org.jenkinsci.plugins.workflow.steps.WriteFileStep$Execution.run(WriteFileStep.java:84)
at org.jenkinsci.plugins.workflow.steps.SynchronousNonBlockingStepExecution$1$1.call(SynchronousNonBlockingStepExecution.java:49)
at hudson.security.ACL.impersonate(ACL.java:260)
at org.jenkinsci.plugins.workflow.steps.SynchronousNonBlockingStepExecution$1.run(SynchronousNonBlockingStepExecution.java:46)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:748)
Finished: FAILURE
我只想在写新数据之前删除旧文件,但这个问题真的让我困惑
# 1 楼答案
很明显,詹金斯对这些文件没有权限。 有一次我测试jenkins时遇到了这个问题,我暂时使用root用户
转到Jenkins配置文件(Red Hat-/etc/sysconfig/Jenkins),查看运行Jenkins的用户。无法删除的文件可能属于其他用户
用合适的用户手动删除这些文件,你应该很好