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) }
readFileandshto list the files and then read them? I think you could make this way less complicated.jobDslstep 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.