Jenkins java rest api作成JOB

2147 ワード

Jenkinsバージョン1.6.10
言うまでもなく、もしあなたがまだこのインタフェースを完成していない場合は、次のコードを実行してjenkinsタスクを作成してください.
    @Test
    public void post_xml() throws IOException {
        String server = "127.0.0.1:8080" ;
        String jenkinsHost = "http://"+server+"/" ;
        String projectName = "TEST44" ;
        String configurationFile = "D:\\workspace\\Qone\\target\\classes\\jobTemplate\\demo.xml";

        String username = "xxxxxxxx";
        String apiToken = "xxxxxxxxxxxxxxxxxxxxxxxx" ;
        HttpClient client = new HttpClient();
        client.getState().setCredentials(
                new AuthScope(server,443,"realm"),
                new UsernamePasswordCredentials(username,apiToken)
        );
        client.getParams().setAuthenticationPreemptive(true);
        PostMethod post = new PostMethod(jenkinsHost+"/createItem?name="+projectName);
        post.setDoAuthentication(true);
        File fileInput = new File(configurationFile);
        RequestEntity requestEntity = new FileRequestEntity(fileInput,"text/xml; charset=UTF-8");
        post.setRequestEntity(requestEntity);
        int code = client.executeMethod(post);
        System.out.print(post.getResponseBodyAsString());
    }

https://github.com/RisingOak/jenkins-client   誰かがすでにここでjenkinsの操作APIをカプセル化して、ダウンロードして直接使用することができて、しかしそのタスクを作成する方法は私は通じていないで、私のこちらの問題はcrumbの取得で、この住所は私のjenkinsサービスの上でアクセスすることができなくて、そこで別の道を探します
if (crumbFlag == true) {
            Crumb crumb = get("/crumbIssuer", Crumb.class);
            if (crumb != null) {
                request.addHeader(new BasicHeader(crumb.getCrumbRequestField(), crumb.getCrumb()));
            }
        }

 
Jenkinsユーザー情報を表示し、設定をクリックすると、API TOKENの情報が表示されます.
 
そしてこの文章を見つけたhttps://wiki.jenkins-ci.org/display/JENKINS/Authenticating+scripted+clients
文章には  groovy scriptがタスクを生成する方法は,個人groovyの論理に基づいて上記JAVA実装を書き出す. 
実行結果