2

I am trying to use Jenkins' JobDSL plugin to programatically create jobs. However, I want to be able to define the parameters in a file. According to docs on distributed builds, this may not be possible. Anyone have any idea how I can achieve this? I could use the readFileFromWorkspace method but I still need to iterate over all files provided and run JobDSL x times. JobDSL code below. The important part I am struggling with is the first 15 lines or so.

#!groovy import groovy.io.FileType def list = [] hudson.FilePath workspace = hudson.model.Executor.currentExecutor().getCurrentWorkspace() def dir = new File(workspace.getRemote() + "/pipeline/applications") dir.eachFile (FileType.FILES) { file -> list << file } list.each { println (it.path) def properties = new Properties() this.getClass().getResource( it.path ).withInputStream { properties.load(it) } def _git_key_id = 'jenkins' consumablesRoot = '//pipeline_test' application_folder = "${consumablesRoot}/" + properties._application_name // Create the branch_indexer def jobName = "${application_folder}/branch_indexer" folder(consumablesRoot) { description("Ensure consumables folder is in place") } folder(application_folder) { description("Ensure app folder in consumables spaces is in place.") } job(jobName) { println("in the branch_indexer: ${GIT_BRANCH}") label('master') /* environmentVariables( __pipeline_code_repo: properties."__pipeline_code_repo", __pipeline_code_branch: properties."__pipeline_code_branch", __pipeline_scripts_code_repo: properties."__pipeline_scripts_code_repo", __pipeline_scripts_code_branch: properties."__pipeline_scripts_code_branch", __gcp_template_code_repo: properties."__gcp_template_code_repo", __gcp_template_code_branch: properties."__gcp_template_code_branch", _git_key_id: _git_key_id, _application_id: properties."_application_id", _application_name: properties."_application_name", _business_mnemonic: properties."_business_mnemonic", _control_repo: properties."_control_repo", _project_name: properties."_project_name" )*/ scm { git { remote { url(control_repo) name('control_repo') credentials(_git_key_id) } remote { url(pipeline_code_repo) name('pipeline_pipelines') credentials(_git_key_id) } } } triggers { scm('@daily') } steps { //ensure that the latest code from the pipeline source code repo has been pulled shell("git ls-remote --heads control_repo | cut -d'/' -f3 | sort > .branches") shell("git checkout -f pipeline_pipelines/" + properties."pipeline_code_branch") //get the last branch from the control_repo repo shell(""" git for-each-ref --sort=-committerdate refs/remotes | grep -i control_repo | head -n 1 > .last_branch """) dsl(['pipeline/branch_indexer.groovy']) } } // Start the branch_indexer queue(jobName) } 
6
  • What is wrong with using readFile and sh to list the files and then read them? I think you could make this way less complicated. Commented Apr 3, 2018 at 17:49
  • Can you run sh outside of job scope? Commented Apr 3, 2018 at 17:55
  • Ok, I think I may have been slightly misunderstanding your question before. It looks like you have a "regular"/freestyle/some other job that runs the Job DSL and it wants to use some local files from the workspace for the Job DSL. Is that correct? Are the local files coming from an SCM? If they are, I think this could be greatly simplified by using the Pipeline job type and running the jobDsl step in there. You won't have to deal with Jenkins internal classes as much and will use established patterns for pipelines. That is just my opinion, though. Commented Apr 3, 2018 at 18:09
  • Didn't want to do that because of the rework involved. I figured it out anyways. Commented Apr 3, 2018 at 18:29
  • Glad you figured it out, sorry for my unhelpful comments! Commented Apr 3, 2018 at 18:30

2 Answers 2

4

In case someone else ends up here in search of a simple method for reading only one parameter file, use readFileFromWorkspace (as mentioned by @CodyK):

def file = readFileFromWorkspace(relative_path_to_file) 

If the file contains a parameter called your_param, you can read it using ConfigSlurper:

def config = new ConfigSlurper().parse(file) def your_param = config.getProperty("your_param") 
Sign up to request clarification or add additional context in comments.

Comments

2

Was able to get it working with this piece of code:

hudson.FilePath workspace = hudson.model.Executor.currentExecutor().getCurrentWorkspace() // Build a list of all config files ending in .properties def cwd = hudson.model.Executor.currentExecutor().getCurrentWorkspace().absolutize() def configFiles = new FilePath(cwd, 'pipeline/applications').list('*.properties') configFiles.each { file -> def properties = new Properties() def content = readFileFromWorkspace(file.getRemote()) properties.load(new StringReader(content)) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.