Use Jenkins to build GIT+Maven projects

Use Jenkins to build GIT+Maven projects

Preface

Recently I wrote a blog about using Jenkins to build SVN+Maven projects . The code version tool used here is SVN, but in fact, many companies also use GITit for code management, so how do we use it Jenkinsto automatically publish GIT+ Mavenprojects?

text

Jenkins

Jenkins is an open source, extensible webinterface-based platform for continuous integration, delivery, and deployment . Allow continuous integration and continuous delivery projects, no matter what platform is used, can handle any type of build or continuous integration.

Usually we Jenkinsmainly use the following functions:

  • Continuous integration refers to the frequent (multiple times a day) integration of code into the backbone. Deliver the part of individual software research and development to the entire part of the software, and integrate frequently to find errors in it more quickly.
  • Continuous delivery refers to frequently delivering new versions of software to the quality team or users for review. If the review passes, the code enters the production stage.
  • Continuous deployment is the next step of continuous delivery, which refers to the automatic deployment of the code to the production environment after passing the review. The goal of continuous deployment is that the code is deployable at any time and can enter the production phase.

Use Jenkins to build GIT+Maven projects

1. Install GIT

In fact, many systems come with their own gittools, but because the built-in gitversion is too low to meet the requirements jenkinsfor the gittool version, it is recommended to install and configure gitafter the upgrade jenkins.

Step 1: Install the required dependencies

yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel
yum install gcc perl-ExtUtils-MakeMaker
yum install perl-ExtUtils-MakeMaker package

Step 2: Uninstall the original GIT

yum remove -y git            
git --version   

Insert picture description here

Step 3: Install the new GIT

cd /usr/local/src/
wget https://www.kernel.org/pub/software/scm/git/git-2.15.1.tar.xz
tar -vxf git-2.15.1.tar.xz
cd git-2.15.1
make prefix=/usr/local/git all
make prefix=/usr/local/git install
echo "export PATH=$PATH:/usr/local/git/bin" >> /etc/profile
source /etc/profile
git --version

Insert picture description here

Step 4: Create GIT link

ln -s /usr/local/git/bin/git-upload-pack /usr/bin/git-upload-pack
ln -s /usr/local/git/bin/git-receive-pack /usr/bin/git-receive-pack

2. Configure git password-free login

Step 1: Jenkins server generates public key and secret key

  • /root/.ssh/id_rsa.pub: Public key, usually used to encrypt the session key.
  • /root/.ssh/id_rsa: Key used to verify the digital signature.
  • /root/.ssh/authorized_keys: Store the public keys of other servers.

The corresponding file will be automatically generated after executing the following command

ssh-keygen -t rsa

Step 2: Add the public key generated by jenkins id_rsa.pubto the git server authorized_keysfile

chattr -ia authorized_keys

chmod 777 authorized_keys

vim authorized_keys

Insert picture description here

Step 3: Get id_rsathe contents of the key

cat /root/.ssh/id_rsa

Insert picture description here

3. Install and configure Jenkins

Step 1: Download the war installation package

Official download address: https://www.jenkins.io/download/

Step 2: Install and configure Jenkins

nohup java -jar jenkins.war &

Jenkins default path: IP:8080After entering the management interface, follow the prompts to configure

  1. Obtain the initial administrator password from the specified location according to the prompt
  2. Configure a user
  3. Install system recommended Jenkinsplug-ins

Insert picture description here

Step 3: Install Jenkins plugins as needed

Install the following plugins in jenkins-Manage Jenkins-plugin management :

  • Publish Over SSH : It is used sshto connect to a remote server by way to achieve remote code push.
    Insert picture description here

Step 4: Configure local tools

In jenkins - Manage Jenkins - Global Tool Configuration configure Maven, JDKand GIT:

  • Use Mavento build the project
  • JDKProvides a running environment for the project
  • GITVersion control repository for code.

MavenConfigured
Insert picture description here
Insert picture description here
JDKconfigured
Insert picture description here
GITconfigured
Insert picture description here

Step 5: Configure the system configuration Configure the remote server
in jenkins-Manage Jenkins-System Configuration . This server is the running container of the project.
Insert picture description here

Step 6: Configuring GIT key
in jenkins- credentials - System - Global Credentials configured gitkey

Insert picture description here

Step 7: Create a new task, configure source code management, build and post-build operations

Source code management

Insert picture description here

Construct

Insert picture description here

Post-build operations

Insert picture description here

run.sh : The script is used to startspring-dubbo-producer.jar

APP_NAME=spring-dubbo-producer.jar
path=`pwd`

#使用说明,用来提示输入参数
usage() {
    
    
echo "Usage: sh 执行脚本.sh [start|stop|restart|status]"
exit 1
}
#检查程序是否在运行
is_exist(){
    
    
pid=`ps -ef|grep $APP_NAME|grep -v grep|awk '{print $2}' `
#如果不存在返回1,存在返回0
if [ -z "${pid}" ]; then
return 1
else
return 0
fi
}
#启动方法
start(){
    
    
is_exist
if [ $? -eq "0" ]; then
echo "${APP_NAME} is already running. pid=${pid} ."
else
source /etc/profile
nohup java -jar $APP_NAME > $path/logs.txt 2>&1 &
fi
}
#停止方法
stop(){
    
    
is_exist
if [ $? -eq "0" ]; then
kill -9 $pid
else
echo "${APP_NAME} is not running"
fi
}
#输出运行状态
status(){
    
    
is_exist
if [ $? -eq "0" ]; then
echo "${APP_NAME} is running. Pid is ${pid}"
else
echo "${APP_NAME} is NOT running."
fi
}
#重启
restart(){
    
    
stop
start
}
#根据输入参数,选择执行对应方法,不输入则执行使用说明
case "$1" in
"start")
start
;;
"stop")
stop
;;
"status")
status
;;
"restart")
restart
;;
*)
usage
;;
esac

Step 7: Build the project and verify

Build the project

Insert picture description here

Verification
Visit the interface of your own project for verification, here is knife4jan example:ip:8080/doc.html

Insert picture description here

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_40990818/article/details/108839662