Skip to content

测试actiong

测试actiong #41

Workflow file for this run

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