8
头图

image.png

What is backend automation

Back-end automation does not mean automatically generating code, but automatically building projects.

If there is no automation, our back-end workflow from development to testing may be as follows:
1. Write code on the local machine
2. Submit the code, push to the git remote warehouse
3. Perform the mvn clean package unit test and package the jar package
4. Various methods such as ssh/ftp sending packages to the test server
5. Restart the java service
In this process, each step has to be repeated manual operations, which greatly increases the time cost. For the results of unit testing or packaging, there is no automatic feedback mechanism, and the running results need to be manually verified. The final deployment is also to manually log in to the server to execute the script, which is very cumbersome.

introduces automation, the whole process becomes:
1. Write code on the local machine
2. Submit the code, push to the git remote warehouse
3.Git hook triggers the construction of jenkins job
4. Pull the project code in jenkins job and run mvn clean package or mvn clean package -Dmaven.test.skip=true
5. Execute the deployment script of the test server in jenkins job
In the automated process, only step 1 and step 2 require manual operation, and the other steps are automatically run. It is a very standardized process, which reduces the risk of manual operation and eliminates repetitive work.

accomplish

flow chart

image.png

  • Developers submit code to the code base
  • Trigger jenki build task
  • The build is successful, the jar package is sent to the server, the restart script is executed, and the service update is complete
  • Build failure, remind developers of code build failure through DingTalk notification/Enterprise WeChat/Email, etc.

1. Preparation:

Prepare git project: https://github.com/wzc570738205/backendproject.git
Prepare nail group (used to receive CI/CD result notification): 35669766
Ready to install the cloud server here, refer to on the linux server

2. Notification of access code submission

When you push the code to the code base, automatically send a message to Dingding through the git webhook

2.1 Add Dingding Group Robot (Smart Group Assistant)

image.png
Copy webhook, you will use it next
image.png

2.2 Add Dingding webhook to the corresponding webhook corresponding to github

image.png

2.3 Notification of test code submission

Configuration is complete, we submit the code for testing
image.png

far, the code submission notification configuration is complete

3. Access project CI

After we submit the code, we need to check whether there are fatal errors in the code merge. Here we take a simple method to allow mvn clean package . If there is no error, then the submission is passed. Failure means that the code has a fatal error and needs to be modified

3.1 Jenkins new project

Install plug-in Maven Integration plugin

image.png

  • Create a job to build a maven project
  • image.png
  • The source code manager selects git, and fills in the address and credentials (create a new one if not)
  • Build trigger selection: GitHub hook trigger for GITScm polling
    image.png
  • Build environment:
  • image.png

     clean package -Dmaven.repo.local=/var/jenkins_home/.m2/repository -Dmaven.test.skip=true
  • Post-build operation (optional):
    image.png

We use jenkins job execution success and failure to determine whether CI is successful

3.1.1 Test

Submit the code again and successfully trigger the jenkins job
image.png

Successfully built
image.png

3.2 Dingnail notification of access construction status (this is the same as front-end access, and will not be explained here)

  • Configure Dingding custom robot, and select security settings custom keywords, here you can #
    image.png
  • Download the jenkins plug-in DingTalk for nail notification
    image.png
  • Configure plug-in=>System management select Dingding, fill in the custom robot webhook address just now
    image.png
    image.png
  • Turn on robot notifications in the project
    image.png
  • Click to start building and test

    image.png
    Wait for CI to end
    image.png
    Failure reminder
    image.png

far, the CI configuration is over, we have implemented the code submission nail reminder, and the CI notification reminder

4. Access CD

In the above steps, we implemented the CI operation, that is, whether the package of the generation environment can be packaged to suggest whether the code has fatal errors. After this step is passed, we need to send the deployment package to the nginx server

4.1 Send package to server

  • Install the jenkins plugin Publish Over SSH
  • Configure the plug-in in the settings, fill in the server ip, use the password in the advanced, and set the remote server folder /
    image.png
    Click to test connectivity
    image.png
  • Add post-build operation 0614a863775237, and execute the restart service shell Send build artifacts over SSH

image.png

shutdown.sh

#! /bin/shell

# 项目名称
APPLICATION="测试端"

# java路基
JAVA=$(which java)

# 项目启动jar包名称
APPLICATION_JAR="compreDisplay_backend-0.0.1-SNAPSHOT.jar"

PID=$(ps -ef | grep "${APPLICATION_JAR}" | grep -v grep | awk '{ print $2 }')
if [[ -z "$PID" ]]
then
    echo ${APPLICATION} is already stopped
else
    echo kill  ${PID}
    kill -9 ${PID}
    echo ${APPLICATION} stopped successfully
fi

startup.sh

#! /bin/shell

# 项目名称
APPLICATION="测试后端"

# java路基
JAVA=$(which java)

# 项目启动jar包名称
APPLICATION_JAR="compreDisplay_backend-0.0.1-SNAPSHOT.jar"

nohup $JAVA ${JAVA_OPT} -jar /applocation/backend/compreDisplay_backend/${APPLICATION_JAR}

At this point, the CD integration is complete, now only need to submit the code, you can achieve automatic packaging, automatic deployment and restart the service.

Concluding remarks

I will continue to write how to access gitee, gitlab, and svn in the future.

WX20210922-091703.png


雾岛听风
12.1k 声望8.6k 粉丝

丰富自己,胜过取悦别人。