N
N
Nikolay Baranenko2021-04-12 22:57:27
Java
Nikolay Baranenko, 2021-04-12 22:57:27

How to correctly write a file in the attached class method to the main Jenkinsfile?

I have a groovy class in which in the method I am trying to write a file

print("Loaded class MyClass.groovy")

public class Prepare {

    private List service1 = ["dev-app-0001","dev-app-0002","dev-app-0003"];
    private List service2 = ["dev-app-0004","dev-app-0005","dev-app-0006","dev-app-0007"]

    String GetHosts() {
        List result = this.service1+this.service2
        return ("[allsystems]\n"+result.join('\n')+"\n").toString();
    }

}

String PrepareHosts() {
    Prepare pr = new Prepare();
//    pr.setPath(Path)
    pr.GetHosts();

    Object hosts = new File("/data/jenkins/workspace/example1/update_sshkeys/hosts");
//    hosts.createNewFile();
    hosts.write(pr.GetHosts())
    return pr.GetHosts();
}

return this


when run, an error is returned for the absence of a file or folder
java.io.FileNotFoundException: /data/jenkins/workspace/example1/update_sshkeys/hosts (No such file or directory)
at java.io.FileOutputStream.open0(Native Method)

, although the folder is from -under the jenkins user is writable

in the Main Jenkinsfile the call looks like this

stage('Git clone') {
            checkout scm
            script {
                def hosts = load "MyClass.groovy"
                print "Result " + hosts.PrepareHosts();
                writeFile file: "hosts", text: hosts.PrepareHosts()
            }
        }


this operation
writeFile file: "hosts", text: hosts.PrepareHosts()

I want to take out from the main Jenkinsfile

How to solve this problem?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question