1

I am using this maven library to connect to Jenkins :

<dependency>
  <groupId>com.offbytwo.jenkins</groupId>
  <artifactId>jenkins-client</artifactId>
  <version>0.3.8</version>
</dependency>

Then I tried to get all Jobs available in Jenkins and I am getting all the available jobs in Jenkins by this :

JenkinsServer jServer = new JenkinsServer(new URI(JENKINS_URL), JENKINS_USER_NAME, JENKINS_PASSWORD);
        Map<String, Job> jobs = jServer.getJobs();

        jobs.forEach((k, v) -> {
            System.out.println("Job ->  " + k.toString());
        });

        JobWithDetails job = jobs.get(JENKINS_JOB_NAME).details();
        job.getBuilds().forEach(b -> System.out.println("Build Details : " + b.getNumber()));

Now, I want to create a Job in Jenkins and thus calling below code as provided in API :

try {

            jServer.createJob("test-job", "src/main/resources/config.xml", true);
        } catch (Exception e) {
            e.printStackTrace();
        }

config.xml :

<?xml version="1.0" encoding="UTF-8"?>
<project>
    <actions />
    <description>test job from java api</description>
    <keepDependencies>false</keepDependencies>
    <properties />
    <scm class="hudson.scm.NullSCM" />
    <canRoam>true</canRoam>
    <disabled>false</disabled>
    <blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
    <blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
    <triggers />
    <concurrentBuild>false</concurrentBuild>
    <builders />
    <publishers />
    <buildWrappers />
    <crumb>xxxxxxx</crumb>
    <crumbRequestField>Jenkins-Crumb</crumbRequestField>
</project>

When I run this program by calling createJob(), I am getting exception :

org.apache.http.client.HttpResponseException: Forbidden
    at com.offbytwo.jenkins.client.validator.HttpResponseValidator.validateResponse(HttpResponseValidator.java:11)
    at com.offbytwo.jenkins.client.JenkinsHttpClient.post_xml(JenkinsHttpClient.java:375)
    at com.offbytwo.jenkins.JenkinsServer.createJob(JenkinsServer.java:389)
    at com.offbytwo.jenkins.JenkinsServer.createJob(JenkinsServer.java:359)
    at com.xxx.App.main(App.java:34)

I am also getting exception when calling getJobXml() :

Exception in thread "main" org.apache.http.client.HttpResponseException: Forbidden
    at com.offbytwo.jenkins.client.validator.HttpResponseValidator.validateResponse(HttpResponseValidator.java:11)
    at com.offbytwo.jenkins.client.JenkinsHttpClient.get(JenkinsHttpClient.java:171)
    at com.offbytwo.jenkins.JenkinsServer.getJobXml(JenkinsServer.java:515)
    at com.offbytwo.jenkins.JenkinsServer.getJobXml(JenkinsServer.java:503)
Kumar
  • 955
  • 5
  • 20
  • 50

1 Answers1

1

I am pretty late to answer your question. But posting this to help others.

I spent two much time to look into this issue but finally managed to create Jenkins Job using jenkins-client. In order to test this I wrote a simple Spring Boot CommandLineRunner program calling from main().

   JenkinsServer jenkins = new JenkinsServer(new URI("http://localhost:6565/"), "admin", "admin");
    Map<String, Job> jobs = jenkins.getJobs();
    
    System.out.println("Calling run method " + jenkins.getJob("bp-jenkins1"));
    
    jenkins.createJob("JobOffByTwo", getConfigXML(), true);

`

The String getConfigXML() method returns a sample config.xml inline xml required to create a Job in Jenkins.

I hope that it would help.

Ashish
  • 341
  • 2
  • 7
  • 17