Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 460
Michael Lihs Avatar asked Apr 19 '17 11:04

Michael Lihs


People also ask

How do I know if Jenkins plugin is installed?

From the Jenkins home page: Click Manage Jenkins. Click Manage Plugins. Click on the Installed tab.

Is Jenkins DSL Groovy?

Jenkinsfiles, using a Domain Specific Language (DSL) based on the Groovy programming language, are persistent files that model delivery pipelines 'as code'.

Is Jenkins pipeline Groovy?

Within a Pipeline Project (read plugin), Jenkins introduces a domain-specific language (DSL) based on 'Groovy', which can be used to define a new pipeline as a script. The flow that would typically require many “standard” Jenkins jobs chained together, can be expressed as a single script.


2 Answers

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

like image 155
Sean Avatar answered Oct 23 '22 17:10

Sean


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.

like image 40
Ilya Chernomorin Avatar answered Oct 23 '22 16:10

Ilya Chernomorin