Browse Source

增加项目路径和上一次提交文件的环境变量支持

master
wxz 2 years ago
parent
commit
d95694a971
  1. 2
      docker-compose.yml
  2. 2
      lastcommit.txt
  3. 43
      main.py
  4. 15
      test.py

2
docker-compose.yml

@ -8,6 +8,8 @@ services:
- "/Volumes/data/ws_python/wxz/JenkinsWebHookTrigger/lastcommit.txt:/lastcommit.txt" - "/Volumes/data/ws_python/wxz/JenkinsWebHookTrigger/lastcommit.txt:/lastcommit.txt"
environment: environment:
RUN_INSTRUCT: "python /main.py runserver 0.0.0.0:7998" RUN_INSTRUCT: "python /main.py runserver 0.0.0.0:7998"
JWHT_GIT_PROJECT_DIR: "/epmet-saas"
JWHT_LAST_DEPLOY_COMMIT_RECORD: "/lastcommit.txt"
restart: "unless-stopped" restart: "unless-stopped"
logging: logging:
driver: local driver: local

2
lastcommit.txt

@ -1 +1 @@
5b0ad41e70f65873bfcb096b74c4d16e3c7a4898 83607de4be0a6c7a8911c51bd4878e2190dd90d9

43
main.py

@ -15,20 +15,15 @@ epmet_root_svc_name_reg = "^([^/]+)/.*$"
epmet_module_svc_name_reg = "^epmet-module/([^/]+)/.*$" epmet_module_svc_name_reg = "^epmet-module/([^/]+)/.*$"
commit_regex = "^([0-9a-z]{40}) .*$" commit_regex = "^([0-9a-z]{40}) .*$"
max_commit_qty = 20 # 项目绝对路径
git_project_env = os.environ.get('JWHT_GIT_PROJECT_DIR')
git_project_dir = git_project_env if git_project_env else "epmet-saas"
def findIndexOfLastCommit(commits: list, last_commit_ver: str): # 上一次提交记录文件
idx: int = None last_deploy_commit_record = os.environ.get('JWHT_LAST_DEPLOY_COMMIT_RECORD')
for c in commits: commit_record_file_path = last_deploy_commit_record if last_deploy_commit_record else "./lastcommit.txt"
if c.startswith(last_commit_ver):
return idx
else:
if not idx:
idx = 0
idx += 1
return idx max_commit_qty = 20
def findLatestCommits(commits: list, last_commit_ver: str, max_amount: int): def findLatestCommits(commits: list, last_commit_ver: str, max_amount: int):
@ -63,7 +58,7 @@ def findModuleList(commits: str):
service_set: set = set() service_set: set = set()
for c in commits: for c in commits:
try: try:
rst = os.popen("cd epmet-saas && git show {} --stat --name-only|grep 'epmet-'".format(c)) rst = os.popen("cd {} && git show {} --stat --name-only|grep 'epmet-'".format(git_project_dir, c))
files = rst.readlines() files = rst.readlines()
for file in files: for file in files:
svc_name = find_service_name_from_file(file) svc_name = find_service_name_from_file(file)
@ -97,7 +92,7 @@ def read_last_commit_ver():
读取上次发布到的版本号(包含) 读取上次发布到的版本号(包含)
:return: :return:
""" """
with open('./lastcommit.txt', 'rt') as lastcommit_file: with open(commit_record_file_path, 'rt') as lastcommit_file:
line_contents = lastcommit_file.readlines() line_contents = lastcommit_file.readlines()
if len(line_contents) > 0: if len(line_contents) > 0:
return line_contents[0] return line_contents[0]
@ -108,7 +103,7 @@ def write_last_commit_ver(new_ver):
写入上次发布到的版本号(包含) 写入上次发布到的版本号(包含)
:return: :return:
""" """
with open('./lastcommit.txt', 'wt') as lastcommit_file: with open(commit_record_file_path, 'wt') as lastcommit_file:
lastcommit_file.write(new_ver) lastcommit_file.write(new_ver)
@ -127,15 +122,15 @@ def deploy_svcs(svc_list):
def do(): def do():
# 切换分支 # 切换分支
os.system("cd epmet-saas && git checkout dev") os.system("cd {} && git checkout dev".format(git_project_dir))
# 更新代码 # 更新代码
os.system("cd epmet-saas && git pull") os.system("cd {} && git pull".format(git_project_dir))
last_commit_ver = read_last_commit_ver() last_commit_ver = read_last_commit_ver()
try: try:
stream = os.popen("cd epmet-saas && git log --pretty=oneline --no-merges -20") stream = os.popen("cd {} && git log --pretty=oneline --no-merges -20".format(git_project_dir))
commits = stream.readlines() commits = stream.readlines()
# 根据commits找出最近提交的commits # 根据commits找出最近提交的commits
@ -159,12 +154,16 @@ def do():
finally: finally:
stream.close() stream.close()
@app.route(rule='/on-dev-push', methods=['GET'])
def on_dev_push(): if __name__ == '__main__':
do() do()
return "success"
# @app.route(rule='/on-dev-push', methods=['GET'])
# def on_dev_push():
# do()
# return "success"
app.run(host="0.0.0.0", port=7998, debug=True) # 多线程模式 # app.run(host="0.0.0.0", port=7998, debug=True) # 多线程模式
# python main.py runserver 0.0.0.0:7999 # python main.py runserver 0.0.0.0:7999

15
test.py

@ -1,7 +1,12 @@
import re import re, os
epmet_module_svc_name_reg = "^epmet-module/([^/]+)/.*$" # 项目绝对路径
url = "epmet-module/gov-org-server/abc.java" git_project_env = os.environ.get('JWHT_GIT_PROJECT_DIR')
git_project_dir = git_project_env if git_project_env else "epmet-saas"
match = re.match(epmet_module_svc_name_reg, url) # 上一次提交记录文件
print(match.groups()) last_deploy_commit_record = os.environ.get('JWHT_LAST_DEPLOY_COMMIT_RECORD')
commit_record_file_path = last_deploy_commit_record if last_deploy_commit_record else "./lastcommit.txt"
print(git_project_dir)
print(commit_record_file_path)
Loading…
Cancel
Save