How to test whether a Jenkins Plugin is installed in Pipeline DSL (Groovy)

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


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.