In jenkins job, create file using system groovy in current workspace

前端 未结 4 623
鱼传尺愫
鱼传尺愫 2020-12-08 10:41

my task is to collect node details and list them in certail format. I need to write data to a file and save it as csv file and attach it as artifacts. But i am not able to c

4条回答
  •  鱼传尺愫
    2020-12-08 11:06

    Problem Groovy system script is always run in jenkins master node, while the workspace is the file path in your jenkins slave node, which doesn't exist in your master node.

    You can verify by the code

    theDir = new File(envVars.get('WORKSPACE'))
    println theDir.exists()
    

    It will return false

    If you don't use slave node, it will return true

    Solution As we can't use normal File, we have to use FilePath http://javadoc.jenkins-ci.org/hudson/FilePath.html

    if(build.workspace.isRemote())
    {
        channel = build.workspace.channel;
        fp = new FilePath(channel, build.workspace.toString() + "/node_details.txt")
    } else {
        fp = new FilePath(new File(build.workspace.toString() + "/node_details.txt"))
    }
    
    if(fp != null)
    {
        fp.write("test data", null); //writing to file
    } 
    

    Then it works in both case.

提交回复
热议问题