Tutorial diagram of using Jenkins for automated deployment under Windows

Tutorial diagram of using Jenkins for automated deployment under Windows

Today we will talk about how to use Jenkins+powershell script to deploy our .NET CORE script to the corresponding server.

The source code management tool we use here is TFS. Although the source code manager is relatively old, the principles are similar.

1. Install Jre. Since our Jenkins is developed based on Java, you need to install the Java runtime environment on the server first.

The installation process is relatively simple. Download JRE from Baidu and then install it.

Download address: https://www.java.com/en/download/manual.jsp

2. Download and install Jenkins. The server here is Windows, so it is best to download the Windows version. After installation, it will run directly as a Windows service.

The installation process is also very simple, and there are many tutorials online.

https://jenkins.io/download/

3. After installing Jenkins, we install the permission plug-in as needed to configure unused accounts with unapproved permissions (this step is optional)

Please refer to this article for details:

https://www.jb51.net/article/161947.htm

4. The most critical step is how to use Jenkins to automatically deploy our code.

There are several points here. I will post the detailed code below.

1) Get the latest source code in TFS. Here I did not get the code through Jenkins, but pulled the code through the tf command.

The advantage of this is that I can use the same source code for all Jenkins tasks, instead of pulling a separate copy of the code for each task.

2) After pulling the code, it is natural to compile our code through commands. Friends who are familiar with .net should know that there are only a few commands.

3) After the code is released, it is natural to copy the code to our site or Windows service.

This step is also very simple. All you need to do is stop the IIS site and application pool through commands; copy the code; restart and start the corresponding site.

After one set, the job is done.

Here are some screenshots of the key steps:

1.Jenkins creates a free-style task

2. You don’t need to select anything else. As long as we use PowerShell scripts, we need Jenkins to support PowerShell and install plugins.

3. Click Save, and you are done. Click Build Now.

ps: The detailed PowerShell script is given below. It is very simple and I will not go into details here. You will understand it as soon as you see the code.

net stop Test1Job
C:\Windows\System32\inetsrv\appcmd.exe stop site Test1.test.com
C:\Windows\System32\inetsrv\appcmd.exe stop site Test1.api.test.com
C:\Windows\System32\inetsrv\appcmd.exe stop apppool /apppool.name:"test1.test.com"
C:\Windows\System32\inetsrv\appcmd.exe stop apppool /apppool.name:"Test1.api.test.com"
dir "$PSScriptRoot\lib" -Filter "*.ps1" | foreach {
 . $_.fullName
 Write-Host "Import $($_.fullName)"
}
#1. Get the source code D:\test\tools\get_tfs.bat
#2. Compile the source code dotnet build D:\test\Source\MTools\Source-Promotion\Test.API.csproj -nowarn:msb3202,nu1503,cs1591 #--no-restore
echo API OK
dotnet build D:\test\Source\MTools\Source-Promotion\Test.Web.csproj -nowarn:msb3202,nu1503,cs1591 #--no-restore
echo WEB OK
dotnet build D:\test\Source\MTools\Source-Promotion\Test.Job.WindowsService.csproj -nowarn:msb3202,nu1503,cs1591 #--no-restore -vq
echo job OK
Sleep 20
#3. Release CCopy D:\test\Source\MTools\Test.\Source-Promotion\Test.API\bin\Debug\netcoreapp2.1 D:\Test\Test1\api
CCopy D:\test\Source\MTools\Test.\Source-Promotion\Test.Web\bin\Debug\netcoreapp2.1 D:\Test\Test1\web
CCopy D:\test\Source\MTools\Test.\Source-Promotion\Test.Web\wwwroot D:\Test\Test1\web\wwwroot
CCopy D:\test\Source\MTools\Test.\Source-Promotion\Test.Job.WindowsService\bin\Debug\netcoreapp2.1\win-x64 D:\Test\Test1\job
net start Test1Job
C:\Windows\System32\inetsrv\appcmd.exe start site Test1.test.com
C:\Windows\System32\inetsrv\appcmd.exe start site Test1.api.test.com
C:\Windows\System32\inetsrv\appcmd.exe start apppool /apppool.name:"test1.test.com"
C:\Windows\System32\inetsrv\appcmd.exe start apppool /apppool.name:"Test1.api.test.com"
Write-Host "success"
Get-Date
function CCopy($folder_a_path,$folder_b_path)
{
 if(!(test-path $folder_b_path))
 {
 md $folder_b_path
 }
 #Traverse all files in the source folder $folders_a = gci $folder_a_path -Recurse
 foreach ($folder_a in $folders_a)
 {
 if($folder_a.fullname -like "*appsettings.json*")
 {
 continue
 }
 #Get the full path name of the target file by replacing it $b = $folder_a.fullname.replace($folder_a_path,$folder_b_path) 
 #Judge whether the target file exists. If it exists, first judge whether it is new or old If (test-path $b)
 {
  #Judge whether the target is a directory. If it is a directory, skip it. If not, create a first-level empty directory. If (!((gi $b).PSIsContainer))
  {
  #Judge the newness of the target file and the source file. If the modification time of the target file is earlier than that of the source file, copy it again and overwrite it. If ((gci $b).lastwritetime -lt $folder_a.lastwritetime)
  {
  copy-item $folder_a.fullname $b -force
  }
  }
 }
 #If the target file does not exist, copy it directly Else
 {
  copy-item $folder_a.fullname $b
 }
 }
}

After finishing a set of handicrafts, if you have any comments or suggestions, please let us know~

Later, we will further write to publish the program to other servers through PowerShell, and after the release is completed, email the results

to be continued...

Summarize

The above is a tutorial on how to use Jenkins for automated deployment under Windows. I hope it will be helpful to you. If you have any questions, please leave me a message and I will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website!

You may also be interested in:
  • How to install Jenkins on Windows platform
  • Detailed explanation of installing Jenkins in Windows
  • Detailed tutorial on how to deploy Jenkins in Windows environment
  • Modify the Jenkins port number on Windows

<<:  MySQL 5.7.21 installation and password configuration tutorial

>>:  Implementation of Vue 3.x project based on Vite2.x

Recommend

Detailed explanation of 7 SSH command usages in Linux that you don’t know

A system administrator may manage multiple server...

Detailed explanation of Vue routing router

Table of contents Using routing plugins in a modu...

Deployment and configuration of Apache service under Linux

Table of contents 1 The role of Apache 2 Apache I...

Three Vue slots to solve parent-child component communication

Table of contents Preface Environment Preparation...

How to solve the synchronization delay caused by MySQL DDL

Table of contents Preface Solution Tool Introduct...

Solution to mysql server 5.5 connection failure

The solution to the problem that mysql cannot be ...

Simple steps to implement H5 WeChat public account authorization

Preface Yesterday, there was a project that requi...

Vue+Echart bar chart realizes epidemic data statistics

Table of contents 1. First install echarts in the...

Basic knowledge of HTML: a preliminary understanding of web pages

HTML is the abbreviation of Hypertext Markup Langu...

Vue3+TypeScript encapsulates axios and implements request calls

No way, no way, it turns out that there are peopl...

Detailed explanation and extension of ref and reactive in Vue3

Table of contents 1. Ref and reactive 1. reactive...