Last active
April 3, 2020 16:04
-
-
Save marcelbirkner/086c7df5c551a56b03a6 to your computer and use it in GitHub Desktop.
jenkins-job-dsl-write-read-file-test.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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 | |
} |
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
How do you use this? Do you integrate this in one of the job dsl configuration or in the seed job?