Jenkins Automated Deployment (Windows server)

Background:

The SSM project was always deployed. Each time, the project was compressed into a zip package in the Tomcat webapps directory, and then decompressed, overwritten, and started Tomcat in the Tomcat Webapps directory of the Windows server. Every time is like this, very helpless pain. I haven’t been very busy recently. I spent a day doing automatic deployment, but I just automated the whole process.

Use tools: Jenkins + Maven + Git + freesshd (Windows build SSH and SFTP tools) + Tomcat

First, let’s introduce the idea:

  • 1. Local Git gets the latest code
  • 2. Overwrite the necessary files (e.g., spring-shro.xml because the Shiro files used by my development environment are inconsistent with those used by the build environment).
  • 3. Run maven to create a war package
  • 4. Use an SFTP tool to upload the WAR package to the server
  • 5. Manage Tomcat on the server, back up the previous project (depending on the date), and overwrite the new WAR package
  • 6. Start tomcat

This is not the best way to do it, and as you all know, bloggers get the latest code, package it, test it, and deploy it on a server. There is no need to upload the WAR package locally to the server. We’ll try this later.

Now back to business:

Jenkins download and installation.

IO/Download /

To download, you can choose to download war package or Windows MSI format. Downloading the MSI format saves many troublesome steps. And it automatically exists as a Windows service.

To download war, you need to put Jenkins. War in the Tomcat webapps directory.

Msi installation is basically a dumb installation, all the way to next.

Just start Tomcat as Jenkins. War.

Then for the first visit, you need to install some plug-ins. You can choose the default one directly. If you need plug-ins, you can install them separately.

Wait for the plug-in installation to complete

Freesshd installation

SSH service provided by the Windows software, look at this link directly to OK jingyan.baidu.com/article/f7f…

One special note is that when you click User to add, SFTP must be checked because you need to upload local files to a Windows server using SFTP.

Jenkins plugin Publich overSSH

We connect the local Window to the Windows server via the Public overSSh plug-in.

1. Install the plug-in first. Choose System Management > Plug-in Management

Search the optional plugins, find them, install them, and then restart Jenkins

2. Select Publish over SSH from the system Administrator drop-down list

If you fail to click Test, SFTP is not connected. This must be enabled.

Set up

  1. Create a job in Jenkins,

2. Click Advanced to configure the workspace

  1. Click Build here to execute the Windows batch command

F: to enterFdisccd\ intoFPacking listcd software\lowrisk_product\CMS-masterGo to this foldercopy CMS.properties F: \software\lowrisk_product\CMS-master\CMS\build\dev\resources\propertiesOverride filecopy spring-context-shiro.xml F: \software\lowrisk_product\CMS-master\CMS\src\main\resources\springOverride filecd CMSProject foldermvn clean install packagepackagingCopy the code

There is no indication of getting the most recent code from Git, which I actually did through the IDE. Every time I configure Git, I delete the previous one and then clone it, which is very slow, so it doesn’t work.

3.. Add post-build steps to post-build actions in a project by selecting the Send Built Artifact over SSH option

The directory of the remote server refers to:

Scripts on remote servers:

@echo off

set JAVA_HOME=E:\app\jre\jre8
E:
cd \
cd app\appserver\apache-tomcat- 7.0.82 \binClosed: :tomcat
call shutdown.bat
cd.cd ..
::
set SourceFile=E: \app\appserver\apache-tomcat- 7.0.82 \webapps\CMS.war

if exist %SourceFile% (:: Back up the file and delete itcd cms_version:: Deletes all files in the current folderrd /s/q %date: ~ 0, 4% %date: ~ 5, 2% %date:~8 2% :: Create the date foldermkdir %date: ~ 0, 4% %date: ~ 5, 2% %date:~8 2% :: Backup filescopy E: \app\appserver\apache-tomcat- 7.0.82 \webapps\CMS.war %date: ~ 0, 4% %date: ~ 5, 2% %date: ~ 8, 2% \CMS.war:: Delete old filesdel E: \app\appserver\apache-tomcat- 7.0.82 \webapps\CMS.war:: Copies a new filecopy C: \Users\Administrator\app\CMS\CMS.war E: \app\appserver\apache-tomcat- 7.0.82 \webapps
) else(:: Copy the new filecopy C: \Users\Administrator\app\CMS\CMS.war E: \app\appserver\apache-tomcat- 7.0.82 \webapps) :: Delete filesdel C: \Users\Administrator\app\CMS\CMS.war:: Sleep for 40 secondsping 127.0.0.1 -n 40 >nul

E:
cd \
cd app\appserver\apache-tomcat- 7.0.82 \bin: : starttomcat
call startup.bat

Copy the code

4. You can select pin notifier configuration on notifications

Click Finish, Save. Click Build.