Create jenkins JLNP slave programmatically
Posted in development on March 15, 2019 by Adrian Wyssmann ‐ 2 min read
Adding a slave to a Jenkins master always required to add the slave via the GUI and then gather the secret in order to be able to connect the slave. So I was wondering whether a slave could be created programmatically.
My initial search brought me to this Stackoverflow post However, I figured you can also create an agent using the REST API. This is especially useful when having an apache proxy in front (see issue JENKINS-47279) and no direct access to the jenkins otherwise (e.g. in a corporate network) where CLI will not work. I recommend to create an API token for this purpose. Then you can do something like this to create a jenkins slave programatically:
Linux (Bash)
In order to get the agent secret via REST API checkout this, which would look something like this:
Windows (PS)
And here my solution for Windows using Powershell:
Hope this is useful for you.