-
Notifications
You must be signed in to change notification settings - Fork 3
102 lines (86 loc) · 3.36 KB
/
push-data.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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 }}
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(force_upload)
with open('Data/version', 'r') as f:
current_version = json.load(f)
previous_version = get_previous_version()
with open('Data/version', 'r') as f:
data = json.load(f)
files_to_upload = {
"program": ["Window.py", "Queryer.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)
bucket.put_object_from_file('version', 'Data/version')
print(f"Uploaded program files to OSS.")
elif 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')
bucket.put_object_from_file('item.zip', 'item.zip')
print(f"Uploaded data file to OSS.")
shell: python