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?
From the Jenkins home page: Click Manage Jenkins. Click Manage Plugins. Click on the Installed tab.
Jenkinsfiles, using a Domain Specific Language (DSL) based on the Groovy programming language, are persistent files that model delivery pipelines 'as code'.
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.
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?
- 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With