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

前端 未结 4 582
鱼传尺愫
鱼传尺愫 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.

    0 讨论(0)
  • 2020-12-08 11:08

    Answer by @Larry Cai covers one part to write a file to slave node from System Groovy Script (as it runs on Master Node).

    The part I am answering is "Some time i see people use "manager" object, how can i get access to it " This is the object already available in Post Build Groovy Script for accessing a lot of things like environment variables, Build Status, Build Display Name etc.

    Quoted from https://wiki.jenkins-ci.org/display/JENKINS/Groovy+Postbuild+Plugin :

    "The groovy script can use the variable manager, which provides various methods to decorate your builds. Those methods can be classified into whitelisted methods and non-whitelisted methods."

    To access it, we can directly call it in the post build groovy script. e.g

    manager.build.setDescription("custom description") manager.addShortText("add your message here")

    All methods available on manager objects are documented here.

    https://wiki.jenkins-ci.org/display/JENKINS/Groovy+Postbuild+Plugin#GroovyPostbuildPlugin-Whitelistedmethods

    0 讨论(0)
  • 2020-12-08 11:13

    The manager object is not available depending on how the groovy is invoked. e.g. in "execute system groovy script".

    You can find the BadgeManager class in jenkins GroovyPostBuild plugin API here: https://javadoc.jenkins.io/plugin/groovy-postbuild/org/jvnet/hudson/plugins/groovypostbuild/GroovyPostbuildRecorder.BadgeManager.html#addShortText-java.lang.String-

    ANSWER: Import the GroovyPostBuild plugin and create a new manager object. e.g. here a job with "Execute System Groovy Script" create a manager object and call the addShortText method:

    // java.lang.Object
    // org.jvnet.hudson.plugins.groovypostbuild.GroovyPostbuildRecorder.BadgeManager
    // Constructor and Description
    // BadgeManager(hudson.model.Run<?,?> build, hudson.model.TaskListener listener, hudson.model.Result scriptFailureResult) 
    
    import hudson.model.*
    import org.jvnet.hudson.plugins.groovypostbuild.GroovyPostbuildAction
    
    def build = Thread.currentThread().executable
    
    manager = new org.jvnet.hudson.plugins.groovypostbuild.GroovyPostbuildRecorder.BadgeManager(build, null, null)
    manager.addShortText("MANAGER TEST", "black", "limegreen", "0px", "white")
    

    This question gives a hint: See here for a nearly working answer: In jenkins job, create file using system groovy in current workspace org.jvnet.hudson.plugins.groovypostbuild.GroovyPostbuildAction and build.getActions().add(GroovyPostbuildAction.createShortText(text, "black", "limegreen", "0px", "white"));

    0 讨论(0)
  • 2020-12-08 11:28

    I suspect the error was caused by the path format, could you try below:

    change

    filename = envVars.get('WORKSPACE') + "\\node_details.txt";
    

    to

    filename = envVars.get('WORKSPACE') + "/node_details.txt";
    

    Because when I tried in my local jenkins server, I get it successfully executed.

    enter image description here

    enter image description here

    0 讨论(0)
提交回复
热议问题