Skip to content

Instantly share code, notes, and snippets.

@marcelbirkner
Last active February 26, 2026 12:50
Show Gist options
  • Select an option

  • Save marcelbirkner/086c7df5c551a56b03a6 to your computer and use it in GitHub Desktop.

Select an option

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

Oni1 commented Nov 22, 2016

Copy link
Copy Markdown

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

@Graham42

Graham42 commented Apr 3, 2020

Copy link
Copy Markdown

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