1.安装
需先下载java环境
# 拉取库的配置到本地对应文件
0、wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins-ci.org/redhat/jenkins.repo
1、wget https://pkg.jenkins.io/redhat-stable/jenkins-2.190.1-1.1.noarch.rpm
2、yum install jenkins-2.190.1-1.1.noarch.rpm -y
# 修改配置文件,用户改成了root,端口8020
3、vim /etc/sysconfig/jenkins
JENKINS_USER="root"
JENKINS_PORT="8020"
4、启动
systemctl start jenkins
systemctl enable jenkins
5.查看状态
[root@iZuf6fy2kg5mx828krkhcuZ qqc_data]# systemctl status jenkins
● jenkins.service - LSB: Jenkins Automation Server
Loaded: loaded (/etc/rc.d/init.d/jenkins; bad; vendor preset: disabled)
Active: active (running) since Sat 2020-10-17 16:24:29 CST; 16s ago
Docs: man:systemd-sysv-generator(8)
CGroup: /system.slice/jenkins.service
└─22749 /etc/alternatives/java -Dcom.sun.akuma.Daemon=daemonized -Djava.awt.headless=true -DJENKINS_HOME=/var/lib/jenkins -jar ...
Oct 17 16:24:27 iZuf6fy2kg5mx828krkhcuZ systemd[1]: Starting LSB: Jenkins Automation Server...
Oct 17 16:24:27 iZuf6fy2kg5mx828krkhcuZ runuser[22734]: pam_unix(runuser:session): session opened for user root by (uid=0)
Oct 17 16:24:29 iZuf6fy2kg5mx828krkhcuZ systemd[1]: Started LSB: Jenkins Automation Server.
Oct 17 16:24:29 iZuf6fy2kg5mx828krkhcuZ jenkins[22729]: Starting Jenkins [ OK ]
[root@iZuf6fy2kg5mx828krkhcuZ qqc_data]# ps -aux | grep 8020
root 22749 32.0 14.6 2351276 148728 ? Ssl 16:24 0:12 /etc/alternatives/java -Dcom.sun.akuma.Daemon=daemonized -Djava.awt.headless=true -DJENKINS_HOME=/var/lib/jenkins -jar /usr/lib/jenkins/jenkins.war --logfile=/var/log/jenkins/jenkins.log --webroot=/var/cache/jenkins/war --daemon --httpPort=8020 --debug=5 --handlerCountMax=100 --handlerCountMaxIdle=20
root 22907 0.0 0.0 112712 964 pts/2 S+ 16:25 0:00 grep --color=auto 8020
6、访问
http://www.qqc-home.com:8020/
如下图显示即已安装成功
# 管理员登录密码位置:
vim /var/lib/jenkins/secrets/initialAdminPassword
登录名:admin
2、使用
2.1 啰嗦几句
ps: 配置了半天也没搞清楚怎么操作,各种选项操作太复杂了;
它的基本流程:手动在服务器执行发布任务命令的过程,进行封装后,可视化页面进行一键批量调用;
本质上就是干了这么一件事,附加了一些用户权限,发布日志记,异常发邮件等相关功能;
有兴趣的同学去jenkins官网(https://www.jenkins.io/zh/doc/)学一下,我暂时学不会啊
2.2 自己实现简易版发布接口
目标:在本地起一个flask 服务,写一个发布接口
(1) 建一个表,存发布日志信息,基本信息如:用户id,发布描述信息,服务器返回的日志等
(2)连接服务器,执行发布命令,日志信息入库,返回
逻辑代码:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2020/12/10 15:28
# @Author : qqc
# @File : release_tool.py
# @Software: PyCharm
import paramiko
import datetime
import json
from utils.connect_mysql import *
class ReleaseTools(object):
def __init__(self, user_id, params):
self.user_id = user_id
self.params = params
@property
def ssh_connect(self):
""" 连接服务器 """
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# 通过ssh 私钥登录
ssh.connect(hostname='47.102.138.171', username='root', key_filename=r'C:Usersadmin.sshid_rsa')
return ssh
@property
def command(self):
""" 发布的命令 """
cmds = " cd /object/test_obj/django_test ; "
"git pull ; "
"supervisorctl restart test-django:test-django-uwsgi "
return cmds
def create_release_data(self, info_data):
""" 记录发布日志 """
sql_str = """
INSERT INTO obj_release
(user_id, params_data, out_data,create_time)
VALUES
({0}, '{1}','{2}', NOW());
""".format(self.user_id, self.params, info_data)
cursor_sql(sql_str)
def obj_release(self):
""" 执行入口 """
stdin, stdout, stderr = self.ssh_connect.exec_command(self.command)
out = stdout.readlines()
err = stderr.readlines()
info_data = json.dumps({"out": out, "err": err}, ensure_ascii=False)
self.create_release_data(info_data)
self.ssh_connect.close()
return {"user_id": self.user_id, "params_data": self.params, "out": out, "err": err,
"create_time": datetime.datetime.now()}
调用路由(参数根据实际业务需求调节):
from flask import Flask, request, jsonify
from utils.release_tool import *
app = Flask(__name__)
@app.route('/get/release')
def release_objects():
user_id = request.args.get("user_id", 0)
params = request.args.get("params", '')
release = ReleaseTools(user_id, params)
res = release.obj_release()
return jsonify(res)
3. 测试
问题:
1、多台服务器发布
2、用户使用权限配置
3、实际生产过程中,通过跳板机连接到生产服务器;
4、可视化页面操作
ps:优化是没完没了的过程,有兴趣的伙伴们来完善起来了啊