59 lines
1.8 KiB
Python
59 lines
1.8 KiB
Python
import requests
|
|
import zipfile
|
|
import os
|
|
from pathlib import Path
|
|
|
|
# Replace these with your details
|
|
GITHUB_SERVER_URL = os.getenv('GITHUB_SERVER_URL')
|
|
GITHUB_REPOSITORY = os.getenv('GITHUB_REPOSITORY')
|
|
|
|
# Set the path to your bin directory
|
|
|
|
bin_path = os.getenv('bin_path') or 'bin'
|
|
zip_name = os.getenv('zip_name') or 'release.zip'
|
|
rel_name = os.getenv('rel_name') or 'Content Release'
|
|
rel_desc = os.getenv('rel_desc') or 'Automatic Content Release from GITEA workflow'
|
|
|
|
access_token = os.getenv('access_token') # Make sure to set this to your Gitea access token
|
|
|
|
# Zip the files in the bin directory
|
|
with zipfile.ZipFile(zip_name, 'w') as zipf:
|
|
for root, dirs, files in os.walk(bin_path):
|
|
for file in files:
|
|
zipf.write(os.path.join(root, file), os.path.relpath(os.path.join(root, file), os.path.join(bin_path, '..')))
|
|
|
|
# Create a release
|
|
release_url = f'{GITHUB_SERVER_URL}/api/v1/repos/{GITHUB_REPOSITORY}/releases'
|
|
headers = {
|
|
'Authorization': f'token {access_token}'
|
|
}
|
|
data = {
|
|
"body": rel_desc,
|
|
"name": rel_name,
|
|
"draft": True,
|
|
"prerelease": True,
|
|
"tag_name": "v1.0.0",
|
|
"target_commitish": "master"
|
|
}
|
|
|
|
response = requests.post(release_url, json=data, headers=headers)
|
|
|
|
if response.ok:
|
|
release = response.json()
|
|
release_id = release['id']
|
|
|
|
# Create a release attachment
|
|
attachment_url = f'{GITHUB_SERVER_URL}/api/v1/repos/{GITHUB_REPOSITORY}/releases/{release_id}/assets'
|
|
files = {
|
|
'attachment': (zip_name, open(zip_name, 'rb'))
|
|
}
|
|
|
|
response = requests.post(attachment_url, headers=headers, files=files)
|
|
|
|
if response.ok:
|
|
print("Attachment uploaded successfully.")
|
|
else:
|
|
print("Failed to upload attachment:", response.text)
|
|
else:
|
|
print("Failed to create release:", response.text)
|