1.安装 jenkins 通过下载镜像安装

1
2
3
4
5
6
7
8
9
10
11
12
sudo wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat/jenkins.repo
sudo rpm --import https://pkg.jenkins.io/redhat/jenkins.io.key

yum --disablerepo=pgdg94 install epel-release # repository that provides 'daemonize'
yum --disablerepo=pgdg94 install java-11-openjdk-devel

# 在线安装
yum --disablerepo=pgdg94 install -y jenkins
# 或 离线rpm安装
https://github.com/jenkinsci/jenkins/releases

rpm -ivh jenkins-2.313-1.1.noarch.rpm

2. 配置端口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
vi /etc/sysconfig/jenkins
-----------------------------------------------------------------------
# 指定目录
JENKINS_HOME="/home/jenkins"

# 访问用户,默认为Jenkins,把它改成root
JENKINS_USER="root"

# 修改端口
JENKINS_PORT="8822"

# 授权
chown -R root:root /var/lib/jenkins
chown -R root:root /var/cache/jenkins
chown -R root:root /var/log/jenkins

# 修改jekins java环境变量
执行 whereis java
# 修改
vi /etc/init.d/jenkins

3. 启动jenkins

1
2
3
4
5
6
7
8
9
10
systemctl jenkins start
systemctl start jenkins

systemctl jenkins stop
systemctl stop jenkins

systemctl jenkins restart
systemctl restart jenkins

systemctl enable jenkins

4. 安装相关插件 安装所有和maven 和 gitlab相关的插件

5. 配置项目 -点击 new-item 选择maven project

img

6. 配置 gitlab地址

branches to build 是分支号

7. 配置maven build命令

img

仅以上3步就可以构建一个项目了

jekins 默认会把项目从git拉去下来存放在

1
/var/lib/jenkins/workspace/oywc-test-new-java

8. 在manage jekins中配置ssh-服务

img

img

在这里配置好服务后

回到item中的配置

img

exec command 是代表build完项目之后 后执行的脚本

springboot.sh 如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/bin/sh


# java -jar /var/lib/jenkins/workspace/oywc-test-new-java/target/oywc-test-0.0.1-SNAPSHOT.jar &


pid=`ps -ef | grep oywc-test-0.0.1-SNAPSHOT.jar | grep -v grep | awk '{print $2}'`
if [ -n "$pid" ]
then
echo $pid
kill -9 $pid
fi


java -jar /var/lib/jenkins/workspace/oywc-test-new-java/target/oywc-test-0.0.1-SNAPSHOT.jar > /usr/local/software/out.log 2>&1 &

9. 点击构建

img

10. 卸载

1
2
3
4
5
6
7
8
9
10
11
12
service jenkins stop
yum clean all

yum -y remove jenkins
# rpm卸载
rpm -e jenkins

# 检查是否卸载成功
rpm -ql jenkins

# 彻底删除残留文件:
find / -iname jenkins | xargs -n 1000 rm -rf