Jenkins中用于清理工作区的Groovy脚本

时间:2019-07-15 03:42:34

标签: jenkins groovy jenkins-groovy

我有下面的代码,这些代码是我从stackoverflow的某人那里获得的。我实质上添加的是try-catch和rm -rf来删除清理目录。但是基于以下代码,它似乎确实可以工作:

摘要

rm -rf /home/ec2-user/workspace/*ws-cleanup*

完整脚本:

import hudson.util.RemotingDiagnostics;

print_ip = 'println InetAddress.localHost.hostAddress';
print_hostname = 'println InetAddress.localHost.canonicalHostName';

// here it is - the shell command, uname as example 
uname = 'def proc = "uname -a".execute(); proc.waitFor(); println proc.in.text';
ls = 'def proc = "ls -al /home/ec2-user/workspace/".execute(); proc.waitFor(); println proc.in.text';
remove = 'def proc = "rm -rf /home/ec2-user/workspace/*ws-cleanup*".execute(); proc.waitFor(); println proc.in.text';

for (slave in hudson.model.Hudson.instance.slaves) {
    println("===========================================")
    println("node: " + slave.getDisplayName())
    println("===========================================")
    println slave.name;
    try {
        println RemotingDiagnostics.executeGroovy(print_ip, slave.getChannel());
        println RemotingDiagnostics.executeGroovy(print_hostname, slave.getChannel());
        println RemotingDiagnostics.executeGroovy(uname, slave.getChannel());
        println RemotingDiagnostics.executeGroovy(ls, slave.getChannel());
        println RemotingDiagnostics.executeGroovy(remove, slave.getChannel());
        println("After removing");
        println RemotingDiagnostics.executeGroovy(ls, slave.getChannel());
     } catch(NullPointerException e) {
        println("Catch an exception");
       }
}

有人可以帮我纠正我的错误吗?我当然想使用 ws-cleanup

0 个答案:

没有答案