一套简单的git版本控制代码

时间:2023-03-08 18:36:12

对于博客来说,我还是直接实践比较好,理论过多,不方便以后的查看

废话不多,直接开干

功能需求:

.公司需要将jenkins打包出来的压缩包通过git上传到git服务器
.而且通过版本控制上传的文件,即分支名为版本名
.git pull上传到git服务器,并且触发post-receive钩子对git服务器文件进行拉取到对应目录解压

功能图表展示:

 1.创建git文件

一套简单的git版本控制代码

2.版本控制

一套简单的git版本控制代码

3.钩子处理(做软连接)

一套简单的git版本控制代码

4.转移文件的目录

一套简单的git版本控制代码

目录结构

 客户端
---git
--client.zip
--server.zip
---version #存放版本文件
--156622635175 #以时间戳存放
---version.txt #存放需要git上传的目录名
git服务端
---git.git
--hooks
--post-receive
--authoup
--post_receive.py
--config.py
...

客户端:

config.py:

 #项目上传git的配置信息

 git_url = 'ssh://git@git.git'

 git_dir = 'D:/git'

 version_dir = 'D:/versions'

 time_stamp = 'D:/version.txt'

 pushgit.py:

 # -*- coding: utf-8 -*-
import os
import shutil
import sys, re
import traceback
import zipfile
# dir = os.path.dirname(os.path.abspath(__file__),'/../')
sys.path.append(os.path.dirname(os.path.abspath(__file__)) + "/../")
from jenkins_structure import settings # 获取到源码目录名
def get_time_stamp_name():
with open(settings.time_stamp, "r", encoding="utf-8") as f:
time_stamp = f.read()
return time_stamp # 推送源码到git服务器
def push_git(version_name, version_comment):
time_stamp = get_time_stamp_name()
get_version_dir = settings.version_dir + "\\" + time_stamp try:
#判断是否存在git目录,不存在从则git库克隆下来
if os.path.exists(settings.git_dir) != True:
items_dir = os.path.dirname(os.path.abspath(settings.git_dir))
os.chdir(items_dir)
os.system("git clone %s" % settings.git_url )
os.system("git checkout -b master")
# 切换到存放版本目录并切换master分支,防止其他分支对其造成错误
os.chdir(settings.git_dir)
except Exception as e:
traceback.print_exc()
os.system("git checkout master -f")
# 创建git版本分支并切换工作分支到该分支
os.system("git checkout -b %s" % version_name)
# 拷贝源码到新的版本目录
shutil.copyfile(get_version_dir + '\\' + "client.zip", settings.git_dir + "\\" + "client.zip")
shutil.copyfile(get_version_dir + '\\' + "server.zip", settings.git_dir + "\\" + "server.zip")
# # 提交并推送到远程仓库
os.system("git add .")
os.system("git commit -m '%s'" % version_comment)
os.system("git push origin %s" % version_name)

 git服务器:

config.py:

 #git服务器
git_dir = "/data/git.git" #获取更新后的git远程库的目录
pull_git = "/data/git" # 存放服务器下的目录
server_dir = "/data/git/server" #存放client下的目录
client_dir = "/data/git/client"

post-receive.py:

 #!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import subprocess
import sys, shutil, traceback
import zipfile
import logging sys.path.append(os.path.dirname(os.path.abspath(__file__)) + "/")
import config
import ossupload # 读取传入钩子中的内容并获取其中的版本号
def read():
text = str(raw_input())
args = text.split(' ')
version = args[2].replace('refs/heads/','')
return version # 解压文件
def extract():
f = zipfile.ZipFile(config.client_dir + "/" + "client.zip", 'r')
for file in f.namelist():
f.extract(file, config.client_dir) def get_newversion(version_name):
try:
# 把git服务器上的压缩包复制到client目录
os.system('git --git-dir=%s --work-tree=%s checkout -f %s' % (config.git_dir, config.client_dir, version_name))
# 把server.zip以版本号重命名并移动到server目录
shutil.copyfile(config.client_dir + '/' + 'server.zip', config.server_dir + '/' + version_name + '.zip')
os.remove(config.client_dir + '/' + 'server.zip')
# 把client.zip解压并重命名
extract()
os.rename(config.client_dir + '/' + 'client', config.client_dir + '/' + version_name)
os.remove(config.client_dir + '/' + 'client.zip')
except Exception as e:
traceback.print_exc()
print(repr(e)) if __name__ == '__main__':
try:
os.unsetenv('unset GIT_DIR')
version_name = read()
get_newversion(version_name)
src = config.client_dir + "/" + version_name + "/"
file_name = os.listdir(src)
src = config.client_dir + "/" + version_name + "/" + file_name[0] + "/"
ossupload.ossutil_upload(src, version_name)
except Exception as e:
traceback.print_exc()
print(repr(e))

 git服务器配置内容

1.在hooks/post-receive 做一个软连接
2.执行用户为git用户,git:git 在git组,所有需要确保一套流程下来需要涉及的目录和文件,git用户都可以有对应的权限

图示:

一套简单的git版本控制代码

一套简单的git版本控制代码

一套简单的git版本控制代码