jenkins自动化部署git 项目,jenkins自动部署前端项目
大家好,小编来为大家解答以下问题,jenkins自动化部署git 项目,jenkins自动部署前端项目,今天让我们一起来看看吧!• Jenkins 的主要功能是监视重复工作的执行,例如软件工程的构建或在 cron下设置的 jobs。因为现在使用的是jenkins低版本,无法安装插件,插件需要更高的版本。/var/www/deploy/:用于存储当前版本文件和解压后的软件包。/var/www/dow
大家好,小编来为大家解答以下问题,jenkins自动化部署git 项目,jenkins自动部署前端项目,今天让我们一起来看看吧!

Source code download: 本文相关源码
注:接上一篇博客《python:git及gitlab服务器部署》
Jenkins概述
• Jenkins是由java编写的一款开源软件
• 作为一款非常流行的CI(持续集成)工作,用于构建和测试各种项目
• Jenkins 的主要功能是监视重复工作的执行,例如软件工程的构建或在 cron下设置的 jobs
持续集成
• 持续集成(CI)是当下最为流行的应用程序开发实践方式
• 程序员在代码仓库中集成了修复bug、新特性开发或是功能革新
• CI工具通过自动构建和自动测试来验证结果。这可以检测到当前程序代码的问题,迅速提供反馈
Jenkins特点
• 简单、可扩展、用户界面友好
• 支持各种SCM(软件配置管理)工具,如SVN、GIT、CVS等
• 能够构建各种风格的项目
• 可以选择安装多种插件
• 跨平台,几乎可以支持所有的平台
准备jenkins服务器(配置IP192.168.122.73、主机名、yum、安装java),要求可以访问外网
[root@jenkins ~]# ifconfig virbr0 down
[root@jenkins ~]# brctl delbr virbr0
[root@jenkins ~]# ifdown eth0; ifup eth0
[root@jenkins ~]# ping www.baidu.com
PING www.a.shifen.com (14.215.177.39) 56(84) bytes of data.
64 bytes from 14.215.177.39 (14.215.177.39): icmp_seq=1 ttl=54 time=14.0 ms
64 bytes from 14.215.177.39 (14.215.177.39): icmp_seq=2 ttl=54 time=13.1 ms
64 bytes from 14.215.177.39 (14.215.177.39): icmp_seq=3 ttl=54 time=62.3 ms
^C
安装jenkins
[root@jenkins ~]# ls
anaconda-ks.cfg
jenkins-2.138.2-1.1.noarch.rpm
initial-setup-ks.cfg
[root@jenkins ~]# yum -y install jenkins-2.138.2-1.1.noarch.rpm
[root@jenkins ~]# systemctl start jenkins
[root@jenkins ~]# systemctl enable jenkins
打开http://192.168.122.73:8080python基础知识点总结图解。安装插件选择自定义=>无。不用创建管理员帐号,使用admin登陆即可。登陆后,将管理员的密码改掉。
[root@jenkins ~]# cat /var/lib/jenkins/secrets/initialAdminPassword
e191d2f592f24baab2b58bd9956d1f62 #将该密码信息粘贴到初始化页面
在Jenkins上安装插件
配置方法详见:jenkins插件下载镜像加速-CSDN博客
清华大学插件地址:https://mirrors.tuna.tsinghua.edu.cn/jenkins/updates/update-center.json
在可选插件中安装git parameter。
因为现在使用的是jenkins低版本,无法安装插件,插件需要更高的版本。所以下载新版本:http://mirrors.jenkins-ci.org/redhat/jenkins-2.177-1.1.noarch.rpm(该版本已安装git parameter)
[root@jenkins ~]# wget http://mirrors.jenkins-ci.org/redhat/jenkins-2.177-1.1.noarch.rpm
[root@jenkins ~]# ls
anaconda-ks.cfg
jenkins-2.177-1.1.noarch.rpm
jenkins-2.138.2-1.1.noarch.rpm
[root@jenkins ~]# systemctl stop jenkins
[root@jenkins ~]# yum -y update jenkins-2.177-1.1.noarch.rpm //升级jenkins
[root@jenkins ~]# systemctl start jenkins
[root@jenkins ~]# yum -y install git //用于从gitlab服务器拉取代码
项目流程
- 程序员在自己的电脑上编写程序
[root@localhost ~]# git init myweb
初始化空的 Git 版本库于 /root/myweb/.git/
[root@localhost ~]# cd myweb/
[root@localhost myweb]# echo '<h1>My Web Site</h1>' > index.html
[root@localhost myweb]# git commit -m "web 1.0"
[root@localhost myweb]# git tag 1.0 //将当前状态标记为1.0版本
[root@localhost myweb]# echo '<h2>2nd version</h2>' >> index.html
[root@localhost myweb]# git add .
[root@localhost myweb]# git commit -m "web 2.0"
[master f2e300a] web 2.0
1 file changed, 1 insertion(+)
[root@localhost myweb]# git tag 2.0
- 在gitlab上,在devops组中创建名为myweb的公开项目,新建的用户是该项目的主程序员
- 程序员上传代码到gitlab
[root@localhost myweb]# git remote rename origin old-origin
[root@localhost myweb]# git remote add origin git@192.168.122.124:devops/myweb.git
[root@localhost myweb]# git push -u origin --all
[root@localhost myweb]# git push -u origin --tags
- 在jenkins上创建工程,自动下载代码。创建一个自由风格的项目=>勾选This project is parameterized =>添加参数 git parameter=> name: webver, Default Value: origin/master =>源码管理:git =>Repository URL: http://192.168.122.124/devops/myweb.git => Branch Specifier (blank for ‘any’): $webver => 保存
- 执行任务(Build with Parameters),jenkins将会下载myweb项目到/var/lib/jenkins/workspace目录,这里选择webver1.0,用于构建项目

