Skip to content

Instantly share code, notes, and snippets.

@mikejoh
Last active July 11, 2019 14:08
Show Gist options
  • Save mikejoh/b7b1940bd8f977589de91eafa88f492a to your computer and use it in GitHub Desktop.
Save mikejoh/b7b1940bd8f977589de91eafa88f492a to your computer and use it in GitHub Desktop.
Configure the Jenkins logstash plugin

Configure the Jenkins Logstash plugin as code

Not all plugins are compatible with the Configurations as Code plugin, the Logstash plugin can be enabled via CasC but the indexer type cannot be configured. This groovy-hook can be used as a workaround, in this example i've configured the indexer type as Elasticsearch.

Compatibility with CasC can be tracked here.

This script can be added to the init.groovy.d/ directory within Jenkins, scripts added to this directory will be executed in the very end of the Jenkins initialization.

Remember that the URI is hardcoded in the example below, if you have some experience running Jenkins as Code you might already have a property file that you read through for each groovy-script to configure different parts of Jenkins aswell as plugins.

import jenkins.model.Jenkins
import jenkins.plugins.logstash.configuration.*
import jenkins.plugins.logstash.*
import org.apache.http.client.utils.URIBuilder
  
def inst = Jenkins.getInstance()
def desc = inst.getDescriptor("jenkins.plugins.logstash.LogstashConfiguration")

if(! desc.isEnabled()) {
	desc.setEnabled(true)
}

uri = (new URIBuilder("http://elasticsearch:9200/jenkins/jobs").build())

ElasticSearch es = new ElasticSearch()
es.setUri(uri)

desc.setLogstashIndexer(es)
desc.save()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment