penguat penguat -4 years ago 331
Groovy Question

Jenkins Pipeline - Create file in workspace (Windows Slave)

For a number of reasons, it would be really useful if I could create a file from a Jenkins pipeline and put it in my workspace. If I can do this, I could avoid pulling in some repositories where I'm currently pulling them in for just one or two files, keep those files in a maintainable place, and I could also use this to create temporary powershell scripts, working around a limitation of the solution described in https://stackoverflow.com/a/42576572

This might be possible through a Pipeline utility, although https://jenkins.io/doc/pipeline/steps/pipeline-utility-steps/ doesn't list any such utility; or it might be possible using a batch script - as long as that can be passed in as a string

Answer Source

You can do something like that:

node (''){
  stage('test'){
    bat """
      echo "something" > file.txt
    """
    String out = readFile(file.txt).trim()
    print out // prints variable out groovy style
    out.useFunction() // allows running functions loaded from the file
    bat "type %out%" // batch closure can access the variable
  }
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download