使用文件作为 Jenkins JobDSL 的输入

Use files as input to Jenkins JobDSL

我正在尝试使用 Jenkins 的 JobDSL 插件以编程方式创建作业。但是,我希望能够在文件中定义参数。根据分布式构建的文档,这可能是不可能的。任何人都知道我如何实现这一目标?我可以使用 readFileFromWorkspace 方法,但我仍然需要遍历提供的所有文件和 运行 JobDSL x 次。下面的 JobDSL 代码。我挣扎的重要部分是前 15 行左右。

#!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)
}

能够使用这段代码让它工作:

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))

如果其他人在这里寻找一种只读取一个参数文件的简单方法,请使用readFileFromWorkspace(如@CodyK 所述):

def file = readFileFromWorkspace(relative_path_to_file)

如果文件中包含一个名为 your_param 的参数,您可以使用 ConfigSlurper:

读取它
def config = new ConfigSlurper().parse(file)
def your_param = config.getProperty("your_param")