Use Jenkins to build GIT+Maven projects

preface

I recently wrote a blog post about using Jenkins to build SVN+Maven projects. The code versioning tool used here is SVN, but in fact many companies use GIT for code management. How can we use Jenkins to automatically release GIT+Maven projects?

The body of the

Jenkins

Jenkins is an open source, extensible web-based platform for continuous integration, delivery and deployment. Allows continuous integration and continuous delivery of projects, regardless of platform, to handle any type of build or continuous integration.

Generally, Jenkins is used to achieve the following functions:

  • Continuous integration refers to the frequent (multiple times a day) integration of code into the trunk. Deliver individual pieces of software to the overall pieces of software, and integrate frequently to find bugs more quickly.
  • Continuous delivery refers to the frequent delivery of new versions of software to the quality team or users for review. If the review passes, the code goes into production.
  • Continuous deployment is the next step in continuous delivery, meaning that code is automatically deployed to production after it has been reviewed. The goal of continuous deployment is for the code to be deployable at all times and ready for production.

Use Jenkins to build GIT+Maven projects

1. Install GIT

In fact, many systems have their own Git tools, but the version of their own Git is too low to meet Jenkins’ requirements for the version of git tools. Therefore, it is recommended to upgrade Git before Jenkins installation and configuration.

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
Copy the code

Step 2: Uninstall GIT

yum remove -y git            
git --version   
Copy the code

Step 3: Install a 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.151..tar.xz
cd git-2.151.
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
Copy the code

Step 4: Create a 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
Copy the code

2. Configure git secret-free login

Step 1: Jenkins server generates public and key

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

The corresponding file is automatically generated after you run the following command

ssh-keygen -t rsa
Copy the code

Step 2: The public key generated by Jenkinsid_rsa.pubJoin the Git serverauthorized_keysIn the file

chattr -ia authorized_keys

chmod 777 authorized_keys

vim authorized_keys
Copy the code

Step 3: Get the keyid_rsaThe content of the

cat /root/.ssh/id_rsa
Copy the code

3. Install and configure Jenkins

Step 1: Download the WAR installation package

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

Step 2: Install and configure Jenkins

nohup java -jar jenkins.war &
Copy the code

The default Jenkins path is IP:8080. Configure the Jenkins path as prompted

  1. Obtain the initial administrator password from the specified location as prompted
  2. Configuring a User
  3. System InstallationJenkinsThe plug-in

Step 3: Install Jenkins plugins as needed

Install the following plug-ins in Jenkins — Manage Jenkins — Plugin Management:

  • Publish Over SSH: Used to passsshTo connect to the remote server, so as to achieve remote code push.

Step 4: Configure local tools

Configure Maven, JDK, and GIT in Jenkins — Manage Jenkins — Global Tool Configuration:

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

Mavenconfiguration





JDKThe configuration of the



GITThe configuration of the

Step 5: Configure the system configuration

Jenkins, the Manage Jenkins – System ConfigurationTo configure the remote server, which is the running container for the project.

Step 6: Configure the GIT key Configure the GIT key in Jenkins – Credentials – System – Global Credentials

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

Source code management

build

Post-build operation

Run. Sh: script used to start spring-dubo-producer.jar

APP_NAME= spring-dubo-producer.jar path= 'PWD' # Usage () {echo"The Usage: sh executing scripts. Sh [start | stop | restart | status]"
exit 1} # to check whether the program is running is_exist () {pid = ` ps - ef | grep $APP_NAME | grep -v grep | awk'{print $2}''# return if there is none1, there is a return0
if [ -z "${pid}" ]; then
return 1
else
return 0Fi} # start(){is_existif [ $? -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# stop(){is_existif [ $? -eq "0" ]; then
kill -9 $pid
else
echo "${APP_NAME} is not running"Fi} # output running status status(){is_existif [ $? -eq "0" ]; then
echo "${APP_NAME} is running. Pid is ${pid}"
else
echo "${APP_NAME} is NOT running."Fi} # restart(){stop start} # According to the input parameters, select the corresponding method to execute, do not enter the instructionscase "$1" in
"start")
start
;;
"stop")
stop
;;
"status")
status
;;
"restart")
restart
;;
*)
usage
;;
esac
Copy the code

Step 7: Build the project and verify

Build the project

Verify access to your own project interface for validation, I use knife4j as an example: IP :8080/doc.html