- 构建完项目后,查看构建历史,jenkins将会下载myweb项目到/var/lib/jenkins/workspace目录

[root@jenkins ~]# ls /var/lib/jenkins/workspace/
myweb
[root@jenkins ~]# cat /var/lib/jenkins/workspace/myweb/index.html
<h1>My Web Site</h1>
完善jenkins
- jenkins下载web项目后,将其打包
- 为了应用服务器可以下载项目软件包,在jenkins上安装httpd服务,以便后端应用服务器下载
[root@jenkins ~]# yum -y install httpd
[root@jenkins ~]# systemctl start httpd
[root@jenkins ~]# systemctl enable httpd
[root@jenkins ~]# mkdir -p /var/www/html/deploy/pkgs
# /var/www/html/deploy/: 保存livever、lastver,即当前版本和前一个版本的版本号
# /var/www/html/deploy/pkgs/: 保存软件压缩包和它的md5值
[root@jenkins ~]# chown -R jenkins.jenkins /var/www/html/deploy/
- jenkins服务器上有很多版本,需要标明最新(当前)版本和前一版本
- jenkins服务器需要公布软件包的md5值,供文件完整性检查
编辑项目,选择下面的Additional Behavious => 新增 Checkout to a sub-directory: myweb-$webver
增加构建步骤=> Excute shell:
deploy_dir=/var/www/html/deploy
pkgs_dir=/var/www/html/deploy/pkgs
cp -r myweb-$webver $pkgs_dir # 将下载的软件目录拷贝到web服务器目录
cd $pkgs_dir
rm -rf myweb-$webver/.git # 删除版本库文件
tar czf myweb-$webver.tar.gz myweb-$webver # 打包压缩
md5sum myweb-$webver.tar.gz | awk '{print $1}' > myweb-$webver.tar.gz.md5 # 计算并保存md5值
rm -rf myweb-$webver # 删除程序目录
cd $deploy_dir
[ -f livever ] && cat livever > lastver # 将当前版本内容写到前一版本文件
echo $webver > livever # 更新当前版本
- 执行任务:Build with Parameters,选择webver1.0,用于构建项目

