Number of gerrit-trigger connections keeps growing

Star InactiveStar InactiveStar InactiveStar InactiveStar Inactive
 

When using the gerrit-trigger plugin in jenkins and wanting to configure everything from git I've experienced that the ssh connections to the gerrit server can grow to eventually consume all connections possible when using the groovy-script given as example for setting up the gerrit-trigger plugin. Since the gerrit-trigger does not yet support Jenkins as Code configuration, it must be setup with a groovy JCasC script.

I found a way of solving this connection leakage which was caused by the configuration being reloaded quite often from the helm side-cart.
This is my code section to make it work in a helm deployment of jenkins:

jenkins:
  JCasC:

gerrit-trigger: |
groovy:
- script: >
import jenkins.model.Jenkins;
import net.sf.json.JSONObject;
import com.sonyericsson.hudson.plugins.gerrit.trigger.GerritServer;
if (Jenkins.instance.pluginManager.activePlugins.find { it.shortName == "gerrit-trigger" } != null)
{
    println("JCasC Groovvy: Setting gerrit-trigger server plugin");
    def gerritPlugin = Jenkins.instance.getPlugin(com.sonyericsson.hudson.plugins.gerrit.trigger.PluginImpl.class);
    // Create new or attach to existing server
    def serverName = "my-gerrit";
    def GerritServer server;
    if (gerritPlugin.containsServer(serverName)) {
        server = gerritPlugin.getServer(serverName);
    }
    else {
        println("JCasC Groovvy: Created new gerrit server ${serverName}");
        server = new GerritServer(serverName);
    }
    server.stop();
    def config = server.getConfig();
    config.setGerritHostName("<gerrit-server>")
    config.setGerritSshPort(29418)
    config.setGerritUserName("<gerrit-ssh-user>")
    config.setGerritFrontEndURL("<your-gerrit-url>:8080")
    config.setGerritAuthKeyFile(new File("/var/jenkins_home/.ssh/id_rsa.<gerrit-ssh-user>"))
    config.setGerritEMail("<jenkins-email>")
    config.setNumberOfReceivingWorkerThreads(3);
    config.setNumberOfSendingWorkerThreads(1);
    config.setUseRestApi(false)
    server.setConfig(config);
    gerritPlugin.addServer(server);
    server.start();
    server.startConnection();
    println("JCasC Groovvy: Setting ${serverName} completed");
}