作者:斯巴达克斯 时间:May 22, 2015 分类:攻城狮的烦恼 评论
最近研究使用jenkins来发布项目,使用publish-over-ssh插件,把打包好的zip或者war包上传到远程服务器上,并执行一些命令,本来也没什么问题,但是公司主要业务在腾讯云上,但是腾讯云的服务器并没有外网地址,SSH连接到服务器只能通过腾讯云HTTP代理服务器,并且需要token+服务器的密码,才能连接到服务器。

问题来了 publish-over-ssh插件 并没有设置访问时ssh时通过HTTP代理服务器,如何解决才好?

到jenkins服务器上该插件的lib目录下看看该插件的构成,该插件依赖于jsch-0.1.45.jar这个jar包,刚好对jsch比较熟悉,于是找到publish-over-ssh的源码下载下来,找到ssh连接的代码,加上http代理的代码,加的代码如下:

src\main\java\jenkins\plugins\publish_over_ssh\BapSshHostConfiguration.java

  1. public BapSshClient createClient(final BPBuildInfo buildInfo, final boolean connectSftp) {
  2. final JSch ssh = createJSch();
  3. final Session session = createSession(buildInfo, ssh);
  4. final BapSshClient bapClient = new BapSshClient(buildInfo, session, isEffectiveDisableExec());
  5. try {
  6. final BapSshKeyInfo keyInfo = getEffectiveKeyInfo(buildInfo);
  7. final Properties sessionProperties = getSessionProperties();
  8. Properties properties = new Properties();
  9. // 得到jar包所在的路径
  10. String jarPath = BapSshHostConfiguration.class.getProtectionDomain().getCodeSource().getLocation().getPath();
  11. String sep = File.separator;
  12. int lastIndex = jarPath.lastIndexOf(File.separator);
  13. // 读取配置文件
  14. FileInputStream fileInputStream = new FileInputStream(jarPath.substring(0,lastIndex)+ sep +"configure.properties");
  15. properties.load(fileInputStream);
  16. String enableHttpProxy = properties.getProperty("enable_http_proxy");
  17. String proxyServer = properties.getProperty("http_proxy_server");
  18. String proxyPort = properties.getProperty("http_proxy_port");
  19. if (keyInfo.useKey()) {
  20. setKey(buildInfo, ssh, keyInfo);
  21. sessionProperties.put(CONFIG_KEY_PREFERRED_AUTHENTICATIONS, "publickey");
  22. } else {
  23. session.setPassword(Util.fixNull(keyInfo.getPassphrase()));
  24. sessionProperties.put(CONFIG_KEY_PREFERRED_AUTHENTICATIONS, "keyboard-interactive,password");
  25. }
  26. session.setConfig(sessionProperties);
  27. if ("1".equals(enableHttpProxy)){
  28. session.setProxy(new ProxyHTTP(proxyServer,Integer.parseInt(proxyPort)));
  29. }
  30. connect(buildInfo, session);
  31. if (connectSftp) setupSftp(buildInfo, bapClient);
  32. return bapClient;
  33. } catch (IOException ioe) {
  34. bapClient.disconnectQuietly();
  35. throw new BapPublisherException(Messages.exception_failedToCreateClient(ioe.getLocalizedMessage()), ioe);
  36. } catch (RuntimeException re) {
  37. bapClient.disconnectQuietly();
  38. throw re;
  39. }
  40. }

这个方法其中以下是添加的

  1. Properties properties = new Properties();
  2. // 得到jar包所在的路径
  3. String jarPath = BapSshHostConfiguration.class.getProtectionDomain().getCodeSource().getLocation().getPath();
  4. String sep = File.separator;
  5. int lastIndex = jarPath.lastIndexOf(File.separator);
  6. // 读取配置文件
  7. FileInputStream fileInputStream = new FileInputStream(jarPath.substring(0,lastIndex)+ sep +"configure.properties");
  8. properties.load(fileInputStream);
  9. String enableHttpProxy = properties.getProperty("enable_http_proxy");
  10. String proxyServer = properties.getProperty("http_proxy_server");
  11. String proxyPort = properties.getProperty("http_proxy_port");
  12. if ("1".equals(enableHttpProxy)){
  13. session.setProxy(new ProxyHTTP(proxyServer,Integer.parseInt(proxyPort)));
  14. }

通过maven重新打包成publish-over-0.18.jar,然后重命名为classes.jar,到插件的lib备份原有的classes.jar之后删除,上传新生成的classes.jar到lib目录,下载该文件

classess.zip

然后新建configure.properties文件,里面写上配置,如下:

  1. #### Enable HTTP Proxy 0:disable 1:enable####
  2. enable_http_proxy=1
  3. http_proxy_server=cvm-proxy.opencloud.qq.com
  4. http_proxy_port=80

classes.jar和configure.properties上传之后,位于如下位置,注意自己的jenkins安装位置

然后重启一下jenkins服务器,在添加主机的IP时候,就填上腾讯云服务器的内网地址和端口,用户名是你的appid,密码是服务器的密码,只不过等发布的时候可以临时把token关闭一下,这样就行了。

只不过这里还有点小问题,这里要是jenkins服务器不只给腾讯云服务器发布项目呢,连接其他的服务器时,不需要通过HTTP代理,这时候 看配置文件里面的

  1. enable_http_proxy=1

这一项如果是1,就启用http代理,0 就禁用http代理,所以在使用在把部署文件传到服务器之前把该值修改一下(通过shell命令),就OK了。

至此就大功告成。

参考: https://www.linux178.com/Linux-Trouble/jenkins-deploy-qcloud.html

分类: web

标签:   jenkins