preparation

Create a Git project.

Here I create a SpringBoot HelloWorld project using IDEA.

Local Idea ensures that the code can be pushed/pulled normally.

Run Jenkins.

Installing a plug-in

Maven build

Install Publish Over SSH, which publishes the project to the specified server once the build is complete.

Configuring global SSH

Go back to Jenkins console, click System Administration > System Configuration, find Publish Over SSH, and fill in host name USERNAME password according to your own server configuration.

If no, click the Add button.

After the configuration is complete, click Test Configuration to test. If success is displayed, the current server is correctly configured and can be connected.

Create a task

A new task

Here, because the Maven plug-in is installed, there is an additional option: build a Maven project

Task allocation

There are several main configurations.

  1. Git source configuration, fill in the git address of the current project, private projects need to configure authorization.

2. Build the environment configuration

3. Build configuration

Goal and Options fill in the project build command,

4. Post Steps

Select Run Only if build succeeds and add the Post step, select Send Files or Execute Command over SSH, and configure the following parameters.

Name: select SSH Server Source files: files to be pushed. Remove prefix: prefix to be removed from the file path. Remote directory: Exec command: script to be executed on the target serverCopy the code

The start.sh content is as follows:

# Jar package location after Jenkins compiled, Work in Jenkins directory JAR_PATH = / root /. Jenkins/workspace/Jenkins - Java/target position # custom jars DIR = / usr/local/API # # jar package name JARFILE= Jenkins -demo-0.0.1 -snapshot. jar if [! -d $DIR/backup]; then mkdir -p $DIR/backup fi ps -ef | grep $JARFILE | grep -v grep | awk '{print $2}' | xargs kill -9 if [ -f $DIR/backup/$JARFILE ]; then rm -f $DIR/backup/$JARFILE fi mv $JAR_PATH/$JARFILE $DIR/backup/$JARFILE java -jar $DIR/backup/$JARFILE > out.log &  if [ $? = 0 ]; then sleep 30 tail -n 50 out.log fi cd $DIR/backup/ ls -lt|awk 'NR>5{print $NF}'|xargs rm -rfCopy the code

The ps: start.sh file may fail to be executed, and you need to grant the permission to execute the file.

chmod 777 start.sh

Click Save. Build the jar file immediately. After the build is successful, check whether the jar file exists in the configuration server path and the port can be accessed successfully.

If all goes well, the deployment is successful.