Kart
Kart

Reputation: 83

How can I install a plugin using jenkinsfile?

Problem Statement

I am using Jenkins for CI/CD automation. I installed the GitHub Pull Request Builder through Jenkin's Plugin Manager.

plugin manager

However I cannot figure out how to do this with a Jenkins Pipeline

Attempts/Documentation

I have skimmed through this exhaustive list of pipeline steps and declarative documentation but cannot find any documentation on how to accomplish this?

Upvotes: 0

Views: 902

Answers (1)

Chris Maggiulli
Chris Maggiulli

Reputation: 3824

Installing Plugin via Jenkinsfile

Firstly, I would question why exactly you want to do this. This is a fairly odd requirement that I have not seen in my 10 years of Jenkins & Hudson experience. With that said, anything that can be done in the Jenkins web interface, and a billion other things that cannot, can be done in either a Pipeline or in the Script Console because these two constructs have access to the entire SDK.

node {

    // 'ghprb' is this plugins short name value
    GITHUB_PULL_REQUEST_BUILDER = 'ghprb'
    for (plugin in [GITHUB_PULL_REQUEST_BUILDER]) {
        e = Hudson.instance.updateCenter.getPlugin(plugin).deploy().get().getError()
        if (e != null)
            println e.message
    }
}

Security

Anything you see on the Jenkins GUI can be done in the script console or a pipeline via the Jenkins SDK. Jenkins has implemented a Sandbox to prevent nefarious actors from executing malicious scripts. Your requirement must use the Jenkins/Hudson SDK and therefore you have two options:

  • Disable the Sandbox for this pipeline
  • Authorize the scripts and/or methods being used

If you try and run the aforementioned script without solving these security constraints the following exception will be thrown:

org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use method jenkins.model.Jenkins getUpdateCenter

Sandbox

To disable the Sandbox simply uncheck the "Use Groovy Sandbox" checkbox. This is the easiest solution. sandbox

Script Approval

If your administrator does not authorize you to outright disable the Sandbox for that job then you can approve the method signatures used in your script or approve the script itself.

The following message signatures would need to be approved for this script

staticMethod hudson.model.Hudson getInstance
method jenkins.model.Jenkins getUpdateCenter
method hudson.model.UpdateSite$Plugin deploy
method java.util.concurrent.Future get
method hudson.model.UpdateCenter$UpdateCenterJob getError

Additional Information

Most likely the plugin will not be available until a restart has occurred. You can automate a restart a variety of ways. You can hit the following endpoints

  • http://<jenkins.server>/restart
  • http://<jenkins.server>/safeRestart

You could invoke the jenkins-cli.jar tool. If you're feeling exceptionally risky you could even do something like Jenkins.instance.restart()

Note: don't use the SDK to restart unless you really know what you're doing

Upvotes: 1

Related Questions