Use git+maven+shell to achieve continuous integration

Foreword: This blog has always been wanted to write, but it has been delayed for half a month because of procrastination, so I will finish writing it tonight.

Requirements: In the past, when working on a project, there was often such a situation. When delivery, there were often changes in requirements and bug fixes. After each online test, if a problem was found, it was necessary to perform actions such as "repair-compile, package-deploy", etc. It's okay to do it once or twice, but it's very tiring to complete such an action every time for a year, especially for deployment, you need to kill -9 tomcat every time, and then start it again. Sometimes it is developed at home, and the typed package is already forty or fifty megabytes. The upload speed is slow and it takes half an hour to upload each time. Every repair and deployment is a rather irritating process.

Knowing "maven in action": Because the main function of using maven in the development process is to add dependencies, because you need to contact the dependencies and aggregation of projects at work, so I downloaded this book and read it. If 100 points are out of 100, then This book can be given 100 points, which is the best and most complete in the maven information I have come across. In the process of learning, I not only knew the relevant knowledge points of maven, but also recognized one thing, that is, using maven for continuous integration.

Continuous Integration: What is Continuous Integration? Ruan Yifeng's article has been described in great detail. For me, the process of continuous integration is the entire "repair-deployment" process mentioned above, that is, the process of continuous integration makes me very irritable. How does maven solve the problem of continuous integration? What about the integration issue? In the book "maven combat", the use of Hudson to automate continuous integration of projects is introduced. What is Hudson? It is the predecessor of jenkins.

Know Jenkins: Encyclopedia - Jenkins is an open source software project, a continuous integration tool developed based on Java, used to monitor continuous repetitive work, and aims to provide an open and easy-to-use software platform, making continuous integration of software into possible. In short, a java project that allows you to automate continuous integration and provide monitoring.... After viewing the relevant information, I immediately downloaded and played. After some contact, I found that continuous integration needs to configure the following through Jenkins:

  • maven
  • git address
  • shell syntax (mainly used to start services)

After looking at it, I found that the configuration method seems to be troublesome, and there is only one server for this project I am working on, so I can only deploy Jenkins on the application server, and then let Jenkins help me complete continuous integration. In general, In order to achieve my needs, then I can draw
write picture description here
the :

  • The role of git is to pull the source code
  • The role of maven is to compile and package
  • The shell is to deploy the application package generated by maven to the server

After reading the above Jenkins process, I was thinking that I can use all these things, so why should I use a project as heavy as Jenkins to consume my server resources? So simply uninstall Jenkins and use git+maven+shell to implement a continuous deployment process. The idea is as follows:
The main idea is:
let the shell help me complete

  • pull code
  • Compile and package
  • Deploy to server

The deployment to the server is simply to copy the software package to the Apps of tomcat, close the tomcat, and then restart it.

So how is the whole step actually done?

1. Install maven Install maven
on your server. There are already many maven installation tutorials on the Internet, so I will not repeat them here. Among them, several points to pay attention to are:
1. Configure maven environment variables, that is, put the /bin directory Configure into environment variables.
2. Configure your maven repository, if you don't want your jar package to be scattered around.
3. Configure the maven Taobao mirror to experience the speed of flying.

2. Install git Install git
on your server. There are already many git installation tutorials on the Internet, so I will not repeat them here. Among them, a few points to pay attention to are:
1. Configure the environment variables of git.
2. Configure ssh, here you can directly refer to the official website tutorial , why do you need to configure ssh here? This is because when we use the git pull command, we need to manually fill in the password. When we use the shell operation, it is more troublesome to simulate the process of entering the password, so we can use the ssh communication method to save the process of entering the password, of course. , when you create a repository to pull the code, you also need to use the ssh access method

3. Test maven and git
First, test git to see if you can pull the code. You can refer to this article here . Some commonly used git commands are recorded, which are very practical.

git remote rm origin
git remote add origin git@gitee.com:jjtHappy/hospital.git
git push origin

After pulling, try to compile and package:

mvn clean package -Dmaven.test.skip=true -Premote

When the execution is successful, you will see the folder where the target is generated in your code base. After entering, you will see the project you packaged.
write picture description here
Seeing this, it has proved that you are not far from success.

Fourth, configure tomcat as a service
Here you can refer to another article I wrote here , why configure tomcat as a service?

  • bootable
  • When you stop tomcat, you can use the service to stop, no need to use kill -9, no need to use ./shutdown.sh, you may say that you want to use ./shutdown.sh, but you need to consider using ./shutdown.sh when you can Can't kill clean process.
  • Service can be started at startup

For the second and third reasons, we need to configure tomcat as a server, so that we can simply use

