通过 jenkins 管道删除文件时出现“java.nio.file.AccessDeniedException:/home/jenkins/workspace/testCases/41/1/1.in”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44758390/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 08:19:28  来源:igfitidea点击:

'java.nio.file.AccessDeniedException:/home/jenkins/workspace/testCases/41/1/1.in' when deleting files over jenkins pipeline

javajenkins

提问by killer_nudt

I have a jenkins server on a linux host.I try to delete some files using jenkins pipeline from jenkins dashboard.my pipeline script goes like follwing:

我在 linux 主机上有一个 jenkins 服务器。我尝试使用 jenkins 仪表板中的 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'
........

when I run this script, it turns out that,sometimes it can be completed successfully but sometimes failed,the error logs are as following:

当我运行这个脚本时,发现有时可以成功完成有时失败,错误日志如下:

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.invoke(FilePath.java:1912)
at hudson.FilePath.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.run(Request.java:336)
at hudson.remoting.InterceptingExecutorService.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.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.call(SynchronousNonBlockingStepExecution.java:49)
at hudson.security.ACL.impersonate(ACL.java:260)
at org.jenkinsci.plugins.workflow.steps.SynchronousNonBlockingStepExecution.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

I just want to delete old files before writing new data,but this porblem really confused me.

我只想在写入新数据之前删除旧文件,但是这个问题真的让我感到困惑。

回答by M. Dhaouadi

Permission denied means you don' have the right on that folder, perhaps you are not their owner? can you run the command as root?

权限被拒绝意味着您没有该文件夹的权限,也许您不是他们的所有者?你能以root身份运行命令吗?

回答by Anand Bajpai

it is clear that Jenkins does not have rights on those files. I faced this issue once I tested jenkins by running with root user temporarily.

很明显 Jenkins 对这些文件没有权限。一旦我通过临时以 root 用户运行来测试 jenkins,我就遇到了这个问题。

Go to Jenkins Config file (Red Hat - /etc/sysconfig/jenkins), and see which user you are using to run Jenkins). File which cannot be deleted might belong to some other user.

转到 Jenkins 配置文件(Red Hat - /etc/sysconfig/jenkins),并查看您使用哪个用户来运行 Jenkins)。无法删除的文件可能属于其他用户。

Delete those files manually with appropriate user and you should be good.

使用适当的用户手动删除这些文件,您应该没问题。