V Mahaja
V Mahaja

Reputation: 365

Set Jenkins "Global Tool Configuration" properties using script

I am trying to deploy the jenkins pod through docker and configure the Jenkins Jobs through the scripts. Using some of below restAPIs

a) https://<hostname>/scriptText
b) https://<hostname>/job//build
c) https://<hostname>/job/api/json
.
.
and more

However when I start building jobs, its getting failed due to the error ERROR: Unable to install JDK unless a valid username/password is provided.

It needs to change the configurations from Manage Jenkins -> Global Tool Configuration -> JDK -> JDK Installations

But there is no any restAPI available to perform this task.

Is there any way to change the jenkins global configuration programatically?

Upvotes: 1

Views: 3018

Answers (1)

V Mahaja
V Mahaja

Reputation: 365

One solution I found is that, set username and password to download the JDK from oracle site in Global Tool Configuration.

To do it, execute below groovy script using scriptText rest api

set_username_password.groovy

import jenkins.model.*
import hudson.model.*

def inst = Jenkins.getInstance()
def desc = inst.getDescriptor("hudson.tools.JDKInstaller")
#create user account here: http://www.oracle.com/technetwork/java/javase/downloads/index.html
println desc.doPostCredential('<username>','<password>')

Execute the rest api as

curl --user <jk_username>:<jk_password> --data-urlencode "script=$(cat set_username_password.groovy)" -X POST "https://<hostname>/scriptText"

References:

https://github.com/glenjamin/jenkins-groovy-examples/blob/master/README.md

http://javadoc.jenkins.io/archive/jenkins-2.73/hudson/tools/JDKInstaller.DescriptorImpl.html

Upvotes: 1

Related Questions