0

我正在使用这个 Maven 库连接到 Jenkins:

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

然后我试图在 Jenkins 中获得所有可用的工作,并且通过以下方式在 Jenkins 中获得所有可用的工作:

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()));

现在,我想在 Jenkins 中创建一个作业,从而调用 API 中提供的以下代码:

try {

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

配置.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>

当我通过调用 createJob() 运行这个程序时,我得到了异常:

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)

调用 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)
4

1 回答 1

0

我很晚才回答你的问题。但是发布这个是为了帮助别人。

我花了两个时间研究这个问题,但最终设法使用 jenkins-client 创建了 Jenkins Job。为了测试这一点,我编写了一个简单的 Spring BootCommandLineRunner程序,从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);

`

String getConfigXML()方法返回在 Jenkins 中创建作业所需的示例 config.xml 内联 xml。

我希望它会有所帮助。

于 2020-08-08T01:26:52.120 回答