首页 > 解决方案 > 如何解决 Jenkins 测试实例中的插件加载问题

问题描述

我想测试我用 job-dsl 编写的 Jenkins 作业,并且官方 job-dsl 存储库中有一个示例,该示例在版本号方面已过时。因此,我希望实现与示例中相同的功能,但使用更新的版本。

基于示例链接存储库,我创建了以下 build.gradle 文件

apply plugin: 'groovy'

ext {
    jobDslVersion = '1.76'
    jenkinsVersion = '2.190.1'
}

sourceSets {
    jobs {
        groovy {
            srcDirs 'generators'
        }
    }
}

repositories {
    jcenter()
    mavenLocal()
    maven {
        url 'https://repo.jenkins-ci.org/public/'
    }
}

configurations {
    testPlugins {}

    // see JENKINS-45512
    testCompile {
        exclude group: 'xalan'
        exclude group: 'xerces'
    }
}

dependencies {
    compile "org.codehaus.groovy:groovy-all:2.5.7"
    compile "org.jenkins-ci.plugins:job-dsl-core:${jobDslVersion}"

    testCompile 'org.spockframework:spock-core:1.3-groovy-2.5'

    // Jenkins test harness dependencies
    testCompile 'org.jenkins-ci.main:jenkins-test-harness:2.56'
    testCompile "org.jenkins-ci.main:jenkins-war:${jenkinsVersion}"

    // Job DSL plugin including plugin dependencies

    testCompile "org.jenkins-ci.plugins:job-dsl:${jobDslVersion}"
    testCompile "org.jenkins-ci.plugins:job-dsl:${jobDslVersion}@jar"
    testCompile 'org.jenkins-ci.plugins:structs:1.20@jar'
    testCompile 'org.jenkins-ci.plugins:script-security:1.63@jar'
    testPlugins 'org.jenkins-ci.plugins:trilead-api:1.0.5@hpi'


    // plugins to install in test instance

    testPlugins 'org.jenkins-ci.plugins:credentials:2.3.0'
    testPlugins 'org.jenkins-ci.plugins:credentials-binding:1.12'

    testPlugins 'org.jenkins-ci.plugins:extra-columns:1.21'
    testPlugins 'org.jenkins-ci.plugins:mailer:1.20'
    testPlugins 'org.jenkins-ci.plugins:build-monitor-plugin:1.12+build.201809061734'

    testPlugins 'org.jenkins-ci.plugins:junit:1.28'
    testPlugins 'org.jenkins-ci.plugins:matrix-project:1.14'
    testPlugins 'org.jenkins-ci.plugins:groovy:2.2'
    testPlugins 'org.jenkins-ci.plugins:htmlpublisher:1.14'

    //testPlugins 'org.jenkins-ci.plugins:badge:1.8'
    testPlugins 'org.jvnet.hudson.plugins:groovy-postbuild:2.5'
    testPlugins 'org.jenkins-ci.plugins:ssh-credentials:1.18'

}

task resolveTestPlugins(type: Copy) {
    from configurations.testPlugins
    into new File(sourceSets.test.output.resourcesDir, 'test-dependencies')
    include '*.hpi'
    include '*.jpi'

    doLast {
        def baseNames = source.collect { it.name[0..it.name.lastIndexOf('.')-1] }
        new File(destinationDir, 'index').setText(baseNames.join('\n'), 'UTF-8')
    }
}

test {
    dependsOn tasks.resolveTestPlugins
    inputs.files sourceSets.jobs.groovy.srcDirs

    // set build directory for Jenkins test harness, JENKINS-26331
    systemProperty 'buildDirectory', project.buildDir.absolutePath
}

但是后来我得到了以下错误,即使我定义了这个 Trilead API 插件的更新版本。如果添加一些其他插件会失败,因为它们相互依赖,我怀疑出于某种原因Jenkins会加载这些插件的一些默认版本,这就是问题的原因。有没有办法打印出加载的插件版本?或者我应该怎么做才能解决这个问题?

java.io.IOException: SSH Credentials Plugin version 1.18 failed to load.
 - Trilead API Plugin version 1.0.4 is older than required. To fix, install version 1.0.5 or later.
    at hudson.PluginWrapper.resolvePluginDependencies(PluginWrapper.java:922)
    at hudson.PluginManager$2$1$1.run(PluginManager.java:545)
    at org.jvnet.hudson.reactor.TaskGraphBuilder$TaskImpl.run(TaskGraphBuilder.java:169)
    at org.jvnet.hudson.reactor.Reactor.runTask(Reactor.java:296)
    at jenkins.model.Jenkins$5.runTask(Jenkins.java:1118)
    at org.jvnet.hudson.reactor.Reactor$2.run(Reactor.java:214)
    at org.jvnet.hudson.reactor.Reactor$Node.run(Reactor.java:117)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)

标签: jenkinsgradlegroovyjenkins-job-dsl

解决方案


从 Jenkins 脚本控制台获取所有插件的 gradle 样式列表http://jenkinsIp:port/script

在下面输入并运行...

Jenkins.instance.pluginManager.activePlugins.sort { it.shortName }.each { plugin ->     
    def manifest = plugin.manifest
    String groupId = manifest.mainAttributes.getValue('Group-Id')
    String artifactId = manifest.mainAttributes.getValue('Extension-Name')
    String version = manifest.mainAttributes.getValue('Implementation-Version')
    if (groupId && artifactId && version) {
        println "testPlugins '$groupId:$artifactId:$version'"
    }
}

推荐阅读