Bence Szabari
Bence Szabari

Reputation: 155

How to solve plugin loading issues in Jenkins test instance

I'd like to test my Jenkins jobs written in job-dsl, and there is an example in the official job-dsl repository which is outdated regarding to the version numbers. So, I would like achieve the same, as in the example but with newer versions.

Based on the example and linked repository I created the following build.gradle file

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
}

But then I got the following error, even If I defined the newer version of this Trilead API plugin. An also If add some other plugins that will fail because they have dependecies on each other, I suspect for some reason the Jenkins loads some default version of these plugins and that's cause the issue. Is there any way to print out the loaded plugin versions? Or what should I do to fix this?

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)

Upvotes: 1

Views: 1117

Answers (2)

QuiliciCF
QuiliciCF

Reputation: 21

For anyone stumbling on this, here's the Jenkins issue that was opened on that topic: https://issues.jenkins.io/browse/JENKINS-68216

I suggested a fix that works for me there.

Upvotes: 0

Alan Rainford
Alan Rainford

Reputation: 21

To get a gradle style listing of versions for all plugins from Jenkins script console http://jenkinsIp:port/script

Enter below and run...

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'"
    }
}

Upvotes: 2

Related Questions