Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Jenkins+GitLab+Msbuild+Python from

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

Preface

The enterprise online environment is .NET Framework, which naturally uses the Windows Server platform. Now the need is to automate the continuous integration of .NET projects, so that every manual release, a machine login, that kind of pain has been known, tedious, inefficient, misoperation, and so on. At present, the combination of .net and Jenkins still has limitations to achieve full automation, the key is the Windows environment, various commands can not be convenient to Linux. The .NET Core might be better. There's nothing we can do. We can do it.

The basic combination is Jenkins + Gitlab + Msbuild. Implement code submission, compilation, and deployment.

Installation environment and process

1) Jenkins is a java product and needs to be installed in JDK8,Windows server 2012 environment.

2) .NET project requires .Net Framework support. Select Framework4.6.1 here. Nuget addresses dependencies. The build tool Msbuild is based on Msbuild under VS2019. All of these are based on Windows server 2012. Just choose server 2008R2, and then build the packaging problem, perplexing for 2 days, and change it decisively.

3) Code repository GitLab. Choose Centos7.6.

4) .net project continuous integration process:

Jenkins operation

1) Jenkins specific installation strategy, fool-like next step. The installation is complete. For download, see the official website: https://jenkins.io/download/, find downlaod for download, where the project is based on .net Freamwork,Jenkins and needs to compile .net programs to facilitate compilation. Choose to install the Windows version.

Here, before installing Jenkins, you need to configure the JDK environment.

2) plug-in installation

After Jenkins is installed, go to system Management-> plug-in Management, and install Msbuild, GitLab, ftp, python and so on.

3) create a new Job

4) Job setting

Set the number of days to retain builds, and the maximum number of builds

Customize the workspace Workspac

5) obtain the Git warehouse code

Specify the Git repository code project branch, which is the test environment and is built using the dev branch.

There are mainly the following configuration items:

Get the code path by Repositories URL:git and follow the ssh protocol.

Credentials: authorizes login information and adds credentials. Here, log in using SSH (user name + private key). You need to log in to Gitlab by configuring SSH with Jenkins first.

Branches to build: specify the branch.

6) build program configuration

After installing the plug-in, go to system Administration-> Global Tool Configuration and find the MSbuild configuration option.

Here Path to MABuild configures MSBuild under VS. Do not use the msbuild tool in the C:\ Windows\ Microsoft.NET\ Framework64\ v4.0.30319\ MSBuild directory.

6.2 create Windows batch command before building, resolve project package dependencies, and configure Nuget.

Nuget needs to be installed locally. The Windows command is as follows: "E:\ Program Files (x86)\ Tools\ nuget.exe" restore "E:\ jenkins-workspace\ APIDataBM-test\ BM.BMData.Web\ BM.BMData.sln"-ConfigFile "C:\ Users\ Administrator\ AppData\ Roaming\ NuGet\ NuGet.Config"-NoCache

Among them, the Nuget restore command is to get the package needed by the project and solve the package dependency problem.

7) build environment configuration

Select "Build a Visual Studio project or solution using MSBuild" to configure as follows:

MSBuild Version: the version previously configured in Global Tool Configuration.

MSBuild Build File: the solution sln file to be built can also be the project .csproj file. Note that they are all relative working paths.

Command Line Arguments:

/ t:Rebuild regeneration

/ p:Configuration=Debug generates Debug mode

/ t:resolveReferences

/ pbloWebProjectOutputDirr = "E:\ Publish-web\ APIData-Test\ web" sln output target directory after construction

/ ppurOutputPath= "E:\ Publish-web\ APIData-Test\ web\ bin" bin directory of the output target

8) Project config configuration

Each environment configuration of the project is centrally stored in a warehouse in Gitlab. Developers need to change configuration changes directly in Gitlab to facilitate unified management and filter some sensitive information. Configuration file security has been increased.

The bat file for Windows batch command is as follows:

@ echo offcd / d E:\ git-config\ config-publicgit checkout devgit pull origin devecho Select the deployed server IP:% 1xcopy E:\ git-config\ config-public\ test\ GroupAPIData\ AB\% 1 E:\ jenkins-workspace\ APIDataBK\ TEST\ AB / exclude:E:\ Python-scripts\ test\ exclude.txt / s / e / h / y@echo offnul

The variable name is expressed differently between Linux and Windows system platform:

Windows:%BUILD_ENV%% variable name%

Linux:$ {BUILD_ENV}, or you can use $BUILD_ENV directly

From the Git Lab repository, the latest configuration file for a branch under pull. Then copy the configuration file in the local git config directory to the project, where the exclude configuration specifies files that do not require copy, such as the hidden .gitkeep file.

Note: to run the Jenkins program, you need to change the permissions of the system administrator account to the local system account (system) by default, otherwise the execution of the git pull command will report a similar permission problem.

9) Parametric construction

