Jenkins installation and construction of springboot project

table of Contents

Introduction to Jenkins

Jenkins installation preparation

Jdk installation

Install git

Install maven

Install Mysql

Install Jenkins

Configure jenkins

jdk configuration

Git configuration

MAVEN configuration

Global configuration gitee

Build the maven project

Jenkins deployed to a remote server

Perform global configuration

Then build the project configuration


Introduction to Jenkins

Jenkins is an open source software project, a continuous integration tool based on Java, used to monitor continuous repetitive work, and aims to provide an open and easy-to-use software platform that enables continuous integration of software projects. My understanding is that I don’t need to frequently enter some packaging commands and then deploy them. Instead, I pull the code remotely through jenkins, and automatically package and deploy the startup project through script commands.

Jenkins installation preparation

Because jenkins is based on java language, java environment is needed. In addition, we have to install maven and git.

Jdk installation

Please refer to the installation of Linux jdk .

Install git

[[email protected] ~]# yum -y install git验证是否安装成功[[email protected] ~]# git --versiongit version 1.8.3.1

Install maven

Go to the official website to download the installation package http://maven.apache.org/download.cgi to download the linux format

Upload to the server and unzip to /usr/local/maven/maven, then configure maven environment variables

[[email protected] maven]# vim /etc/profile

Add the following

export MAVEN_HOME=/usr/local/maven/mavenexport PATH=$PATH:$MAVEN_HOME/bin

: Wq save, refresh to take effect

[[email protected] maven]# source /etc/profile

Check whether the configuration is successful, if the version information appears, it is successful.

[[email protected] maven]# mvn -version

Modify maven's settings.xml file to add mirror dependency

	 <mirror>        <id>nexus-aliyun</id>        <mirrorOf>*</mirrorOf>        <name>Nexus aliyun</name>        <url>http://maven.aliyun.com/nexus/content/groups/public</url>    </mirror>    <mirror>        <id>osc</id>        <mirrorOf>*</mirrorOf>        <url>http://maven.oschina.net/content/groups/public/</url>    </mirror> 

Install Mysql

Since the project is connected to the database, install it here or not. In order to save trouble, install it with docker here

获取镜像[[email protected] ~]# docker pull mysql安装[[email protected] ~]# docker create --name mysql8 -e MYSQL_ROOT_PASSWORD=root -p 3306:3306 mysql启动[[email protected] ~]# docker start mysql8

If a 2059 error is reported when using Navicat to connect. solution

将mysql用户登录的加密规则修改为mysql_native_password。[[email protected] ~]# docker exec -it mysql8 [email protected]:/etc/mysql# mysql -uroot -p输入密码 执行以下sql%是支持远程连接,localhost是本地连接。ALTER USER 'root'@'%' IDENTIFIED BY 'root' PASSWORD EXPIRE NEVER; ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'root'; FLUSH PRIVILEGES;然后就可以连接上了。

Install Jenkins

Download the installation package Jenkins official website download address

After downloading and uploading to the server, execute the following command to install

[[email protected] ~]# rpm -ivh  jenkins-2.296-1.1.noarch.rpm

The default path after installation

/usr/lib/jenkins/jenkins.war    WAR包  /etc/sysconfig/jenkins       配置文件/var/lib/jenkins/        默认的JENKINS_HOME目录/var/log/jenkins/jenkins.log    Jenkins日志文件

Modify the location of the configuration file as shown in the screenshot below

[[email protected] ~]# vim /etc/sysconfig/Jenkins

Fill in your own java path and modify the user to root

Note: 1. Do not start the actual production with the root user, but create a user by yourself

2. The java path should be written to java under bin, such as /usr/local/jdk1.8.0_181/bin/java

Then authorize the following files to root and then when you execute the script, it will tell you that you have no permission

chown -R root:root /var/lib/jenkinschown -R root:root /var/log/jenkinschown -R root:root /var/cache/jenkins

Start jenkins

[email protected] ~]# service jenkins start

The default port is 8080. Browser access http:your ip:8080

If the following error is reported at this time

The solution is as follows, the reason for the error is a jdk problem

1. Uninstall the original jdk and install the official jdk version.
       2. Additional configuration plug-ins on the server, install according to the version of your own server, as follows:

需要安装#Ubuntu9以上apt-get install ttf-dejavu #CentOS 6yum install dejavu-sans-fonts #CentOS 7yum install fontconfig 

Then restart it

[[email protected] jenkins]# service jenkins restart

Configure jenkins

cat /var/lib/jenkins/secrets/initialAdminPassword

Copy the password here, and then in the next step, install the recommended plug-in, create a user, and then save to continue.

Because we want to build a maven project, we need to install the maven plug-in, and we need to pull the code from the code cloud, so we need the gitee plug-in (same as the picture below)

The following global configuration is as shown below

jdk configuration

Git configuration

MAVEN configuration

Global configuration gitee

Private token, click the question mark and tell you where to generate it

Build the maven project

Configure maven

The script is as follows, the script must be placed in your own location, for example mine is /opt

#!/bin/bash -ileproject=jenkins.jarpathName=test-jenkinsecho "拷贝文件"rm -rf /opt/springboot/$projectmv  /var/lib/jenkins/workspace/$pathName/target/$project /opt/springboot/echo "切换路径"cd /opt/springboot/ pid=`ps -ef | grep $dir$project | grep -v grep | awk '{print $2}'`if [ -n "$pid" ]then   kill -9 $pid   echo "杀死存在进程"fiBUILD_ID=dontKillMe echo "执行"nohup java -jar $project --spring.profiles.active=test > catalina.out  2>&1 &echo "启动成功!"

If the editing tool under Windows causes the file format to be under dos, you need to change it to Unix and execute the following command

sed -i "s/\r//" start.sh

Then authorize start.sh

chmod +x start.sh

Then start building, and then wait quietly.

Jenkins deployed to a remote server

Create a new virtual machine, here you can clone one directly.

Need to install Publish Over SSH plugin

Perform global configuration

name------------take it at will

hostname--------------The IP address where you want to run the service

username------------------Login username

Remote Directory-----------The location to send the packed package to

Then build the project configuration

source file ---------- source file address

remove prefix---------Remove prefix

remote directory-----Remote address, plus the previous configuration will eventually be sent to the file /opt/myjar

exec command ------Execute the script file, which needs to be placed on the machine where the service is to be published

The script is as follows

project=jenkins.jarecho "Stopping $project"source /etc/profilepid=`ps -ef | grep $project | grep -v grep | awk '{print $2}'`if [ -n "$pid" ]then   echo "kill -9 的pid:" $pid   kill -9 $pidfiecho "stop finish"nohup java -jar /opt/myjar/$project --spring.profiles.active=test > catalina.out  2>&1 &echo "start run....."

Then rebuild it and it's all right.

Finally, the demo address, you can download it directly, or you can build a springboot project yourself. Note that my project configuration file is configured in the Apollo configuration center. If you don't need it, please open the comment on the above configuration, and then comment out the Apollo configuration.