Search code examples
jenkinsgroovy

How to change new File method in Groovy?


How do I replace the new File method with a secure one? Is it possible to create a python script and connect it?

Part of the code where I have a problem:

def template Name = new File(file: "${template}").normalize.name.replace(".html", "").replace(".yaml", "")

But when I run my pipeline, I get the error

java.lang.SecurityException: Unable to find constructor: new java.io .File java.util.LinkedHashMap 

This method is prohibited and is blacklisted. How do I replace it and with what?


Solution

  • If you're reading the contents of the file, you can replace that "new File" with "readFile". See https://www.jenkins.io/doc/pipeline/steps/workflow-basic-steps/#readfile-read-file-from-workspace

    readFile: Read file from workspace
    Reads a file from a relative path (with root in current directory, usually > workspace) and returns its content as a plain string.
    file : String
    Relative (/-separated) path to file within a workspace to read.
    encoding : String (optional)
    The encoding to use when reading the file. If left blank, the platform default encoding will be used. Binary files can be read into a Base64-encoded string by specifying "Base64" as the encoding.