0

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

penguat
  • 1,337
  • 1
  • 13
  • 25

1 Answers1

1

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
  }
}
Itai Ganot
  • 5,873
  • 18
  • 56
  • 99
  • Thankyou - is there a way I can do that with multiple lines? The config files and scripts I would wish to use span multiple lines. – penguat Jun 14 '17 at 12:49
  • You will find your answer in this SO question: https://stackoverflow.com/questions/7954719/how-can-a-batch-script-do-the-equivalent-of-cat-eof – Itai Ganot Jun 14 '17 at 12:54