If multiple servers are published and built, choose to use project parameterization. Find the "This project is parameterized" item in Jenkins, select "choice parameter", fill in the name (this will be used as a parameter later), option (here use IP according to the actual environment), and describe. As shown below:

Publish the application

After the application is successfully built, the next step is to copy the output directory of the construction project to the corresponding site directory of the application server. Because the target application servers are all windows systems, they cannot be operated through ssh remote commands like the linux system. Currently, there are three solutions, using Ftp to upload directly to the application server, using Rsync under Windows, and Python scripting.

1) Ftp mode

1.1 the Ftp plug-in needs to be installed on Jenkins first. At the same time, the Ftp service is installed and configured on the target application server.

After installing the plug-in, go to system Administration-> Global Tool Configuration and find the FTP repository hosts configuration option.

Porfile Name: pick a name and the Job configuration will be applied later.

Hostname:Ftp hostname (port number defaults to 21)

TimeOut: setting timeout

Root Repository Path:Ftp server follow / directory

User Name:Ftp user name

Password:Ftp password

1.2 Project configuration

Post-build operations-> add post-build steps-> Publish artifacts to FTP

FTP site: configured globally

Source: select the release package path, which is the directory of the project build output.

Remote directory: put to the remote destination path (relative to the Ftp root)

The premise is to first install the FTP service on the target server to be released, build the ftp site of IIS, and install it as a FTP server.

When the configuration is complete, click build on the Jenkins.

2) Rsync mode

2.1 the Windows server environment is configured with the rsync command.

In the construction of Job configuration items in Jenkins, add construction steps-> Execute Windows batch command

2.2 configure rsync

Using the rsync command in Windows server requires the installation and configuration of cwRsync, which has server side and client side. CwRsync Server services are no longer available free of charge on the official website. Install the cwRsync Server,Jenkins server on the application server and configure the client side of the rsync. Specific cwRsync Server and Client installation strategy.

Under the default installation of cwRsync Server, the C:\ Program Files (x86)\ ICW\ rsync.conf file on the cwRsync Server side is configured as follows: uid = 0gid = 0use chroot = falsestrict modes = falsehosts allow = * log file = rsyncd.logport = 873 [APIData] path = / cygdrive/e/Web/APIData-test/websecrets file = / cygdrive/c/Program Files (x86) / ICW/rsyncd.secretslist = trueauth users = rsync_userread only = false

The rsync command on the cwRsync Client side is configured in Jenkins as follows:

"C:\ Program Files (x86)\ cwRsync\ bin\ rsync.exe"-avzP-progress-delete-port=873-password-file= "/ cygdrive/c/Program Files (x86) / cwRsync/passwd.txt" / cygdrive/e/Publish-web/APIData-Test2/web/ rsync_user@10.10.10.53::APIData

When the configuration is complete, click build on the Jenkins.

3) the implementation of Python script

WinRM service is the remote management service of PowerShell under windows server. The Python script can manipulate the windows command line by connecting to the winRM module.

3.1 configure the winRM service

WinRM service operations on the controlled windows server:

To check the winRM service status, the default is not started. There is no result output after the execution of the following command. You need to perform the next steps to enable it.

> winrm enumerate winrm/config/listener

Configure winRM service startup

> winrm quickconfig

View the winrm service listener of windows

Winrm e winrm/config/listener

Winrm service configuration auth

> winrm set winrm/config/service/auth @ {Basic= "true"}

Configure winrm service encryption to allow non-encryption

> winrm set winrm/config/service @ {AllowUnencrypted= "true"}

3.2Configuring python script

In the construction of Job configuration items, add construction steps-> Execute Windows batch command

The Python script is as follows:

Import winrmimport osimport sysimport timeenv = os.getenv ("ENV") print ('choose to publish and build the server ENV is:', env) if env = '10.10.10.10':win2008r2 = winrm.Session (' http://10.10.10.10:5985/wsman',auth=('deploy_user', 'xxxxxx')) ip = win2008r2.run_cmd (' ipconfig | findstr "10.10.10.10"') app_list = win2008r2.run_cmd ('C:\\ Windows\\ System32\\ inetsrv\\ appcmd.exe list app') app_stop = win2008r2.run_cmd ('C:\ Windows\\ System32\\ inetsrv\\ appcmd.exe stop site DataapiBM') backup = win2008r2.run_cmd ('xcopy D:\\ Web-prd\\ DataapiBM D:\\ Web-prd\\ Backup\\ DataapiBM-%date:~8, 2% month% date:~11, 2% day% time:~0, 2% hour% time:~3,2%% time:~6,2% seconds\ / exclude:D:\\ Web-prd\\ backup\\ exclude.txt / ryhs') rsync_code = win2008r2.run_cmd ('C:\\ cwRsync\\ bin\\ rsync.exe-vzrtopg-numeric-ids-progress-port=1873-password-file=/cygdrive/c/cwRsync/passwd.txt

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report