- 在docker配置好Jenkins
- 在Jenkins所在的容器装python环境
- 配置好你的git【可以尝试git clone 项目】
- 进入Jenkins所在的容器新建 /projects 项目,并chmod 777 -R /projects
- 准备好python脚本文件如下
脚本文件:
# -*- coding:utf-8 -*-"""@Author: 1991cfp@Date: 2021/8/6 11:08@LastEditTime: 2021/8/6 11:08@LastEditors: 1991cfp@Description: """import shutilimport subprocessimport jsonimport osimport sysimport timefrom git.repo import Repoimport requestsimport astimport pandas as pdfrom requests.auth import HTTPBasicAuthclass UpdateFileToGit(object): def __init__(self, git_url, svn_url, git_local_path, project_name, files, branch, temp_path): self.git_url = git_url self.svn_url = svn_url self.git_local_path = git_local_path self.branch = branch self.temp_path = temp_path self.project_name = project_name self.files = files # json 文件保存路径 json_data_path = os.path.join( os.path.join(os.path.join(os.path.join(git_local_path, project_name), "project"), "common"), "config") self.json_data_path = os.path.join(json_data_path, "game_data") def check_local_git(self): download_path = os.path.join(self.git_local_path, self.project_name) if os.path.isdir(download_path): print("删除文件") shutil.rmtree(download_path) # giturl:git 地址 克隆到本地的 download_path目录下,并指定分支为branch print("git clone 到本地文件".center(50, "-")) Repo.clone_from(self.git_url, to_path=download_path, branch=self.branch) def get_files(self): file_names = [] for file in self.files: file_name = os.path.join(self.temp_path, file) response = requests.get(self.svn_url + file, auth=HTTPBasicAuth("1991cfp", "oNIQu2s1gibfDGYk1ahaboG03")) #填你的svn地址 if response.status_code == 200: file_names.append(file_name) with open(file_name, mode="wb") as f: print(f"{file}文件已下载".center(50,"-")) f.write(response.content) else: print(file, "获取文件不存在") return file_names def excel_to_json(self, save_files): for file in save_files: df = pd.read_excel(file, sheet_name=None) sheets = [i for i in list(df.keys()) if "Sheet" not in i] for sheet in sheets: json_data = {} df = pd.read_excel(file, sheet_name=sheet) df.fillna("", inplace=True) columns = list(df.keys()) invide_column = columns[2:columns.index("end")] for i in range(3, len(df)): p_data = df.iloc[i] if p_data[columns[0]] != "end": json_data[str(p_data[invide_column[0]])] = {} for column in invide_column: json_data[str(p_data[invide_column[0]])][column] = str(p_data[column]) if p_data[columns[0]] == "end": break # 创建保存项目的json path if not os.path.isdir(self.json_data_path): os.makedirs(self.json_data_path) file_path = os.path.join(self.json_data_path, sheet + ".json") with open(file_path, "w", encoding="utf8") as f: f.write(json.dumps(json_data)) print(f"{sheet}.json文件已经保存到data_json目录中".center(50,"-")) def commintToGit(self): print("正在提交文件到git".center(50,"-")) download_path = os.path.join(self.git_local_path, self.project_name) print(download_path,"download_path") repo = Repo(download_path) repo.config_writer().set_value("user","name",git_name).release() repo.config_writer().set_value("user","email",git_email).release() repo.git.add(".") rq = time.strftime('%Y-%m-%d', time.localtime(time.time())) repo.git.commit(m="jenkines 表刷{0}".format(rq)) # 提交commit命令,实际git命令为:git commit -m "backup at time" git_url = "https://"+self.git_url.split("@")[1] print(git_url,"git_url") repo.git.push(self.git_url, self.branch) # push代码到远程仓库,可指定本地分支和远程分支名 def main(self): self.check_local_git() files = self.get_files() if files: self.excel_to_json(files) self.commintToGit()if __name__ == '__main__': #git_url, svn_url, git_local_path, project_name, files, branch, temp_path #git_url = "https://gitee.com/cfp_2020/yfs_work.git" git_name = "1991cfp" git_email = "954742660@qq.com" git_url = sys.argv[1] svn_url = sys.argv[2] project_name = sys.argv[3] files = sys.argv[4].split(",") branch = sys.argv[5] print("git_url",git_url) print("svn_url",svn_url) print("project_name",project_name) print("files",files,type(files)) print("get branch",branch) #svn_url = "http://tdsvn.master.gao7.com:8080/tandy2019/prj_pxm_all/data/sncj_lhy/version/3in1/static/excel/" git_local_path = "/projects" #git_local_path = r"C:\Users\20210226\Desktop\git_test" #project_name = "yfs_work" #files = '["E.俄罗斯方块.xlsx","G.公路赛车.xlsx"]' #branch = "develop" temp_path = "/tmp" #temp_path = r"C:\Users\20210226\Desktop\git_test" obj = UpdateFileToGit(git_url,svn_url,git_local_path,project_name,files,branch,temp_path) obj.main()
Jenkins配置



#!/bin/bashgit_url="https://18018742292:cfp1314520@gitee.com/cfp_2020/yfs_work.git"svn_url="http://tdsvn.master.gao7.com:8080/tandy2019/prj_pxm_all/data/tbfw_qyy/version/develop/static/excel/"project_name="yfs_work"echo $file_listpython /projects/file.py $git_url $svn_url $project_name $file_list $server_branch