Jenkins is an open source project continuous integration tool developed in Java.
1. Environment before installation
1.1.1 JDK installation
JDK1.8
extraction code: t4c3
(1) Decompression
tar -zxvf jdk-8u231-linux-x64.tar.gz
(2) Configure environment variables
# 编辑profile文件
vim /etc/profile
Add the following:
export JAVA_HOME=/java/jdk8
export CLASSPATH=.:$JAVA_HOME/lib/dt.:jar$JAVA_HOME/lib/tools.jar
export PATH=$PATH:$JAVA_HOME/bin
注意:按照自己的实际java解压路径来填写。
1.1.2 Maven
Maven
extraction code: 98ut
(1) decompression
tar -zxvf apache-maven-3.8.5-bin.tar.gz
(2) Configure the settings.xml file
# 进入 解压后maven的conf文件夹(操作时根据自己实际的加压路径)
cd /maven/conf
# 编辑 settings.xml文件
vim setttings.xml
# 添加镜像 这样下载依赖(jar包)时会很快
<mirrors>
<mirror>
<id>alimaven</id>
<mirrorOf>central</mirrorOf>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/repositories/central/</url>
</mirror>
</mirrors>
注意:按照自己的实际maven解压路径来填写。
1.1.3 git
Install directly using the command
yum install git
2. Jenkins installation
1.1 Install Jenkins via war
Jenkins.war
extraction code: wkjn
(1) Start Jenkins
because we 安装前环境
have already installed it in JDK
, so just run Jenkin's War package directly:
nohup java -jar jenkins.war & #后台运行
(2) The default port number for accessing Jenkins is 8080, just enter + port
in the browser :ip地址
8080
根据页面提示去linux系统中对应的文件目录下找管理员密码。
(3) Install plug-ins, manage users, and configure instances.
Choose here 安装推荐的插件
. When using the war package to run, there will be no failure to install the recommended plug-ins.
At this point, the installation of Jenkins through the war package has been completed.
需要特别注意:通过war包的方式安装Jenkins,Jenkins的workspace文件夹和其他文件都是在下面的目录下:
/root/.jenkins/
1.2 Install by command
(1) Download
wget https://repo.huaweicloud.com/jenkins/redhat-stable/jenkins-2.190.3-1.1.noarch.rpm
(2) Installation
rpm -ivh jenkins-2.190.3-1.1.noarch.rpm
(3) Modify the configuration
Modify the JDK path of Jenkins to the JDK path installed by yourself. The command is as follows:
cd /etc/init.d/
vim jenkins
(4) Run Jenkins to view the status
systemctl start jenkins #运行
systemctl status jenkins #查看状态
It shows that the following logo is running:
the subsequent steps are consistent with running the war package.
需要特别注意:通过命令行安装的Jenkins的配置文件和workspace文件夹都是在下面的目录下:
cd /var/lib/jenkins/
Jenkins的配置文件在:
/etc/sysconfig/jenkins
3. Problems that may be encountered in Jenkins installation
3.1 Unable to install安装推荐的插件
(1) Problem description
Download and install Jenkins through the wget command line, and there will be a problem that the plug-in cannot be installed:
Caused by: javax.net.ssl.SSLHandshakeException: PKIX path building failed unable to find valid certification path to requested target
or it could be:
There were errors checking the update sites: SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification
(2) Cause of the problem
This is because the request is an issue with the SSL certificate.
(3) Problem solving
Enter the JENKINS_HOME directory of Jenkins:
/var/lib/jenkins/
Find hudson.model.UpdateCenter.xml in this directory:
modify it as follows:
<sites>
<site>
<id>default</id>
<url>https://mirrors.tuna.tsinghua.edu.cn/jenkins/updates/update-center.json</url>
</site>
</sites>
Go back to the previous level, find the updates folder, and then modify the default.json file to replace all the
http://updates.jenkins-ci.org/download
replace with
https://mirrors.tuna.tsinghua.edu.cn/jenkins
Will
http://www.google.com
Replace with:
http://www.baidu.com
Log in to Jenkins 系统管理
-> 插件管理
-> 高级
, configure the site:
https://updates.jenkins.io/update-center.json
Then go to download the plugin.
Of course, even if the above things are configured, there will be wrong plug-ins downloaded, but there are relatively few plug-ins. Next, you can download individual plug-ins that fail from the plug-in page of the Jenkinsc official website and put them in Jenkins. For example:
search for plugins that failed to download:
select the corresponding version (some versions may not be compatible with the Jenkins version and need to upgrade the Jenkins version)
log in to Jenkins and upload the plugin in 系统管理
-> -> 插件管理
-> :高级
4. Configure a Maven project
Let me take the code put on Gitee as an example:
4.1 Download Gitee and Maven plugins
Log in to Jenkins 系统管理
-> I插件管理
have already installed it here. If you want to install it, you need to search for the corresponding plug-in name to install it.可选插件
4.2 Create a Maven project
new task
4.3 Configuration items
4.3.1 Global tool configuration
(1) Configure maven, JDK, git
The maven, JDK, and git paths are the installation paths.
4.3.2 System configuration
(1) Gitee configuration
So how to get the token of Gitee?
登陆Gitee
-> 设置
-> 私人令牌
then commit.
Copy token.
4.3.3 Project configuration
(1) Gitee link
Select 系统配置
the Gitee link configured in here.
(2) Source code management
- Warehouse URL: It is your warehouse address on Gitee.
- Credentials authentication information: Fill in the GiteeAPI token configured above.
- Branch: Which branch do you need Jenkins to manage. Fill in according to your actual situation
(3) build trigger
(4) build environment
(4) pre-steps (Pre Steps)
(5) Build (emphasis)
use Maven to build the project, the command is:
clean install -Dmaven.test.skip=true
(5) Post Steps (emphasis)
added执行shell
Shell script explanation:
cd /java/project/ # 进入存放项目的文件夹(自定义的)
sh stop.sh # 停止正在运行的旧版本项目 shell脚本
sh replace.sh # 替换编译后的jar包 shell脚本
#启动新编译打包的项目
BUILD_ID=dontKillMe nohup java -jar /java/project/编译后包的名称.jar &
stop.sh and replace.sh contents:
#stop.sh
#找到正在运行旧项目的 编号
#然后停止
pid=`ps -ef | grep 编译后的jar包的名称 | grep -v grep | awk '{print $2}'`
if [ -n "$pid" ]
then
kill -9 $pid
fi
#replate.sh
#判断指定路径下的jar包是否存在
#如果存在,更名备份
#将Jenkins打包编译后的项目移动到指定的项目目录下
file="/java/project/编译后包的名称.jar"
if [ -f "$file" ]
then
mv /java/project/编译后包的名称.jar /java/project/编译后包的名称.jar.`date +%Y%m%d%H%M%S`
fi
mv /root/.jenkins/workspace/项目名称/target/编译后包的名称.jar /java/project/
(6) Save the configuration.
5. Build the project
Check out the build log:
![insert image description here](https://img-blog.csdnimg.cn/0ecd236d95504488a2ced0238b40a89a.png
6. Summary
The process of building and running Jenkins projects is as follows:
- Jenkins installed by running the war package, the compiled package is
/root/.jenkins/workspace/
below. - Jenkins installed through the command, the compiled package is
/var/lib/jenkins/workspace/
below