Jenkins remotely connects to Windows to execute commands

Environmental Statement

        I installed the Jenkins service on a Linux server as the Jenkins server. In addition, there is a Windows computer with a python environment installed and the python code for automated testing is saved locally. This computer is used to perform chrome-based browsing. Automated web testing of the server. Both machines are on the same network.

        The effect we want to achieve now is to use Jenkins' scheduled build tasks to enable Windows computers to regularly execute automated tests and return results without the need for manual clicks to run.

        To achieve the effect, the first step that needs to be solved is how to connect Jenkins to Windows. As we all know, Windows remote uses the RDP protocol, while Jenkins uses the SSH protocol for file transfer and command transmission, so you must first make Windows support SSH protocol connections.

Windows installation SSH service

        There are many applications for installing SSH services on Windows, such as OpenSSH, freeSSHd, etc. This article uses the example of installing openssh server to enable Jenkins to connect to the windows server through SSH.

        To install OpenSSH on Windows, you can follow another article I wrote, address: https://blog.csdn.net/v781423070/article/details/131322758

Install and configure Jenkins

        For the installation and basic configuration of Jenkins, you can check out another article I wrote at: https://blog.csdn.net/v781423070/article/details/130456211

Jenkins installs Publish Over SSH plugin

Open the Manage Jenkins→Plugins→Available plugins interface, enter SSH in the search box, and install the Publish Over SSH plug-in, as shown below (because I have already installed it, I did not search it). After the installation is completed, restart the Jenkins service to take effect.

Jenkins configure SSH connection

Open the Manage Jenkins→System interface, as shown in the red box below, and find the SSH Servers option under Publish over SSH. (The option will not be displayed if the Publish Over SSH plug-in is not installed)

As shown in the red box below

Name: Connection name, write whatever you want.

Hostname: The IP address of the host to be connected remotely. The IP address of the Windows computer I want to remotely connect to is 172.20.1.144.

Username: remote connection account. If the computer implements SSH service through OpenSSH, the account is the account used for normal windows remote desktop connection, and it is the account used to log in to the computer normally.

Remote Directory: Can be empty or enter /. (Enter /, then it corresponds to the directory corresponding to the user name under the user under the C drive under Windows)

Check the checkbox and fill in the login password: If the computer uses OpenSSH to implement the SSH service, the account password is the account password for the normal Windows Remote Desktop connection, which is the account password for normal login to the computer.

Fill in Port: SSH port of the remote host.

Timeout (ms): The default timeout is 300000.

Others can be left blank and do not need to be filled in. After filling in, click Test Configuration in the lower right corner to test it. If Success appears in the lower left corner, it means the connection is successful and the link is up.

Click the Save button to save.

Jenkins creates tasks to implement

Click [New Item]

Enter a task name, select Freestyle project, and click OK.

Fill in the task description, write whatever you want.

Other configuration items such as [Source Code Management] [Build Triggers] [Build Steps] [Post-Build Operations] will not be described in detail here. Build tasks based on ideas.

The main focus here is [Build Environment], check Send files or execute commands over SSH after the build runs. (If the Publish Over SSH plug-in is not installed, the option will not be displayed)

Name: Select the SSH link name. (Note: What is written here is the link name, not the remote host IP)

In the Exec command input box, enter the command that needs to be executed by the remote Windows host. You need to add "cmd /c" in front of the command, otherwise the command will not be executed. For example, the command in the red box in the picture below means to execute the run.bat file in the C:\Users\YVIEW\PycharmProjects\124\ directory.

Click the Advanced button and change the Exec timeout time to 0 to prevent execution timeout interruptions.

No need to fill in other options, save. The build task is as shown below. Check the log and it shows success. (In fact, automated testing has been automatically executed on Windows computers, and the purpose has been achieved)

What I entered in the Exec command input field is to let the remote machine execute the batch file. The content of the batch file is as shown below. The commands to be operated have been written in the file. In the end, I only need to execute the bat file. The content of the bat file means that you enter the \Users\YVIEW\PycharmProjects\124 directory on the C drive. This directory is one of the project directories of Pycharm, and then execute the man.py file to run the code to perform automated testing.

other problems

In the above practical operation, Windows uses OpenSSH to establish a connection, but I also tried to install freeSSHd to establish an SSH connection. (For the installation of freeSSHd, please see my other article at the address: https://blog.csdn . net/v781423070/article/details/131324571 ), however, I encountered a problem. The connection using the Linux command line was successfully established, but an error was reported in Jenkins, as shown below. The error message should be that there is a problem with the SSH algorithm. There is currently no problem. Find a solution.

Other thoughts on the whole Jenkins environment

        Currently there are only 2 machines, one is a Jenkins server and the other is a Windows test computer. Jenkins remotely connects to Windows and sends and execute remote commands. Windows executes the test and returns the results. This solution solves the problem of manually clicking to run and execute the test. No problem, you can perform tasks regularly.

        However, Jenkins can do much more than that.

        If you upload the test code to Git, download the code from Git to the Jenkins server through Jenkins, then package the code locally on the Jenkins server, then connect to Windows remotely, send the file there, and then execute the remote command, Windows will then execute the automated test and return the results. . This solution not only solves the trouble of manually clicking to run, but also solves the trouble of manually copying to Windows if the test code changes.

        Jenkins can trigger build tasks (pull when code is updated) or polling trigger tasks (pull regularly), which is also very flexible.

Guess you like

Origin blog.csdn.net/v781423070/article/details/131326930