@Grab(group='com.cdancy', module='jenkins-rest', version='0.0.11') import com.cdancy.jenkins.rest.JenkinsClient import com.cdancy.jenkins.rest.domain.common.Error import com.cdancy.jenkins.rest.domain.common.RequestStatus void dealWithErrors(String msg, List errors) { if (errors.size() > 0) { for (Error error : errors) { System.err.println("Exception: " + error.exceptionName()) } throw new RuntimeException(msg) } } // Read the configuration files (previously obtained with curl or other) String folderConfig = new File("folder.xml").text String jobConfig = new File("job.xml").text // Create a client instance JenkinsClient client = JenkinsClient.builder().build() // Create a folder RequestStatus status = client.api().jobsApi().create(null, "folder-1", folderConfig) dealWithErrors("Unable to create folder", status.errors()) println("Folder created successfully") // Create a nested folder status = client.api().jobsApi().create("folder-1", "folder-2", folderConfig) dealWithErrors("Unable to create folder", status.errors()) println("Folder created successfully") // Create a job inside the nested folders status = client.api().jobsApi().create("folder-1/folder-2", "myJob", jobConfig) dealWithErrors("Unable to create myJob", status.errors()) println("Job created successfully") // Clean up status = client.api().jobsApi().delete("folder-1/folder-2", "myJob") dealWithErrors("Unable to delete myJob", status.errors()) status = client.api().jobsApi().delete("folder-1", "folder-2") dealWithErrors("Unable to delete folder-2", status.errors()) status = client.api().jobsApi().delete(null, "folder-1") dealWithErrors("Unable to delete folder-1", status.errors())