Creating Jenkins node using REST API

Jenkins

Jenkins is a very powerful CI Server that allows to do a lot of interesting things.

But installing it or its slaves could be boring 🙂 I wanna start sharing with you a scripts to automate these things.

Today’s post will be about creating ф simple Jenkins node using Powershell, but you can reimplement it on other languages.

Firstly, we need a groovy script, that Jenkins can execute.

Lets break this on parts:

This block will import all needed classes and namespaces to Jenkins shell.
Lets create an object that will represent the node (All fields could be seen in comments):

And the final step is creating a node:

All vars that starts with $ sign are Powershell vars, that you should set before.

You can check this script by running it with Jenkins console (http://your-jenkins-server.com/script), but replace vars with actual values 🙂

The next step, we will create Powershell function, that will generate this script for us:

Next step, is make REST call to Jenkins to create node:

Where $apiToken – your Jenkins user’s API token that you can find under current user settings in Jenkins.

Also, it would be nice to have function, that will check if already created such node

This one is very simple – just check if GET request to node page returns OK result.

And finaly, lets combine it in one function

That’s it! Thanks for your attention!

Leave a Reply