service tomcat start
service tomcat stop

command to operate tomcat.

Fifth, the shell
is the main event next. The shell is the core of the entire continuous integration, and it is the robot that helps us operate. It mainly completes the following logic:
write picture description here

The next step is to understand the whole logic through shell code:

deploy.sh

#! /bin/bash
logsPath=/home/apps/service/deploy/logs #日志地址
srcPath=/home/apps/software/git-repository/hospital #源码地址
deployPath=/home/apps/service/webapps/hospital #部署路径
function log(){#日志函数
        logPath=$logsPath/deploy.`date +%Y-%m-%d`.log
        if [ ! -d $logsPath  ]; then
                mkdir -p $logsPath
        fi
        if [ ! -d $logPath ];then
                touch $logPath
        fi
        echo [`date +%Y-%m-%d\ %H:%M:%S`] $* >> $logPath;
}

#执行git拉取源码
log 'pull src from github'
cd $srcPath
gitLog=`git pull origin 2>&1` 
if [ $? -ne 0  ];
then 
    log $gitLog
    exit $?
fi
echo "$gitLog" | while read line
do 
    log $line
done

#执行maven进行编译
log 'maven is building'
mavenLog=`mvn clean package -Dmaven.test.skip=true -Premote  2>&1`
if [ $? -ne 0  ];
then 
        log $mavenLog
        exit $?
fi
echo "$mavenLog" | while read line
do 
        log $line
done

#杀死tomcat
log 'stop tomcat service'
tomcatLog=`service tomcat stop 2>&1`

