优化自动检测的工作流 #67
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Push to OSS | |
on: | |
push: | |
branches: | |
- master | |
workflow_dispatch: | |
inputs: | |
force_upload: | |
description: 'Force Upload file to OSS' | |
required: false | |
default: false | |
jobs: | |
check-version-and-push: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v3 | |
with: | |
fetch-depth: 3 | |
# - name: Detect changes in version file | |
# id: changes | |
# uses: dorny/paths-filter@v2 | |
# with: | |
# filters: | | |
# version: | |
# - 'Data/version' | |
- name: Setup Python | |
uses: actions/setup-python@v4 | |
with: | |
python-version: '3.8' | |
- name: Install dependencies | |
run: | | |
python -m pip install --upgrade pip | |
pip install oss2 | |
- name: Parse version and upload to OSS | |
env: | |
OSS_ACCESS_KEY_ID: ${{ secrets.OSS_ACCESS_KEY_ID }} | |
OSS_ACCESS_KEY_SECRET: ${{ secrets.OSS_ACCESS_KEY_SECRET }} | |
OSS_BUCKET_NAME: ${{ secrets.OSS_BUCKET_NAME }} | |
OSS_ENDPOINT: ${{ secrets.OSS_ENDPOINT }} | |
FORCE_UPLOAD: ${{ github.event.inputs.force_upload }} | |
run: | | |
import os | |
import json | |
from oss2 import Auth, Bucket | |
import subprocess | |
import zipfile | |
def get_previous_version(): | |
last_commit = subprocess.check_output(['git', 'rev-parse', 'HEAD^1']).strip().decode() | |
try: | |
output = subprocess.check_output(['git', 'show', f'{last_commit}:Data/version'], stderr=subprocess.STDOUT) | |
return json.loads(output.decode()) | |
except subprocess.CalledProcessError as e: | |
if "does not exist" in e.output.decode(): | |
print('no need update') | |
return None | |
raise | |
force_upload = os.environ['FORCE_UPLOAD'] | |
print(f"force_upload: {force_upload}") | |
if force_upload == "true": | |
current_version = {"program": "latest", "data": "latest"} | |
else: | |
with open('Data/version', 'r') as f: | |
current_version = json.load(f) | |
print(f"current_version: {current_version}") | |
previous_version = get_previous_version() | |
print(f"previous_version: {previous_version}") | |
with open('Data/version', 'r') as f: | |
data = json.load(f) | |
files_to_upload = { | |
"program": ["Window.py", "Queryer.py"], | |
"UI": ["UI/check_update.py", "UI/cost_page.py", "UI/history_page.py", "UI/loading_page.py", "UI/main_window.py", "UI/query_item_id.py", "UI/select_item_list.py", "UI/show_price.py"], | |
"data": ["Data/item.Pdt"], | |
"version": ['Data/version'] | |
} | |
auth = Auth(os.environ['OSS_ACCESS_KEY_ID'], os.environ['OSS_ACCESS_KEY_SECRET']) | |
bucket = Bucket(auth, os.environ['OSS_ENDPOINT'], os.environ['OSS_BUCKET_NAME']) | |
header = {'Content-Type': 'text/plain', 'Content-Encoding': 'utf-8'} | |
if not previous_version or current_version != previous_version: | |
if current_version['program'] != previous_version['program']: | |
files = files_to_upload["program"] | |
# 上传文件 | |
for file in files: | |
bucket.put_object_from_file(file, file, headers=header) | |
ui_files = files_to_upload["UI"] | |
for file in ui_files: | |
bucket.put_object_from_file(file, file, headers=header) | |
bucket.put_object_from_file('version', 'Data/version') | |
bucket.put_object_from_file('Paissa.int', 'Paissa.py') | |
print(f"Uploaded program files to OSS.") | |
if current_version['data'] != previous_version['data']: | |
files = files_to_upload["data"] | |
bucket.put_object_from_file('version', 'Data/version') | |
with zipfile.ZipFile('item.zip', 'w') as zipf: | |
zipf.write('Data/item.Pdt', arcname="item.Pdt") | |
bucket.put_object_from_file('item.zip', 'item.zip') | |
print(f"Uploaded data file to OSS.") | |
shell: python |