使用文件作为Jenkins JobDSL的输入

使用文件作为Jenkins JobDSL的输入,jenkins,groovy,jenkins-pipeline,jenkins-job-dsl,Jenkins,Groovy,Jenkins Pipeline,Jenkins Job Dsl,我正在尝试使用Jenkins的JobDSL插件以编程方式创建工作。但是,我希望能够在文件中定义参数。根据分布式构建的文档,这可能是不可能的。有人知道我怎样才能做到这一点吗?我可以使用readFileFromWorkspace方法,但我仍然需要迭代提供的所有文件并运行JobDSL x次。下面是JobDSL代码。我正在努力解决的重要部分是前15行左右 #!groovy import groovy.io.FileType def list = [] hudson.FilePath workspa

我正在尝试使用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)
}
#!棒极了
导入groovy.io.FileType
def列表=[]
hudson.FilePath workspace=hudson.model.Executor.currentExecutor().getCurrentWorkspace()
def dir=新文件(workspace.getRemote()+“/pipeline/applications”)
dir.eachFile(FileType.FILES){file->

list能够使用这段代码:

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

能够使用以下代码使其正常工作:

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所述):

如果文件包含一个名为
your_param
的参数,则可以使用
ConfigSlurper
读取该参数:

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

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

如果文件包含一个名为
your_param
的参数,则可以使用
ConfigSlurper
读取该参数:

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

使用
readFile
sh
列出文件然后读取它们有什么不对?我想你可以简化这种方式。你能在工作范围之外运行sh吗?好的,我想我以前可能对你的问题有点误解。看起来你有一个“常规”的/freestyle/其他一些运行作业DSL的作业,它希望使用工作区中的一些本地文件来执行作业DSL。是否正确?本地文件是否来自SCM?如果是,我认为通过使用管道作业类型并在其中运行
作业DSL
步骤可以大大简化。您不必处理Jenkins内部类同样多,并且将使用已建立的管道模式。不过,这只是我的意见。由于涉及到返工,我不想这样做。无论如何,我已经找到了答案。很高兴你找到了答案,很抱歉我的评论毫无帮助!使用
readFile
sh
列出文件然后读取它们有什么错?我想你可以让这条路不那么复杂。你能在工作范围之外运行sh吗?好的,我想我以前可能对你的问题有点误解。看起来你有一个“常规的”/freestyle/其他一些运行作业DSL的作业,它希望使用工作区中的一些本地文件来执行作业DSL。是否正确?本地文件是否来自SCM?如果是,我认为通过使用管道作业类型并在其中运行
作业DSL
步骤可以大大简化。您不必处理Jenkins内部类同样多,并且将使用已建立的管道模式。不过,这只是我的意见。由于涉及到返工,我不想这样做。无论如何,我已经找到了答案。很高兴你找到了答案,很抱歉我的评论毫无帮助!