[root@jenkins ~]# ls /var/www/html/deploy/
livever pkgs
[root@jenkins ~]# cat /var/www/html/deploy/livever
1.0
[root@jenkins ~]# ls /var/www/html/deploy/pkgs/
myweb-1.0.tar.gz myweb-1.0.tar.gz.md5
- 再次执行任务:Build with Parameters,选择webver2.0,再次构建项目,参看jenkins服务器构建的项目
[root@jenkins ~]# ls /var/www/html/deploy/pkgs/
myweb-1.0.tar.gz myweb-1.0.tar.gz.md5 myweb-2.0.tar.gz myweb-2.0.tar.gz.md5
[root@jenkins ~]# cat /var/www/html/deploy/livever
2.0
[root@jenkins ~]# cat /var/www/html/deploy/lastver
1.0
#################################################################
编写python工具,实现代码自动上线
- 检查是否有新版本
- 如果有新版本,下载
- 检查下载的压缩包是否损坏
- 如果没有损坏,部署
将物理主机作为应用服务器,对外提供服务:
/var/www/download/:用于存储下载的软件包
/var/www/deploy/:用于存储当前版本文件和解压后的软件包
/var/www/html/link:指向软件目录的链接
[root@jenkins ~]# mkdir /var/www/download
[root@jenkins ~]# mkdir /var/www/deploy
在后端web应用服务器(这里将jenkins作为web应用服务器,实际生产环境应为独立的web服务器)编写自动化部署python程序
[root@jenkins ~]# vim bushu_web.py
import requests
import wget
import os
import hashlib
import tarfile
def has_new_ver(ver_url, ver_fname):
# 如果本地没有版本文件,则有新版本
if not os.path.isfile(ver_fname):
return True
# 取出本地版本
with open(ver_fname) as fobj:
local_ver = fobj.read()
# 如果本地版本和远程版本不一致,则有新版本
r = requests.get(ver_url)
if local_ver != r.text:
return True
return False
def check_pkgs(md5_url, pkg_path):
# 取出远程md5值
r = requests.get(md5_url)
remote_md5 = r.text.strip()
m = hashlib.md5()
with open(pkg_path, 'rb') as fobj:
while True:
data = fobj.read(4096)
if not data:
break
m.update(data)
local_md5 = m.hexdigest()
if local_md5 == remote_md5:
return True
return False
def deploy(pkg_path, deploy_dir, version):
dest = '/var/www/html/link'
src = '/var/www/deploy/myweb-%s' % version
tar = tarfile.open(pkg_path, 'r')
tar.extractall(path=deploy_dir) //解压缩到deploy_dir
tar.close()
# 如果目标链接已存在,先删除,否则无法创建
if os.path.exists(dest):
os.remove(dest)
# 创建链接
os.symlink(src, dest)
if __name__ == '__main__':
# 如果没有新版本则退出
ver_url = 'http://192.168.122.73/deploy/livever'
ver_fname = '/var/www/deploy/livever'
download_dir = '/var/www/download/'
deploy_dir = '/var/www/deploy/'
if not has_new_ver(ver_url, ver_fname):
print('没有发现新版本')
exit(1)
# 有新版本,下载软件压缩包
r = requests.get(ver_url)
version = r.text.strip() //strip() 方法用于移除字符串头尾指定的字符(默认为空格或换行符)或字符序列
pkg_url = 'http://192.168.122.73/deploy/pkgs/myweb-%s.tar.gz' % version
wget.download(pkg_url, download_dir) //将pkg_url指定内容下载到download_dir
# 检查软件包是否损坏
pkg_path = pkg_url.split('/')[-1]
pkg_path = os.path.join(download_dir, pkg_path)
md5_url = pkg_url + '.md5'
if not check_pkgs(md5_url, pkg_path):
print('软件包已损坏')
os.remove(pkg_path)
exit(2)
# 如果软件压缩包是完好的,则部署,并更新软件版本文件
deploy(pkg_path, deploy_dir, version)
with open(ver_fname, 'w') as fobj:
fobj.write(r.text)
#开发人员编写代码
[root@localhost myweb]# vim index.html
[root@localhost myweb]# cat index.html
<h1>My Web Site</h1>
<h2>2nd version</h2>
<img src="http://img4.imgtn.bdimg.com/it/u=1725415784,3706433400&fm=26&gp=0.jpg">
<h4>4th version</h4>
[root@localhost myweb]# git add .
[root@localhost myweb]# git commit -m "web4.0" //提交新版本
[root@localhost myweb]# git tag 4.0
[root@localhost myweb]# git push -u origin --all
[root@localhost myweb]# git push -u origin --tags
Total 0 (delta 0), reused 0 (delta 0)
To git@192.168.122.124:devops/myweb.git
* [new tag] 4.0 -> 4.0
#运维人员基于4.0版本构建项目
运行自动化部署脚本
[root@jenkins ~]# python3 bushu_web.py
100% [..............................................................] 279 / 279
浏览器访问http://192.168.122.73/link/,页面更新至4.0版本
更多推荐




所有评论(0)