1. 在docker配置好Jenkins
  2. 在Jenkins所在的容器装python环境
  3. 配置好你的git【可以尝试git clone 项目】
  4. 进入Jenkins所在的容器新建 /projects 项目,并chmod 777 -R /projects
  5. 准备好python脚本文件如下

脚本文件:

  1. # -*- coding:utf-8 -*-
  2. """
  3. @Author: 1991cfp
  4. @Date: 2021/8/6 11:08
  5. @LastEditTime: 2021/8/6 11:08
  6. @LastEditors: 1991cfp
  7. @Description:
  8. """
  9. import shutil
  10. import subprocess
  11. import json
  12. import os
  13. import sys
  14. import time
  15. from git.repo import Repo
  16. import requests
  17. import ast
  18. import pandas as pd
  19. from requests.auth import HTTPBasicAuth
  20. class UpdateFileToGit(object):
  21. def __init__(self, git_url, svn_url, git_local_path, project_name, files, branch, temp_path):
  22. self.git_url = git_url
  23. self.svn_url = svn_url
  24. self.git_local_path = git_local_path
  25. self.branch = branch
  26. self.temp_path = temp_path
  27. self.project_name = project_name
  28. self.files = files
  29. # json 文件保存路径
  30. json_data_path = os.path.join(
  31. os.path.join(os.path.join(os.path.join(git_local_path, project_name), "project"), "common"), "config")
  32. self.json_data_path = os.path.join(json_data_path, "game_data")
  33. def check_local_git(self):
  34. download_path = os.path.join(self.git_local_path, self.project_name)
  35. if os.path.isdir(download_path):
  36. print("删除文件")
  37. shutil.rmtree(download_path)
  38. # giturl:git 地址 克隆到本地的 download_path目录下,并指定分支为branch
  39. print("git clone 到本地文件".center(50, "-"))
  40. Repo.clone_from(self.git_url, to_path=download_path, branch=self.branch)
  41. def get_files(self):
  42. file_names = []
  43. for file in self.files:
  44. file_name = os.path.join(self.temp_path, file)
  45. response = requests.get(self.svn_url + file, auth=HTTPBasicAuth("1991cfp", "oNIQu2s1gibfDGYk1ahaboG03")) #填你的svn地址
  46. if response.status_code == 200:
  47. file_names.append(file_name)
  48. with open(file_name, mode="wb") as f:
  49. print(f"{file}文件已下载".center(50,"-"))
  50. f.write(response.content)
  51. else:
  52. print(file, "获取文件不存在")
  53. return file_names
  54. def excel_to_json(self, save_files):
  55. for file in save_files:
  56. df = pd.read_excel(file, sheet_name=None)
  57. sheets = [i for i in list(df.keys()) if "Sheet" not in i]
  58. for sheet in sheets:
  59. json_data = {}
  60. df = pd.read_excel(file, sheet_name=sheet)
  61. df.fillna("", inplace=True)
  62. columns = list(df.keys())
  63. invide_column = columns[2:columns.index("end")]
  64. for i in range(3, len(df)):
  65. p_data = df.iloc[i]
  66. if p_data[columns[0]] != "end":
  67. json_data[str(p_data[invide_column[0]])] = {}
  68. for column in invide_column:
  69. json_data[str(p_data[invide_column[0]])][column] = str(p_data[column])
  70. if p_data[columns[0]] == "end":
  71. break
  72. # 创建保存项目的json path
  73. if not os.path.isdir(self.json_data_path):
  74. os.makedirs(self.json_data_path)
  75. file_path = os.path.join(self.json_data_path, sheet + ".json")
  76. with open(file_path, "w", encoding="utf8") as f:
  77. f.write(json.dumps(json_data))
  78. print(f"{sheet}.json文件已经保存到data_json目录中".center(50,"-"))
  79. def commintToGit(self):
  80. print("正在提交文件到git".center(50,"-"))
  81. download_path = os.path.join(self.git_local_path, self.project_name)
  82. print(download_path,"download_path")
  83. repo = Repo(download_path)
  84. repo.config_writer().set_value("user","name",git_name).release()
  85. repo.config_writer().set_value("user","email",git_email).release()
  86. repo.git.add(".")
  87. rq = time.strftime('%Y-%m-%d', time.localtime(time.time()))
  88. repo.git.commit(m="jenkines 表刷{0}".format(rq)) # 提交commit命令,实际git命令为:git commit -m "backup at time"
  89. git_url = "https://"+self.git_url.split("@")[1]
  90. print(git_url,"git_url")
  91. repo.git.push(self.git_url, self.branch) # push代码到远程仓库,可指定本地分支和远程分支名
  92. def main(self):
  93. self.check_local_git()
  94. files = self.get_files()
  95. if files:
  96. self.excel_to_json(files)
  97. self.commintToGit()
  98. if __name__ == '__main__':
  99. #git_url, svn_url, git_local_path, project_name, files, branch, temp_path
  100. #git_url = "https://gitee.com/cfp_2020/yfs_work.git"
  101. git_name = "1991cfp"
  102. git_email = "954742660@qq.com"
  103. git_url = sys.argv[1]
  104. svn_url = sys.argv[2]
  105. project_name = sys.argv[3]
  106. files = sys.argv[4].split(",")
  107. branch = sys.argv[5]
  108. print("git_url",git_url)
  109. print("svn_url",svn_url)
  110. print("project_name",project_name)
  111. print("files",files,type(files))
  112. print("get branch",branch)
  113. #svn_url = "http://tdsvn.master.gao7.com:8080/tandy2019/prj_pxm_all/data/sncj_lhy/version/3in1/static/excel/"
  114. git_local_path = "/projects"
  115. #git_local_path = r"C:\Users\20210226\Desktop\git_test"
  116. #project_name = "yfs_work"
  117. #files = '["E.俄罗斯方块.xlsx","G.公路赛车.xlsx"]'
  118. #branch = "develop"
  119. temp_path = "/tmp"
  120. #temp_path = r"C:\Users\20210226\Desktop\git_test"
  121. obj = UpdateFileToGit(git_url,svn_url,git_local_path,project_name,files,branch,temp_path)
  122. obj.main()

Jenkins配置

image.png

image.png

image.png

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