Allows for easy and simple C# interaction with Jenkins.
Via NuGet
PM> Install-Package JenkinsNetClient
First you need a connection to a jenkins server, just pass it your jenkins instance's url.
JenkinsConnection myConn = new JenkinsConnection("myjenkins");
If Jenkins is secuired then you will have to provide a username and api token:
JenkinsConnection myConn = new JenkinsConnection("myjenkins", "username", "apitoken");
We can get a list of Views and Jobs from a JenkinsServer
var myJenkins = new JenkinsServer(myConn);
List<JenkinsView> views = myJenkins.Views();
List<JenkinsJob> jobs = myJenkins.Jobs();
We can easily create a new view ...
var newView = new JenkinsView(myConn, "hudson.model.ListView", "Name of my new view");
if(newView.Exists())
{
throw new exception("View already exists on jenkins");
}
newView.Create(); // Create on jenkins
if(!newView.Exists())
{
throw new exception("View doesn't exist on jenkins - Create must have failed");
}
... or delete an existing view
JenkinsView existingView = views[0];
existingView.Delete(); // Delete from jenkins
if(existingView.Exists())
{
throw new exception("View still exists on jenkins - delete failed");
}
var newJob = new JenkinsJob(myConn, "hudson.model.FreeStyleProject", "name of new job");
if(newJob.Exists())
{
throw new exception("job already exists on jenkins");
}
newJob.Create(); // Create job on jenkins
if(!newJob.Exists())
{
throw new exception("job doesn't exist on jenkins after call to create");
}
newJob.Delete();
if(newJob.Exists())
{
throw new exception("job still exists on jenkins after call to delete");
}
Adding an existing job to a view ...
JenkinsJob existingJob = Jobs[0];
JenkinsView existingView = views[0];
if(!existingView.Contains(existingJob))
{
existingView.Add(existingJob);
}
if(!existingView.Contains(existingJob))
{
throw new exception("Add failed");
}
... and removing it again.
existingView.Remove(existingJob);
if(existingView.Contains(existingJob))
{
throw new exception("job still on view - remove failed");
}
Getting the config xml for a job
JenkinsJob existingJob = Jobs[0];
string configXml = existingJob.Config;
and setting it
JenkinsJob existingJob = Jobs[0];
existingJob.Config = xml