I'm trying to configure the Hockeyapp plugin for Jenkins to take an API token from an environment variable. So far, I've manage to get something that works for the initial load of Jenkins, setting the API token, but if I change the environment variable and reload Jenkins the new token isn't applied.
My understanding of whats happening is my script is creating a new descriptor for Hockeyapp, and saving it - which works on initial Jenkins load as one doesn't exist. On changing the token and rebooting, a descriptor does exist and I'm not changing it, which is why the problem happens from the second boot onwards.
import hockeyapp.*
def env = System.getenv()
def hockeyappConfig = new HockeyappRecorder.DescriptorImpl()
String apiToken = env['HOCKEYAPP_API_TOKEN'] ?: ''
if (apiToken?.trim()) {
hockeyappConfig.setDefaultToken(apiToken)
hockeyappConfig.save()
}
Is anyone able to explain how to get the existing configuration for Hockeyapp via Groovy, so it can be edited, ideally with the code (my Java is not good)?
I think the answer lies somewhere in Jenkins.getInstance()
and then pulling out the Hockeyapp config, but I'm getting a bit out of my depth with unfamiliarity in both Java/Groovy, and programmatic configuration of Jenkins.
Relevant JavaDoc:
The below groovy script worked for me - after using getExtensionList
to get the current config it's possible to change the token.
File: $JENKINS_HOME/init.groovy.d/hockeyapp.groovy
/*
Set the default API token for Hockeyapp in global configuration
Environment Variables:
- HOCKEYAPP_API_TOKEN: Hockeyapp API Token
*/
import hockeyapp.*
import jenkins.model.Jenkins
def env = System.getenv()
Jenkins jenkins = Jenkins.getInstance()
def hockeyappConfig = jenkins.getExtensionList(HockeyappRecorder.DescriptorImpl.class)[0]
String apiToken = env['HOCKEYAPP_API_TOKEN'] ?: ''
if (apiToken?.trim()) {
hockeyappConfig.setDefaultToken(apiToken)
hockeyappConfig.save()
}