Gradle从Job-DSL-Plugin生成XML

发布于 2025-02-11 17:14:38 字数 2023 浏览 0 评论 0原文

是否有人设置了Gradle构建以在本地生成工作DSL XML?

所以我为作业和插件依赖性添加了一个源。这样我就可以在课堂上构建开槽。现在,我认为我想添加一个我链接到编译或测试的执行任务,该任务与以下内容相等,但我不确定做到这一点的最佳方法。

我已经可以通过ClassPath(从理论上)访问JAR,因此我应该能够轻松地构建

curl -O https://repo.jenkins-ci.org/public/org/jenkins-ci/plugins/job-dsl-core/@version@/job-dsl-core-@[email protected]
java -jar job-dsl-core-@[email protected] sample.dsl.groovy

构建。

sourceSets {
...
    jobs {
        groovy {
            srcDirs 'jenkins-job-dsl'
            compileClasspath += main.compileClasspath
        }
    }
}

dependencies {
...
compile "org.jenkins-ci.plugins:job-dsl-core:$version"
}

task buildOneUsingClassFile(type: JavaExec) {
    group = "Execution"
    classpath = sourceSets.jobs.runtimeClasspath + sourceSets.jobs.compileClasspath
    main = 'my_pipeline_dsl_job_class_name'
}

task generateDSLXml(type: JavaExec) {
    group = "Execution"
    description = "generate all found groovy dsl files"
    classpath = sourceSets.jobs.compileClasspath
    main = 'javaposse.jobdsl.Run'
    args sourceSets.jobs.groovy
      .filter { it.path.endsWith('.groovy') }
      .collect {it.path.toString() }
      .unique()
}

以上主要有效,除了我看到在Jenkins本身上有效的失败,即使具有相同的版本。但是,我在

Has anyone setup a gradle build to generate Jobs DSL xml locally?

So I added a sourceSet for jobs and the plugin dependency. So I can build the groovy into classes. Now I think I want to add an execution task that I link to compile or maybe test that generates in a gradle equivalent to the following but I'm not quite sure of the best way to do this.

I have access to the jar already via classpath (in theory) so I should be able to exec easily

curl -O https://repo.jenkins-ci.org/public/org/jenkins-ci/plugins/job-dsl-core/@version@/job-dsl-core-@[email protected]
java -jar job-dsl-core-@[email protected] sample.dsl.groovy

build.gradle

sourceSets {
...
    jobs {
        groovy {
            srcDirs 'jenkins-job-dsl'
            compileClasspath += main.compileClasspath
        }
    }
}

dependencies {
...
compile "org.jenkins-ci.plugins:job-dsl-core:$version"
}

task buildOneUsingClassFile(type: JavaExec) {
    group = "Execution"
    classpath = sourceSets.jobs.runtimeClasspath + sourceSets.jobs.compileClasspath
    main = 'my_pipeline_dsl_job_class_name'
}

task generateDSLXml(type: JavaExec) {
    group = "Execution"
    description = "generate all found groovy dsl files"
    classpath = sourceSets.jobs.compileClasspath
    main = 'javaposse.jobdsl.Run'
    args sourceSets.jobs.groovy
      .filter { it.path.endsWith('.groovy') }
      .collect {it.path.toString() }
      .unique()
}

The above mostly works, except that I see failures that work on jenkins itself even with the same version. However, I see those same failures on https://job-dsl.herokuapp.com/ too so they may represent a different issue

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吃→可爱长大的 2025-02-18 17:14:38

gradle

  • 添加为Job-dsl文件添加源设置为
  • Job-dsl ext.jobdsloutputdir添加dir dir
  • add sudd依赖项,用于job-dsl的依赖
  • 项添加工作DSL
buildscript {
    ...
    ext {
    ...
        jobDslOutputDir = new File(buildDir, 'generated/jenkins-job-dsl')
    }

}

sourceSets {
    // our shared library internal code
    main {
        groovy {
            srcDirs = ['src']
        }
        resources {
            srcDirs = ['resources']
        }
    }
    // out shared library test code
    test {
        groovy {
            srcDirs = ['test']
        }
        resources {
            srcDirs = ['resources']
        }
    }
    // job DSL files
    jobs {
        groovy {
            srcDirs 'jenkins-job-dsl'
            compileClasspath += main.compileClasspath
        }
    }
}

dependencies {
    ...
    compile 'org.jenkins-ci.plugins.workflow:workflow-multibranch:2.24@jar'
    compile 'org.jenkins-ci.plugins:job-dsl-core:1.77'
}


// Generate Task
task generateDSLXml(type: JavaExec) {
    doFirst {
        // ensure Project's ext.jobDslOutputDir directory exists
        jobDslOutputDir.mkdirs()
    }

    group = "Execution"
    description = "generate job xml for all found groovy dsl files"
    classpath = sourceSets.jobs.compileClasspath
    main = 'javaposse.jobdsl.Run'
    workingDir = jobDslOutputDir
    // collect all the groovy files found in the jobs source set for they represent
    // the job dsl files creating jobs
    args sourceSets.jobs.groovy.filter { it.path.endsWith('.groovy') }.collect { it.path.toString() }.unique()
}

// disable spotbugs for job DSL
task spotbugs(
        group: "Verification",
        description: """Marker task to enable Spotbugs (disabled by default)."""
)
gradle.taskGraph.whenReady { taskGraph ->
    tasks.spotbugsMain.onlyIf { taskGraph.hasTask tasks.spotbugs }
    tasks.spotbugsTest.onlyIf { taskGraph.hasTask tasks.spotbugs }
    tasks.spotbugsJobs.onlyIf { false } // skip for job dsl
}

Job DSL多分支和多个分支的缺少方法问题,

我们应该能够使用

multibranchPipelineJob('my-build') {
    factory {
        workflowBranchProjectFactory {
            scriptPath('path-to-Jenkinsfile')
        }
    }
}

此问题失败的方法错误。当我们重构发现这里-dsl.herokuapp.com/“ rel =“ nofollow noreferrer”> dsl playground 和通过gradle


        it / factory(class: 'org.jenkinsci.plugins.workflow.multibranch.WorkflowBranchProjectFactory') {
            owner(class: 'org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject', reference: '../..')
            scriptPath("jenkinsfile")
        }
    }

Gradle

  • Add source set for job-dsl files
  • Add output dir for job-dsl ext.jobDslOutputDir
  • Add dependencies for job-dsl
  • Add task for job-dsl
buildscript {
    ...
    ext {
    ...
        jobDslOutputDir = new File(buildDir, 'generated/jenkins-job-dsl')
    }

}

sourceSets {
    // our shared library internal code
    main {
        groovy {
            srcDirs = ['src']
        }
        resources {
            srcDirs = ['resources']
        }
    }
    // out shared library test code
    test {
        groovy {
            srcDirs = ['test']
        }
        resources {
            srcDirs = ['resources']
        }
    }
    // job DSL files
    jobs {
        groovy {
            srcDirs 'jenkins-job-dsl'
            compileClasspath += main.compileClasspath
        }
    }
}

dependencies {
    ...
    compile 'org.jenkins-ci.plugins.workflow:workflow-multibranch:2.24@jar'
    compile 'org.jenkins-ci.plugins:job-dsl-core:1.77'
}


// Generate Task
task generateDSLXml(type: JavaExec) {
    doFirst {
        // ensure Project's ext.jobDslOutputDir directory exists
        jobDslOutputDir.mkdirs()
    }

    group = "Execution"
    description = "generate job xml for all found groovy dsl files"
    classpath = sourceSets.jobs.compileClasspath
    main = 'javaposse.jobdsl.Run'
    workingDir = jobDslOutputDir
    // collect all the groovy files found in the jobs source set for they represent
    // the job dsl files creating jobs
    args sourceSets.jobs.groovy.filter { it.path.endsWith('.groovy') }.collect { it.path.toString() }.unique()
}

// disable spotbugs for job DSL
task spotbugs(
        group: "Verification",
        description: """Marker task to enable Spotbugs (disabled by default)."""
)
gradle.taskGraph.whenReady { taskGraph ->
    tasks.spotbugsMain.onlyIf { taskGraph.hasTask tasks.spotbugs }
    tasks.spotbugsTest.onlyIf { taskGraph.hasTask tasks.spotbugs }
    tasks.spotbugsJobs.onlyIf { false } // skip for job dsl
}

Job DSL multi branch and missing method problems

For multi branch we should be able to use

multibranchPipelineJob('my-build') {
    factory {
        workflowBranchProjectFactory {
            scriptPath('path-to-Jenkinsfile')
        }
    }
}

However this fails with a missing method error. When we refactor thus found here, generation works on dsl playground and via gradle


        it / factory(class: 'org.jenkinsci.plugins.workflow.multibranch.WorkflowBranchProjectFactory') {
            owner(class: 'org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject', reference: '../..')
            scriptPath("jenkinsfile")
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文