Advanced Automated Deployment of windows Service+OpenSSH+SVN on Jenkins

Posted by Jove on Thu, 09 Dec 2021 19:55:57 +0100

1. Introduction

I've written two articles about introducing automated deployment using Jenkins, regardless of whether the enterprise is using it now. Net Framework or. Net Core, whether the enterprise is using git or SVN. In fact, these two articles are usually enough to deliver 80% of the business in the development phase. The disadvantage is that both articles simply set the context

Deployed to a single machine. So when this article explains how to transfer compiled source packages to other servers, it also includes continuous deployment of windows services. After communicating with many friends, most enterprises use relatively conservative technologies and feel it is necessary to talk about them together.

Topic about making CI/CD with windows services. After all, while developing new projects, you have to maintain old ones.

Windows+. Getting started with automated deployment of NetCore+git+IIS on Jenkins.

Windows+. Automated deployment of Net Framework+svn+IIS on Jenkins.

II. Required Environment

1.Publish Over SSH is installed in Jenkins Plugin Management

2.Publish Over FTP Installed in Jenkins Plugin Management

3.windows Build FTP

4.windows Install OpenSSh

3. Install FTP/SSH environment and configure FTP/SSH in Jenkins for construction

1. About installing Publish Over FTP and Publish Over SSH plug-ins in Jenkins, there are not many descriptions here. If you don't know how to install them, you can see the first two articles.

2. Configure FTP accounts in the system configuration of jenkins.

2.1 The name above is any name, remote Directory is the directory on the server, the directory server filled in here must be created in advance. Typically, the server root directory can be written as'/'.

  1. Build FTP on windows, which can be Baidu. Where you notice, the firewall remembers to open ports 21 and 20.

4. Create one using topShelf. Net Framework Service.

5. Create a new free style project, TestWinservice, and configure it as follows:

5.1 Code Pull Configuration


5.2 Build Configuration

5.2. 1 Compile command description

/p:Configuration=Release
/p:VisualStudioVersion=16.0
/p:ExcludeGeneratedDebugSymbol=false 
/p:ExcludeXmlAssemblyFiles=false

5.3 FTP File Transfer Configuration

5.4 Start building and see the results

The picture above clearly shows that the transfer of the past 4 files was successful, but there is a problem at this time. How can the transfer of the past files make it deploy automatically and how can the deployment script be executed remotely? Suddenly shit, I wasn't thoughtful. So preparing for SSH, of course you can also use Web Deploy. I think using Web Deploy is a bit cumbersome, so give up.
Install OpenSSh on 5.5 windows.
Download address: https://github.com/PowerShell/Win32-OpenSSH/releases

Once unzipped, open the directory and execute the command with administrator privileges

powershell.exe -ExecutionPolicy Bypass -File install-sshd.ps1

Set up service auto-start and start service

net start sshd

Remember that firewall rules allow port 22 to be used to test connections with Xshell tools.

5.6 Configuring SSH in Jenkins is the same procedure as configuring FTP in Jenkins, where accounts are used.

Basically, it's similar to configuring FTP without much explanation.

Replace the location where the FTP configuration for the project was built before 5.7 with SSH Server, as shown below.

The script logic executed, and the windows services created with TopShelf, all run the installation as an administrator.

cd C:\WINDOWS\system32
e:
cd E:\publish\ConsoleService
set scrpath=C:\publish\ConsoleService\*.*
set dirpath=E:\publish\ConsoleService
sc query MhgService | findstr /i "STATE">nul
:: If the service is not installed, skip stopping the service
if errorlevel 1 goto NotExist
:: To update windows service
NetFxConsoleService.exe stop
XCOPY %scrpath% %dirpath% /Y /S
NetFxConsoleService.exe start
if not %ERRORLEVEL% geq 0 goto Error:
goto next
:NotExist
XCOPY %scrpath% %dirpath% /Y /S
NetFxConsoleService.exe install
NetFxConsoleService.exe start
:next
echo "MhgService Update Successful!" goto :success
:error
echo "MhgService Update failed!"
:success
echo completion of enforcement,Any key to exit
pause >nul
exit

Note: In the above script, I switched from cd C:\WINDOWS\system32 to Administrator Run Script. The script can be installed on one of my servers, and on another server it will be prompted to install the service as Administrator. The problem has been torn about for a long time. Later, I learned online that administrator privileges can be obtained during script execution. See the script below.

@ECHO OFF
setlocal EnableDelayedExpansion
color 3e
title Service Configuration
  
PUSHD %~DP0 & cd /d "%~dp0"
%1 %2
mshta vbscript:createobject("shell.application").shellexecute("%~s0","goto :runas","","runas",1)(window.close)&goto :eof
:runas

::Script logic

e:
cd E:\publish\ConsoleService

set scrpath=C:\publish\ConsoleService\*.*
set dirpath=E:\publish\ConsoleService
sc query MhgService | findstr /i "STATE">nul
..................

Tested this way, currently only a double-click run on the server can install successfully, and no service installation is seen when executed in Jenkins SSH Server. If someone knows why, they can tell me. In fact, vbs have been tossed around, but only on the server can double-click to run.

4. Summary

In the process of automated deployment of Windows services, I feel that I need to do CI/CD things in Windows, and hair really gets less and less. If you want to play CI/CD, you have the opportunity to use linux,. net core embraces open source. In a word, you can only feel it if you have tossed around.

Topics: Linux Windows git jenkins Cyber Security