詹金斯版2.121.3-从工作区删除文件

时间:2018-09-04 13:53:39

标签: jenkins-pipeline

在詹金斯版中。 2.121.3使用管道尝试删除文件。其给脚本不允许的错误消息。

是否还有其他方法可以不使用OS命令删除Jenkins中的文件?

Scripts not permitted to use method java.io.File delete. Administrators can decide whether to approve or reject this signature.
[Pipeline] End of Pipeline
org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use method java.io.File delete

管道代码

stage('Delete test.zip file') {
    if (fileExists('test.zip')) {
        new File('test.zip').delete()
    } else {
        println "test.zip file not found"
    }
}

3 个答案:

答案 0 :(得分:1)

有几种替代方法:

  1. 通过jenkins shared library,您可以将此代码包装为函数或类:
#!/usr/bin/groovy
package org.utils

class PipelineUtils { 
    static def deleteFile(String name) { new File(name).delete() } 
}

在管道脚本中,您需要导入库:

@Library('your-jenkins-library')_    
import static org.utils.PipelineUtils.*
deleteFile('test.zip')
  1. @Sean建议通过“管理Jenkins>进程内脚本批准”批准脚本。
  2. File Operations Plugin
fileOperations([script.fileDeleteOperation(excludes: '', includes: 'test.zip')])
  1. Workspace Cleanup Plugin,但是您需要找到合适的排除模式,否则将清除所有文件:
def new_exclude_patterns = [[pattern: ".git/**", type: 'EXCLUDE']]
cleanWs deleteDirs: false, skipWhenFailed: false, patterns: new_exclude_patterns

答案 1 :(得分:0)

导航到/ scriptApproval /(管理Jenkins>进程内脚本批准)并批准脚本。

答案 2 :(得分:0)

如果您正在linux从属服务器(或路径中带有sh的Windows从属服务器)上运行管道,则可以使用以下调用来避免任何脚本批准

c = Client()
r = c.lookup("213.73.91.35")
print(r.asn)
相关问题