java调用jenkins-client实现添加任务

  • java调用jenkins-client实现添加任务已关闭评论
  • 879 views
  • A+
所属分类:未分类

上篇文章,我们讲了一下通过jenkins-client连接jenkins,下来我们看看怎么添加任务以及中间遇到的问题。

我这边是用maven项目测试,通过git获取代码编译,最后查出来编译后的包

代码如下:

public static Boolean createJob(String jobName,String config) {
    boolean flag = false;

    //创建连接

    JenkinsServer jenkinsServer = JenkinsClient.getInstance().getConnection();
    try {
        jenkinsServer.createJob(jobName, config);
        jenkinsServer.close();
        flag = true;
    } catch (IOException e) {
        LOG.error("创建任务失败", e);
    }

    return flag;
}

其中,config的内容如下:

<?xml version='1.1' encoding='UTF-8'?>
<maven2-moduleset plugin="maven-plugin@3.0">
    <actions/>
    <description>xubotestjavaapi</description>
    <keepDependencies>false</keepDependencies>
    <properties/>
    <scm class="hudson.plugins.git.GitSCM" plugin="git@3.7.0">
        <configVersion>2</configVersion>
        <userRemoteConfigs>
            <hudson.plugins.git.UserRemoteConfig>
                <url>http://xubo-iri@10.100.14.253/xa-service/ossign/all.git</url>
                <credentialsId>e33be950-4969-44ed-9e56-d1806394cc72</credentialsId>
            </hudson.plugins.git.UserRemoteConfig>
        </userRemoteConfigs>
        <branches>
            <hudson.plugins.git.BranchSpec>
                <name>*/master</name>
            </hudson.plugins.git.BranchSpec>
        </branches>
        <doGenerateSubmoduleConfigurations>false</doGenerateSubmoduleConfigurations>
        <submoduleCfg class="list"/>
        <extensions/>
    </scm>
    <canRoam>true</canRoam>
    <disabled>false</disabled>
    <blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
    <blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
    <triggers/>
    <concurrentBuild>false</concurrentBuild>
    <rootModule>
        <groupId>com.qiku.ossign</groupId>
        <artifactId>ossign-all</artifactId>
    </rootModule>
    <aggregatorStyleBuild>true</aggregatorStyleBuild>
    <incrementalBuild>false</incrementalBuild>
    <ignoreUpstremChanges>false</ignoreUpstremChanges>
    <ignoreUnsuccessfulUpstreams>false</ignoreUnsuccessfulUpstreams>
    <archivingDisabled>false</archivingDisabled>
    <siteArchivingDisabled>false</siteArchivingDisabled>
    <fingerprintingDisabled>false</fingerprintingDisabled>
    <resolveDependencies>false</resolveDependencies>
    <processPlugins>false</processPlugins>
    <mavenValidationLevel>-1</mavenValidationLevel>
    <runHeadless>false</runHeadless>
    <disableTriggerDownstreamProjects>false</disableTriggerDownstreamProjects>
    <blockTriggerWhenBuilding>true</blockTriggerWhenBuilding>
    <settings class="jenkins.mvn.FilePathSettingsProvider">
        <path>/qiku/software/maven-3.3.9/conf/settings.xml</path>
    </settings>
    <globalSettings class="jenkins.mvn.FilePathGlobalSettingsProvider">
        <path>/qiku/software/maven-3.3.9/conf/settings.xml</path>
    </globalSettings>
    <reporters/>
    <publishers/>
    <buildWrappers>
        <org.jenkinsci.plugins.credentialsbinding.impl.SecretBuildWrapper plugin="cr
edentials-binding@1.16">
            <bindings class="empty-list"/>
        </org.jenkinsci.plugins.credentialsbinding.impl.SecretBuildWrapper>
    </buildWrappers>
    <prebuilders/>
    <postbuilders>
        <hudson.tasks.Shell>
            <command>ls -al  ossign-api/target/ossign.war</command>
        </hudson.tasks.Shell>
    </postbuilders>
    <runPostStepsIfResult>
        <name>FAILURE</name>
        <ordinal>2</ordinal>
        <color>RED</color>
        <completeBuild>true</completeBuild>
    </runPostStepsIfResult>
</maven2-moduleset>

 

xml属性具体我就不多说了,执行创建任务的程序后,报错如下:

org.apache.http.client.HttpResponseException: No valid crumb was included in the request

查看jenkins日志,如下:

六月 29, 2018 2:17:04 下午 hudson.security.csrf.CrumbFilter doFilter
警告: No valid crumb was included in request for /ajaxExecutors by admin. Returning 403.

解决方法:

原因是jenkins设置的安全策略,防止跨域攻击,去掉

java调用jenkins-client实现添加任务

 

 

测试如下:

public static void main(String[] args) {
    String str = FileUtils.readToStr("E:\\mywork\\devops-all\\src\\main\\resources\\config.xml");
    Boolean flag=createJob("xxxxuuuubbbb",str);
   System.out.println(flag);
}

然后登录到jenkins管理端,查看任务是否创建成功,如下:

java调用jenkins-client实现添加任务

OK了

  • 安卓客户端下载
  • 微信扫一扫
  • weinxin
  • 微信公众号
  • 微信公众号扫一扫
  • weinxin
avatar