How can I update managed controller Java Options and Jenkins Options programmatically?

Article ID:360055521551
1 minute readKnowledge base

Issue

  • Don’t know how to add/update Java arguments to Jenkins in CloudBees CI Modern programmatically.

Resolution

You can use different approaches to get these settings updated in your Managed controllers. The options involving the usage of the UI are described in How to add Java arguments to Jenkins on CloudBees CI Modern.

If you want to use a programmatic approach, you can take advantage of the following groovy snippet:

import com.cloudbees.masterprovisioning.kubernetes.KubernetesMasterProvisioning
import com.cloudbees.opscenter.server.model.ManagedMaster
def managed_controller="YOUR_MANAGED_CONTROLLER_NAME"

def instance = jenkins.model.Jenkins.instanceOrNull.getItemByFullName(managed_controller, ManagedMaster.class)
instance.getConfiguration().setJavaOptions("YOUR_JAVA_OPTIONS")
instance.getConfiguration().setJenkinsOptions("YOUR_JENKINS_OPTIONS")
instance.save()

These settings will be applied upon the managed controller restart.

You will have different options to run the snippet above:

Java Options may contain System Properties, they are prefixed with -D. System Properties cannot contain Java Arguments such as -XshowSettings:vm or -XX:*.