从 jenkins 在远程服务器(windows)上运行批处理脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22553588/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 11:28:38  来源:igfitidea点击:

Run batch scripts on a remote server (windows) from jenkins

windowsbatch-filedeploymentantjenkins

提问by ?yvind B

I've got a continuous integration server (Jenkins ) which builds my code (checks for compilation errors) and runs tests and then deploys the files to a remote server (not a war file, but the actual file structure) I do this with a Jenkins plugin which allows me to transfer files via samba, it does this nightly.

我有一个持续集成服务器(Jenkins),它构建我的代码(检查编译错误)并运行测试,然后将文件部署到远程服务器(不是War文件,而是实际的文件结构)我用一个Jenkins 插件允许我通过 samba 传输文件,它每晚都这样做。

Now, what I need to do is run an ant command on the remote server. And after that I need to start the application server on the remote server, the application server is started by running a .bat file from the command line.

现在,我需要做的是在远程服务器上运行 ant 命令。之后我需要在远程服务器上启动应用程序服务器,通过从命令行运行 .bat 文件来启动应用程序服务器。

I'm pretty clueless how to accomplish this, I know Jenkins is capable of running batch commands, but how do I make them run in the context of the server and not the context of the build server?

我对如何实现这一点一无所知,我知道 Jenkins 能够运行批处理命令,但是如何让它们在服务器的上下文中运行而不是在构建服务器的上下文中运行?

回答by Slav

If Jenkins on Windows, remote on *nix, use plink.exe(which is essentially command line PuTTy)

如果 Jenkins 在 Windows 上,远程在 *nix 上,使用plink.exe(本质上是命令行PuTTy

If Jenkins on Windows, remote on Window, use psexec.exe

如果 Jenkins 在 Windows 上,在 Window 上远程,请使用 psexec.exe

If Jenkins on *nix, remote on *nix, use ssh

如果 Jenkins 在 *nix 上,远程在 *nix 上,请使用 ssh

If Jenkins on *nix, remote on Windows, (update 2015-01) Ansible http://docs.ansible.com/intro_windows.htmlhas support for calling Windows commands, eg powershell, from a unix/linux machine, https://github.com/ansible/ansible-examples/blob/master/windows/run-powershell.yml

如果 Jenkins 在 *nix 上,在 Windows 上远程,(更新 2015-01)Ansible http://docs.ansible.com/intro_windows.html支持从 unix/linux 机器调用 Windows 命令,例如 powershell,https:/ /github.com/ansible/ansible-examples/blob/master/windows/run-powershell.yml

Tell me what OSes are involved (both on Jenkins and remote), and I will flash this out further.

告诉我涉及哪些操作系统(在 Jenkins 和远程上),我将进一步说明。

Edit:
The download page for psexec.exelists all command line options. You will want something along the lines of:

编辑:
下载页面psexec.exe列出了所有命令行选项。你会想要一些类似的东西:

psexec \\remotecomputername -u remoteusername -p remotepassword cmd /c <your commands here>
Replace <your commands here>with actual commands as you would execute them from command prompt.

psexec \\remotecomputername -u remoteusername -p remotepassword cmd /c <your commands here>
替换<your commands here>为实际命令,就像从命令提示符执行它们一样。

Note that psexecfirst needs to install a service, and required elevated command prompt/admin remote credentials to do so.
Also, you need to run psexec -accepteulaonce to accept the EULA prompt.

请注意,psexec首先需要安装服务,并且需要提升的命令提示符/管理员远程凭据才能这样做。
此外,您需要运行psexec -accepteula一次才能接受 EULA 提示。

回答by Rann Lifshitz

Following Slav's answer above, here is a simpler solution for Jenkins (*nix) to remote (windows):

按照上面 Slav 的回答,这里是 Jenkins (*nix) 到远程 (windows) 的更简单的解决方案:

  1. Install an SSH server on your remote windows (MobaSSH home editionworked well for me)
  2. Make sure your Jenkins user, on your Jenkins machine, has the required certification to open an SSH connection with your remote (you can simply open a terminal and ssh to your remote once, then accept the certification. Make sure it is saved for the Jenkins user).
  3. You can now add an execute shell build phase in your Jenkins job which can SSH to your remote windows machine.
  1. 在远程 Windows 上安装 SSH 服务器(MobaSSH 家庭版对我来说效果很好)
  2. 确保您的 Jenkins 用户在您的 Jenkins 机器上具有打开与远程的 SSH 连接所需的认证(您只需打开一个终端并 ssh 到您的远程一次,然后接受认证。确保它已为 Jenkins 保存用户)。
  3. 您现在可以在您的 Jenkins 作业中添加一个执行 shell 构建阶段,它可以通过 SSH 连接到您的远程 Windows 机器。

Notes :

注意事项:

  1. The established connection might require some additional work - you might have to set windows environment variables or map network drivers in order for your executed commands or batch files to work properly on your windows machines.
  2. If you wish to run GUI related operations this solution might not be relevant (Following my work on running automation tests which require GUI manipulation).
  3. Using Jenkins SSH plugin is an issue, as seen here.
  1. 建立的连接可能需要一些额外的工作 - 您可能必须设置 Windows 环境变量或映射网络驱动程序,以便您执行的命令或批处理文件在您的 Windows 机器上正常工作。
  2. 如果您希望运行与 GUI 相关的操作,则此解决方案可能不相关(按照我对运行需要 GUI 操作的自动化测试的工作)。
  3. 使用SSH詹金斯插件是一个问题,因为看到这里

回答by ?yvind B

I ended up going with a different approach after trying out psexec.exe for a while.

在尝试了 psexec.exe 一段时间后,我最终采用了不同的方法。

Psexec.exe and copying files over the network was a bit slow and unstable, especially since the domain I work on has a policy of changing password every months (which broke the build).

Psexec.exe 和通过网络复制文件有点慢且不稳定,特别是因对我来说有效的域有一个每月更改密码的策略(这破坏了构建)。

In the end I went with the master/slave approach, which is faster and more stable. Since I don't have to use psexec.exe and don't have to copy files over the network.

最后我选择了主/从方法,它更快更稳定。因为我不必使用 psexec.exe 也不必通过网络复制文件。