8
votes

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?

2

2 Answers

4
votes

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

3
votes

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.