怎么把项目部署到腾讯云

怎么把项目部署到腾讯云,第1张

使用Jenkins发布腾讯云项目

在最近的科学研究中,使用jenkins来宣布新项目,使用publish-over-ssh插件,将打包的zip或war包上传到远程控制服务器,并实现一些指令。本来是没有问题的,但是企业的关键业务流程是腾讯云服务,但是腾讯云服务的服务器没有外网地址。SSH连接服务器只能根据腾讯云服务的HTTP代理服务器和令牌服务器的登录密码连接服务器。

那么问题来了。在浏览期间,通过ssh发布插件不会为ssh设置HTTP代理服务器。怎么解决?

转到jenkins服务器上插件的lib文件目录,看看插件的组成。插件依赖于jar包jsch-0.1.45.jar,正好比较了解jsch,所以找publish-over-ssh的×××,还有ssh连接的代码,加上http代理的代码。添加的代码如下:


src\main\Java\Jenkins\plugins\publish_over_ssh\bapsshhostconfiguration.Java



public BapSshClient createClient(final BPBuildInfo buildInfo, final boolean connectSftp) {         final JSch ssh = createJSch();         final Session session = createSession(buildInfo, ssh);         final BapSshClient bapClient = new BapSshClient(buildInfo, session, isEffectiveDisableExec());         try {             final BapSshKeyInfo keyInfo = getEffectiveKeyInfo(buildInfo);             final Properties sessionProperties = getSessionProperties();             Properties properties = new Properties();             // 获得jar包所属的途径             String jarPath = BapSshHostConfiguration.class.getProtectionDomain().getCodeSource().getLocation().getPath();             String sep = File.separator;             int lastIndex = jarPath.lastIndexOf(File.separator);             // 载入环境变量             FileInputStream fileInputStream = new FileInputStream(jarPath.substring(0,lastIndex) sep "configure.properties");             properties.load(fileInputStream);             String enableHttpProxy = properties.getProperty("enable_http_proxy");             String proxyServer = properties.getProperty("http_proxy_server");             String proxyPort = properties.getProperty("http_proxy_port");             if (keyInfo.useKey()) {                 setKey(buildInfo, ssh, keyInfo);                 sessionProperties.put(CONFIG_KEY_PREFERRED_AUTHENTICATIONS, "publickey");             } else {                 session.setPassword(Util.fixNull(keyInfo.getPassphrase()));                 sessionProperties.put(CONFIG_KEY_PREFERRED_AUTHENTICATIONS, "keyboard-interactive,password");             }             session.setConfig(sessionProperties);             if ("1".equals(enableHttpProxy)){                 session.setProxy(new ProxyHTTP(proxyServer,Integer.parseInt(proxyPort)));             }             connect(buildInfo, session);             if (connectSftp) setupSftp(buildInfo, bapClient);             return bapClient;         } catch (IOException ioe) {             bapClient.disconnectQuietly();             throw new BapPublisherException(Messages.exception_failedToCreateClient(ioe.getLocalizedMessage()), ioe);         } catch (RuntimeException re) {             bapClient.disconnectQuietly();             throw re;         }     }


下面补充这种方式。


Properties properties = new Properties(); // 获得jar包所属的途径 String jarPath = BapSshHostConfiguration.class.getProtectionDomain().getCodeSource().getLocation().getPath(); String sep = File.separator; int lastIndex = jarPath.lastIndexOf(File.separator); // 载入环境变量 FileInputStream fileInputStream = new FileInputStream(jarPath.substring(0,lastIndex) sep "configure.properties"); properties.load(fileInputStream); String enableHttpProxy = properties.getProperty("enable_http_proxy"); String proxyServer = properties.getProperty("http_proxy_server"); String proxyPort = properties.getProperty("http_proxy_port"); if ("1".equals(enableHttpProxy)){         session.setProxy(new ProxyHTTP(proxyServer,Integer.parseInt(proxyPort))); }



按照maven的说法,再次打包到publish-over-0.18.jar中,然后重命名为classes.jar,备份插件lib中的数据,删除原来的classes.jar,将新形成的classes.jar提交到lib文件目录中,免费下载文件。


classes.jar


然后,创建一个新的configure.properties文件,在其中写入配置,如下所示:

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

提交classes.jar和configure.properties后,它们位于以下位置。注意你的詹金斯安装的地方。

然后重启jenkins服务器。添加服务器的IP时,填写腾讯云服务器的内网地址和端口号。登录名是您的appid,密码是服务器的登录密码。只是你可以在令牌发布的时候暂时关闭它,这样就可以了。

只是这里有点小问题。如果jenkins服务器不仅向腾讯云服务器公布新项目,而且在连接其他服务器时,不需要按照HTTP进行代理会怎样?看看此刻的环境变量。

enable_http_proxy=1

如果该项为1,将打开http代理,0将禁止使用http代理。因此,在应用程序将部署的文档发送到服务器之前,可以更改这个值(根据shell指令)。

就是这样。


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zz/782462.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-04
下一篇 2022-05-04

发表评论

登录后才能评论

评论列表(0条)

保存