Skip to content

Generate Checksums and Upload as Artifact #5

Generate Checksums and Upload as Artifact

Generate Checksums and Upload as Artifact #5

Workflow file for this run

name: Generate File Checksums
on:
push:
branches:
- main
paths:
- 'files/**'
- '!.github/**'
- '!**/*.sha256' # از اجرای مجدد workflow به خاطر فایل‌های checksum جلوگیری می‌کند
workflow_dispatch:
jobs:
generate-checksums:
# به این جاب اجازه نوشتن در ریپازیتوری را می‌دهد
permissions:
contents: write
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Generate and Save Individual Checksums
id: generate_checksums
run: |
TARGET_DIR="files"
# اسکریپت پایتون برای ساخت فایل‌های checksum جداگانه
cat << 'EOF' > generate_checksums.py
import os
import hashlib
import sys
def create_individual_checksums(root_dir):
"""
برای هر فایل در دایرکتوری مشخص شده، یک فایل .sha256 جداگانه ایجاد می‌کند.
"""
for dirpath, _, filenames in os.walk(root_dir):
for filename in filenames:
# از پردازش فایل‌های checksum موجود صرف نظر می‌کند
if filename.endswith('.sha256') or ".git" in dirpath:
continue
file_path = os.path.join(dirpath, filename)
output_filepath = f"{file_path}.sha256"
# محاسبه هش SHA256
sha256_hash = hashlib.sha256()
with open(file_path, "rb") as f:
# خواندن فایل به صورت تکه‌تکه برای مدیریت فایل‌های بزرگ
for byte_block in iter(lambda: f.read(4096), b""):
sha256_hash.update(byte_block)
digest = sha256_hash.hexdigest()
# نوشتن هش در فایل .sha256 مخصوص همان فایل
with open(output_filepath, "w", encoding="utf-8") as f_out:
# فرمت استاندارد: هش *نام_فایل
f_out.write(f"{digest} *{os.path.basename(file_path)}\n")
print(f"Checksum for {filename} saved to {output_filepath}")
if __name__ == "__main__":
if len(sys.argv) > 1:
target_directory = sys.argv[1]
if os.path.isdir(target_directory):
create_individual_checksums(target_directory)
else:
print(f"Error: Directory '{target_directory}' not found.")
sys.exit(1)
else:
print("Error: Please provide the target directory.")
sys.exit(1)
EOF
# اجرای اسکریپت پایتون
python generate_checksums.py $TARGET_DIR
- name: Commit and push if changed
uses: stefanzweifel/git-auto-commit-action@v5
with:
commit_message: "ci: Update file checksums"
# الگو برای پیدا کردن تمام فایل‌های .sha256 در پوشه files و زیرپوشه‌های آن
file_pattern: "files/**/*.sha256"
commit_user_name: "GitHub Actions Bot"
commit_user_email: "github-actions[bot]@users.noreply.github.com"