Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save marcelbirkner/086c7df5c551a56b03a6 to your computer and use it in GitHub Desktop.
Save marcelbirkner/086c7df5c551a56b03a6 to your computer and use it in GitHub Desktop.
jenkins-job-dsl-write-read-file-test.groovy
/**
* Jenkins Job DSL Groovy Script showing how to write & read files to Jenkins Workspace
*/
import groovy.json.JsonSlurper
import groovy.json.JsonBuilder
import groovy.json.JsonOutput
import hudson.FilePath
import hudson.*
def projects = []
projects.add([projectname: 'maven-project-base', ci: true, deployment: false])
projects.add([projectname: 'maven-project-common', ci: true, deployment: false])
projects.add([projectname: 'maven-project-webapp', ci: true, deployment: true])
def json = JsonOutput.toJson(projects)
println json
hudson.FilePath workspace = hudson.model.Executor.currentExecutor().getCurrentWorkspace()
new File("${workspace}/test.json").write(new JsonBuilder(projects).toPrettyString())
File f = new File("${workspace}/test.json")
def slurper = new JsonSlurper()
def jsonText = f.getText()
println "jsonText ${jsonText}"
projects = slurper.parseText( jsonText )
projects.each {
println it.projectname
}
@Oni1
Copy link

Oni1 commented Nov 22, 2016

How do you use this? Do you integrate this in one of the job dsl configuration or in the seed job?

@Graham42
Copy link

Graham42 commented Apr 3, 2020

Unfortunately this requires allowing new File if using script security which is not a method that should be generally allowed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment