7

I would like to have pre-flight checks for my Jenkins pipeline that test whether a certain plugin is installed or not. I found this post Check a plugin exists within a Jenkins Pipeline (Groovy) which asks the same question, but the answers provided are not usable for me, since they test whether a DSL method provided by the plugin is available and not, whether the plugin in general is available or not.

What I would like to have is something like this (in my Jenkinsfile):

pluginAvailable('plugin-name', '0.0.1')

where 0.0.1 might be a (optional) minimum version.

Is there anything like that in the Pipeline DSL or another Jenkins class?

Community
  • 1
  • 1
Michael Lihs
  • 5,664
  • 12
  • 40
  • 67

2 Answers2

3

Had this same thought for a while and put together a helper, just cleaned it up and published an example implementation to github.

@Library('shared-utilities@development') _

pluginDependencies = [
  'pipeline-utility-steps': '',       // installed at any version
  'scm-api': '2.6.3',                 // installed and at version 2.6.3
  'build-timestamp':'^1.0.3',         // installed and at version 1.*
  'warnings':'~5.0.0',                // installed and at version 5.0.*
  'config-file-provider': '>3.6.1',   // installed and greater than 3.6.1
  'pipeline-utility-steps': '>=2.3.0',// installed and greater than or eq
  'workflow-basic-steps': '<2.20',    // installed and less than 2.20
  'maven-plugin': '<=3.4'             // installed and less than or eq 3.4
  ]

assertPluginsInstalled( requiredPlugins: pluginDependencies )

pipeline{
    agent any

    stages{
        stage( 'one' ){
            steps{
                sh "echo 'Running stage after making sure required plugins are installed'"
            }
        }
    }
}

README for the function https://github.com/Perficient-DevOps/jenkins-shared-library/blob/master/vars/assertPluginsInstalled.md

Source https://github.com/Perficient-DevOps/jenkins-shared-library/blob/master/vars/assertPluginsInstalled.groovy

Sean
  • 563
  • 6
  • 9
2

Check out the 2nd answer here - How to get a list of installed jenkins plugins with name and version pair?

  1. create a groovy script for parsing (thanks to malenkiy_scot) Save the following as plugins.groovy:

def plugins = jenkins.model.Jenkins.instance.getPluginManager().getPlugins() plugins.each {println "${it.getShortName()}: ${it.getVersion()}"}

Create a function that accepts a plugin name and version, and iterates over the generated file by the snippet above.

  • Putting this in my **Jenkinsfile** caused a failure with `org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use staticMethod jenkins.model.Jenkins getInstance` – vossad01 Nov 09 '17 at 14:16
  • 1
    @vossad01- you can approve that script from Manage Jenkins. There should be an option there for script approval. – Ilya Chernomorin Nov 09 '17 at 14:34
  • works fine for me - sorry for not getting back on you in such a long time... but today I needed it and... TADAAA - it worked :) – Michael Lihs Nov 29 '17 at 13:09