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 GIT
it for code management, so how do we use it Jenkins
to automatically publish GIT
+ Maven
projects?
text
Jenkins
Jenkins is an open source, extensible web
interface-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 Jenkins
mainly 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 git
tools, but because the built-in git
version is too low to meet the requirements jenkins
for the git
tool version, it is recommended to install and configure git
after 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
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
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.pub
to the git server authorized_keys
file
chattr -ia authorized_keys
chmod 777 authorized_keys
vim authorized_keys
Step 3: Get id_rsa
the contents of the key
cat /root/.ssh/id_rsa
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:8080
After entering the management interface, follow the prompts to configure
- Obtain the initial administrator password from the specified location according to the prompt
- Configure a user
- Install system recommended
Jenkins
plug-ins
Step 3: Install Jenkins plugins as needed
Install the following plugins in jenkins-Manage Jenkins-plugin management :
- Publish Over SSH : It is used
ssh
to connect to a remote server by way to achieve remote code push.
Step 4: Configure local tools
In jenkins - Manage Jenkins - Global Tool Configuration configure Maven
, JDK
and GIT
:
- Use
Maven
to build the project JDK
Provides a running environment for the projectGIT
Version control repository for code.
Maven
Configured
JDK
configured
GIT
configured
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.
Step 6: Configuring GIT key
in jenkins- credentials - System - Global Credentials configured git
key
Step 7: Create a new task, configure source code management, build and post-build operations
Source code management
Construct
Post-build operations
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
Verification
Visit the interface of your own project for verification, here is knife4j
an example:ip:8080/doc.html