4

For example, I can write groovy script from IDE like Eclipse and here is the code:

instance = Jenkins.getInstance()
instance.setNumExecutors(2)
println instance.getNumExecutors()

My question is, how can I connect directly from IDE to an running jenkins by providing an jenkins url? Either local or remote jenkins should be enough for me. Just something like:

instance = Jenkins.getInstance("http://localhost:8080", "admin", "admin")
instance.setNumExecutors(2)
println instance.getNumExecutors()

Does any person has this kind of experience ?

mainframer
  • 20,411
  • 12
  • 49
  • 68

1 Answers1

2

In my opinion this does not work. You execute Groovy scripts inside the "Groovy script console" (aka Jenkins script console) which is part of and therefore runs on the server.

If you want to trigger the start locally, you need additionally the Jenkins CLI client which is able to execute Groovy scripts with the groovy command or execute Groovy interactivelly via groovysh on the Jenkins server. In my understanding the CLI sends the script to the server and the server executes it, so you can see your output on the server and you are not able to debug it or run it locally from the IDE.

Another alternative would be to send your groovy script to Jenkins script console with a rest call, see Jenkins Script Console. Same limitations as above.

Another option (which I wouldn't use in your case) is to access the server via the remote api. In this case you can't use the object model and you would code directly on the remote api.

This is a little bit outdated, but check it anyway: Writing programs that drive Jenkins - blog post from Kohsuke Kawaguchi.

ChrLipp
  • 15,526
  • 10
  • 75
  • 107
  • It seems really hard to connect my groovy script to an running jenkins instance from local IDE. But your thoughts are helpful, thanks. – mainframer Jun 28 '16 at 06:00