#删除旧部署数据
log 'delete old deploy data'
rm $deployPath/* -rf
log 'copy war to deploy path'

#把代码包拷贝到部署路径
cp $srcPath/target/hospital.war $deployPath/hospital.war
cd $deployPath 
unzip hospital.war > /dev/null

#启动tomcat
log 'start tomcat'
service tomcat start

#检测tomcat进程
TomcatID=$(ps -ef |grep tomcat |grep -w 'tomcat'|grep -v 'grep'|awk '{print $2}')  
log tomcatId is $TomcatID

Execute the script to get the log:

[2017-12-26 10:43:57] pull src from github
[2017-12-26 10:43:59] 来自 gitee.com:jjtHappy/hospital
[2017-12-26 10:43:59] a7f407d..3f9190d master -> origin/master
[2017-12-26 10:43:59] 更新 a7f407d..3f9190d
[2017-12-26 10:43:59] Fast-forward
[2017-12-26 10:43:59] pom.xml | 2 +-
[2017-12-26 10:43:59] 1 file changed, 1 insertion(+), 1 deletion(-)
[2017-12-26 10:43:59] maven is building
[2017-12-26 10:44:10] [INFO] Scanning for projects...
[2017-12-26 10:44:10] [WARNING]
[2017-12-26 10:44:10] [WARNING] Some problems were encountered while building the effective model for com.haizhi:hospital:war:0.0.1-SNAPSHOT
[2017-12-26 10:44:10] [WARNING] 'dependencies.dependency.(groupId:artifactId:type:classifier)' must be unique: org.slf4j:slf4j-log4j12:jar -> duplicate declaration of version 1.6.1 @ line 299, column 15
[2017-12-26 10:44:10] [WARNING] 'dependencies.dependency.scope' for org.springframework.data:spring-data-releasetrain:pom must be one of [provided, compile, runtime, test, system] but is 'import'. @ line 215, column 11
[2017-12-26 10:44:10] [WARNING]
[2017-12-26 10:44:10] [WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[2017-12-26 10:44:10] [WARNING]
[2017-12-26 10:44:10] [WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[2017-12-26 10:44:10] [WARNING]
[2017-12-26 10:44:10] [INFO]
[2017-12-26 10:44:10] [INFO] ------------------------------------------------------------------------
[2017-12-26 10:44:10] [INFO] Building hospital 0.0.1-SNAPSHOT
[2017-12-26 10:44:10] [INFO] ------------------------------------------------------------------------
[2017-12-26 10:44:10] [INFO]
[2017-12-26 10:44:10] [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ hospital ---
[2017-12-26 10:44:10] [INFO] Deleting /home/apps/software/git-repository/hospital/target
[2017-12-26 10:44:10] [INFO]
[2017-12-26 10:44:10] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ hospital ---
[2017-12-26 10:44:10] [INFO] Using 'UTF-8' encoding to copy filtered resources.
[2017-12-26 10:44:10] [INFO] Copying 10 resources
[2017-12-26 10:44:10] [INFO]
[2017-12-26 10:44:10] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ hospital ---
[2017-12-26 10:44:10] [INFO] Changes detected - recompiling the module!
[2017-12-26 10:44:10] [INFO] Compiling 218 source files to /home/apps/software/git-repository/hospital/target/classes
[2017-12-26 10:44:10] [WARNING] /home/apps/software/git-repository/hospital/src/main/java/com/haizhi/hospital/freemark/intf/impl/MedicalRecordDocServiceImpl.java: /home/apps/software/git-repository/hospital/src/main/java/com/haizhi/hospital/freemark/intf/impl/MedicalRecordDocServiceImpl.java使用或覆盖了已过时的 API。
[2017-12-26 10:44:10] [WARNING] /home/apps/software/git-repository/hospital/src/main/java/com/haizhi/hospital/freemark/intf/impl/MedicalRecordDocServiceImpl.java: 有关详细信息, 请使用 -Xlint:deprecation 重新编译。
[2017-12-26 10:44:10] [WARNING] /home/apps/software/git-repository/hospital/src/main/java/com/haizhi/hospital/dao/special/impl/StatisticRegisterDaoImpl.java: 某些输入文件使用了未经检查或不安全的操作。
[2017-12-26 10:44:10] [WARNING] /home/apps/software/git-repository/hospital/src/main/java/com/haizhi/hospital/dao/special/impl/StatisticRegisterDaoImpl.java: 有关详细信息, 请使用 -Xlint:unchecked 重新编译。
[2017-12-26 10:44:10] [INFO]
[2017-12-26 10:44:10] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ hospital ---
[2017-12-26 10:44:10] [INFO] Not copying test resources
[2017-12-26 10:44:10] [INFO]
[2017-12-26 10:44:10] [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ hospital ---
[2017-12-26 10:44:10] [INFO] Not compiling test sources
[2017-12-26 10:44:10] [INFO]
[2017-12-26 10:44:10] [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ hospital ---
[2017-12-26 10:44:10] [INFO] Tests are skipped.
[2017-12-26 10:44:10] [INFO]
[2017-12-26 10:44:10] [INFO] --- maven-war-plugin:2.2:war (default-war) @ hospital ---
[2017-12-26 10:44:10] [INFO] Packaging webapp
[2017-12-26 10:44:10] [INFO] Assembling webapp [hospital] in [/home/apps/software/git-repository/hospital/target/hospital]
[2017-12-26 10:44:10] [INFO] Processing war project
[2017-12-26 10:44:10] [INFO] Copying webapp resources [/home/apps/software/git-repository/hospital/src/main/webapp]
[2017-12-26 10:44:10] [INFO] Webapp assembled in [342 msecs]
[2017-12-26 10:44:10] [INFO] Building war: /home/apps/software/git-repository/hospital/target/hospital.war
[2017-12-26 10:44:10] [INFO] WEB-INF/web.xml already added, skipping
[2017-12-26 10:44:10] [INFO] ------------------------------------------------------------------------
[2017-12-26 10:44:10] [INFO] BUILD SUCCESS
[2017-12-26 10:44:10] [INFO] ------------------------------------------------------------------------
[2017-12-26 10:44:10] [INFO] Total time: 9.621 s
[2017-12-26 10:44:10] [INFO] Finished at: 2017-12-26T10:44:10+08:00
[2017-12-26 10:44:10] [INFO] Final Memory: 28M/263M
[2017-12-26 10:44:10] [INFO] ------------------------------------------------------------------------
[2017-12-26 10:44:10] stop tomcat service
[2017-12-26 10:44:12] delete old deploy data
[2017-12-26 10:44:12] copy war to deploy path
[2017-12-26 10:44:13] start tomcat
[2017-12-26 10:44:13] tomcatId is 37253 37254 37255

Complete the entire deployment process successfully.

Don't forget the last link, the deployment script is not started in the background, here you need to create a script to start it in the background:

deploy-service.sh

#! /bin/bash
/home/apps/service/deploy/deploy.sh &

When you repair in the future, you only need to modify the code locally, and then go to the server to execute the ./deploy-service.sh script, then the program will automatically help you complete the entire process of compilation-packaging-deployment.

When you see this, you will say, it’s so troublesome, you still need to go to the server to start the script yourself, hehe, I actually thought of this point, and I have come up with a way, but I’m too lazy to do it, because I think it’s smart enough.

Use the function of git hook:
1. Automatically call an interface of the server when submitting code
2. Use this interface to start the ./deploy-service.sh script

In this way, you can automatically compile, package and deploy every time you submit, and the integrated continuous integration process is not very convenient! Like me @

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326064117&siteId=291194637