欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

jenkins部署时,弹出提示部署不成功 博客分类: 持续集成 jenkinscargodeployContainerException

程序员文章站 2024-03-18 21:11:58
...
公司的生产环境部署应用的时候,报了一个错误

 Deploying [/root/.jenkins/workspace/bqj-open/anne-web-api/target/anne-web-api.war]
ERROR: Build step failed with exception
org.codehaus.cargo.container.ContainerException: Failed to deploy [/root/.jenkins/workspace/bqj-open/anne-web-api/target/anne-web-api.war]
	at org.codehaus.cargo.container.tomcat.internal.AbstractTomcatManagerDeployer.deploy(AbstractTomcatManagerDeployer.java:107)
	at org.codehaus.cargo.container.tomcat.internal.AbstractTomcatManagerDeployer.redeploy(AbstractTomcatManagerDeployer.java:185)
	at hudson.plugins.deploy.CargoContainerAdapter.deploy(CargoContainerAdapter.java:73)
	at hudson.plugins.deploy.CargoContainerAdapter$1.invoke(CargoContainerAdapter.java:116)
	at hudson.plugins.deploy.CargoContainerAdapter$1.invoke(CargoContainerAdapter.java:103)
	at hudson.FilePath.act(FilePath.java:997)
	at hudson.FilePath.act(FilePath.java:975)
	at hudson.plugins.deploy.CargoContainerAdapter.redeploy(CargoContainerAdapter.java:103)
	at hudson.plugins.deploy.DeployPublisher.perform(DeployPublisher.java:61)
	at hudson.tasks.BuildStepMonitor$3.perform(BuildStepMonitor.java:45)
	at hudson.model.AbstractBuild$AbstractBuildExecution.perform(AbstractBuild.java:735)
	at hudson.model.AbstractBuild$AbstractBuildExecution.performAllBuildSteps(AbstractBuild.java:676)
	at hudson.maven.MavenModuleSetBuild$MavenModuleSetBuildExecution.post2(MavenModuleSetBuild.java:1072)
	at hudson.model.AbstractBuild$AbstractBuildExecution.post(AbstractBuild.java:621)
	at hudson.model.Run.execute(Run.java:1760)
	at hudson.maven.MavenModuleSetBuild.run(MavenModuleSetBuild.java:542)
	at hudson.model.ResourceController.execute(ResourceController.java:97)
	at hudson.model.Executor.run(Executor.java:405)
Caused by: org.codehaus.cargo.container.tomcat.internal.TomcatManagerException: FAIL - Deployed application at context path / but context failed to start


公司jenkins部署的版本是2.60.1,用的部署插件是
Deploy to container Plugin,版本是1.10版本,
百度了一下原因,大部分都说要把ContextPath的/ROOT换成/,可惜不是我的原因,我看了下插件的源码,大致思路是通过tomcat的manager-script角色进行部署,可是我通过tomcat的界面部署时可以的,而通过war包部署时的时候报错了,可是远程调试jenkins太麻烦,而且还是线上环境,索性直接根据插件源码,写了个小工具类,测试部署是否成功


public class TestJenkinsCargoDeploy {


    public static final String CHAR_SET="utf8";
    public static void main(String[] args) throws IOException {

        String path="/";
        String version=null;
        String config=null;
        String war=null;
        boolean update=false;
        String tag=null;
        InputStream data = new FileInputStream(new File("C://jenkins.war"));

        StringBuilder buffer = new StringBuilder("/deploy");
        try{
            buffer.append("?path=").append(URLEncoder.encode(path,CHAR_SET));
            if (version != null) {
                buffer.append("&version=").append(URLEncoder.encode(version, CHAR_SET));
            }
            if (config != null) {
                buffer.append("&config=").append(URLEncoder.encode(config.toString(), CHAR_SET));
            }
            if (war != null) {
                buffer.append("&war=").append(URLEncoder.encode(war.toString(), CHAR_SET));
            }
            if (update) {
                buffer.append("&update=true");
            }
            if (tag != null) {
                buffer.append("&tag=").append(URLEncoder.encode(tag, CHAR_SET));
            }
        }catch(Exception ex){

        }

        invoke(buffer.toString(),data);



    }

    public static void  invoke(String path,InputStream data) throws IOException {
        String url="http://localhost:8080/manager/text";
        String userAgent="Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36";
        String username="tomcat";
        String password="s3cret";


        URL invokeURL = new URL(url + path);
        HttpURLConnection connection = (HttpURLConnection)invokeURL.openConnection();
        connection.setAllowUserInteraction(false);
        connection.setDoInput(true);
        connection.setUseCaches(false);
        if (data == null)
        {
            connection.setDoOutput(false);
            connection.setRequestMethod("GET");
        }
        else
        {
            connection.setDoOutput(true);
            connection.setRequestMethod("PUT");
            connection.setRequestProperty("Content-Type", "application/octet-stream");

            connection.setChunkedStreamingMode(0);
        }
        if (userAgent != null) {
            connection.setRequestProperty("User-Agent", userAgent);
        }
        if (username != null)
        {
            String authorization = toAuthorization(username, password);
            connection.setRequestProperty("Authorization", authorization);
        }
        connection.connect();
        if (data != null) {
            pipe(data, connection.getOutputStream());
        }
        String response=null;
        try
        {
            response = toString(connection.getInputStream(), "UTF-8");
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        if (!response.startsWith("OK -")) {
            System.out.println(response);
        }


    }



    private static String toAuthorization(String username, String password)
    {
        StringBuilder buffer = new StringBuilder();
        buffer.append(username).append(':');
        if (password != null) {
            buffer.append(password);
        }
        return "Basic " + new String(Base64.encodeBase64(buffer.toString().getBytes()));
    }


    private static void pipe(InputStream in, OutputStream out)
            throws IOException
    {
        BufferedOutputStream bufferedOut = new BufferedOutputStream(out);

        byte[] bytes = new byte['?'];
        int n;
        while ((n = in.read(bytes)) != -1) {
            bufferedOut.write(bytes, 0, n);
        }
        bufferedOut.flush();
        bufferedOut.close();
        in.close();
    }

    private static String toString(InputStream in, String charset)
            throws IOException
    {
        InputStreamReader reader = new InputStreamReader(in, charset);

        StringBuilder buffer = new StringBuilder();
        char[] chars = new char['?'];
        int n;
        while ((n = reader.read(chars, 0, chars.length)) != -1) {
            buffer.append(chars, 0, n);
        }
        return buffer.toString();
    }
}