Remove Jenkins Plugins via Script - jenkins

Does anyone know how I can remove jenkins plugins from a script? I know there is no CLI command that exists for it. But was wondering, maybe you can just delete the plugin folder, and attempt to delete all associated data.
Reason I want this, is I want to develop a script that will run nightly. It will scan each Jenkins server, remove plugins if not found in file stored in GIT, it will add plugins if new ones are added, and upgrade or downgrade other plugins if the version doesn't match. The goal is to keep all Jenkins servers in sync as far as plugins go.
Any thoughts on how I can achieve this goal?

groovy script for removing a plugin:
String pluginNameToRemove = "myPluginToRemove"
def jenkins = Jenkins.getInstance()
def pluginManager = jenkins.getPluginManager()
def pluginWrapperToUninstall = pluginManager.getPlugin(pluginNameToRemove)
pluginWrapperToUninstall.doDoUninstall()
You can also remove the content of the plugins folder, download all the plugins you want according to the file, copy them to the plugins folder and restart jenkins. (We have a very similar flow on our environment for deploying jenkins instances)

Related

Configuring plugins with Jenkins Job Builder

I'm trying to use Jenkins Job Builder to install jenkins plugins, but I misunderstand what JJB can do or I'm doing something wrong. I used the get-plugins-info command to get a YAML description of my plugins. Later, when rebuilding the jenkins installation, I used jenkins-jobs -p plugins_info.yaml jobs in the hopes that JJB would install the plugins listed in the YAML file. But it didn't install the plugins.
So my first question is: should I even expect JJB to install these plugins? The documentation for what JJB is doing with the plugin information is limited, so I'm running on assumptions here.
Assuming JJB is supposed to be installing the plugins in the YAML file, how can I figure out why it's not? I've looked at jenkins' logs to no avail.
I'll start by mentioning that I use Jenkins Job Builder for creating and versioning my jobs. But if you want to install/configure plugins in Jenkins in an automated fashion you can use init.groovy.d scripts that will initialize your jenkins instance. In order to do so create the following directory ${JENKINS_HOME}/init.groovy.d/ then place your groovy scripts in that directory. This is a script that I use to install plugins when I start Jenkins.
import jenkins.model.*
import java.util.logging.Logger
def logger = Logger.getLogger("")
def installed = false
def initialized = false
def plugins = ["git", "cloudbees-folder", "build-timeout"]
logger.info("" + plugins)
def instance = Jenkins.getInstance()
def pm = instance.getPluginManager()
def uc = instance.getUpdateCenter()
plugins.each {
logger.info("Checking " + it)
if (!pm.getPlugin(it)) {
logger.info("Looking UpdateCenter for " + it)
if (!initialized) {
uc.updateAllSites()
initialized = true
}
def plugin = uc.getPlugin(it)
if (plugin) {
logger.info("Installing " + it)
def installFuture = plugin.deploy()
while(!installFuture.isDone()) {
logger.info("Waiting for plugin install: " + it)
sleep(3000)
}
installed = true
}
}
}
if (installed) {
logger.info("Plugins installed, initializing a restart!")
instance.save()
instance.restart()
}
Add as many plugin names to the array plugins. Hope this help you and others out.
JJB has no capability to manage Jenkins Plugins. You will need to look into other tools to handle that for you such as puppet, ansible, etc...
The usage for "get-plugins-info" and the "-p" parameter in the update command is to pass currently installed plugin info to JJB in cases where a system administrator does not want to all JJB "administrator" access permissions in Jenkins to "query" plugin info during the update run. The latest versions of Jenkins no longer allows anonymous querying of plugin info and unfortunately moved that permission to the administrator permission inside of Jenkins.
plugins-info is useful because JJB supports multiple versions of certain plugins and needs to know what the installed version is to appropriately create the XML depending on supported versions.

Artifact Deployer Plugin Alternative

We are using the artifact deployer plugin in a Jenkins freestyle job, and recently Jenkins is displaying warnings about the plugin no longer being safe to use.
This plugin is no longer being distributed according to their wiki site here
Does anyone know of any alternative plugins, or ways in a freestyle job to copy content from one location to another (on same node)
Thanks
To copy all the contents from one directory to another directory in a Linux system use the following command:
cp -a /path/to/source_dir/. /path/to/dest_dir/
You can add an Execute shell step in in your job configuration in Build section and add the above command into it.

Avoid caching of jenkins pipeline scripts

We are using jenkins pipeline and groovy scripts to do automated build pipeline steps.
However, jenkins has decided to cache previous version of these script files and I have yet to figure out how to clear this cache or how to force load the newer version of these scripts.
The scripts are coming from a git repository and executed through a Jenkninsfile bootstrapper loading scripts using the myscript = load "#script/path/to/script.groovy" syntax.
It is these script.groovy files that are not "updated".
Well, we had it sorted out. Someone changed the casing on on of the folders.
Jenkins does not delete the previous folder, but keeps it. Scripts were still pointing to the previous cased folder but jenkins was pulling the updates to the new cased folder.

Jenkins - Updating build changelog during build step

I noticed that if you use Jenkins with the SVN or CVS option, a changelog.xml is created for each build that contains the author and the commit message for that build.
Unfortunately, in my setup, I am not using SVN or CVS, so I am unable to take advantage of the changelog parser. I was wondering if it was possible to create your own changelog with the same format (like the SVN XML changelog) and then point Jenkins to it during the build process. This way, when someone clicks on changes for the build, they'll be able to see what changed and who changed it.
I've tried just creating a changelog.xml and then updating build.xml to use the SVN parser, but two issues that I've noticed:
1) You have to reload configuration files to get it to show up
2) Build.xml doesn't appear to be created until the job is complete
There is some information on the changelog parser, but it doesn't seem that you can just access it during a build step: https://wiki.jenkins-ci.org/display/JENKINS/Change+log
Maybe a system groovy scripts would be a good direction (groovy script plugin). Just add a new script as your build step. You can access your AbstractBuild object by running the following code:
import hudson.model.*
import hudson.util.*
import hudson.scm.*
def thr = Thread.currentThread()
def build = thr?.executable
I'm trying to solve a similar problem currently, but my use case change a bit. I try to copy the changes from Upstream project in the similar way that BlameSubversion plugin does. Unfortunately I can't use the mentioned above SCM plugin because it doesn't work with post-commit-hook, so I have to write my own solution.
Take a look at copyChangeLogFromTriggerJob and copyRevisionFromTriggerJob methods to get know how BlameSubversion does that.
I'm able to copy changes and revision but I'm still fighting with ChangeLogParser.
I would be gladfull for any help as well.

Jenkins nodelabel plugin where does .jpi come from?

I am writing a puppet script that will automatically setup a jenkins instance with the nodelabel parameter plugins. It successfully downloads the .hpi but the plugin still doesn't work.
When I install it using the web UI I see that a working version has both .hpi and .jpi files. I can't find a link for a .jpi anywhere so I am guessing this is generated from the .hpi. I'd like to know if/how this is created during the automated install process so that I can mimic it in puppet.
The .jpi and .hpi files are just zip files (JPI = jenkins plug in, HPI = hudson plug in). If you put the .hpi in your plugins directory (probably /var/lib/jenkins/plugins) and restart Jenkins, it will install the plug in.
What I found was Jenkins-ci.org stores .hpi files, and installing this way will leave them as .hpi files. Installing through the gui changes them to .jpi.
You might also look at the one of the existing puppet modules for Jenkins.

Resources