Generate Checksums and Upload as Artifact #10
This file contains hidden or 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: Generate and Manage Individual File Checksums | |
| on: | |
| push: | |
| branches: | |
| - main | |
| paths: | |
| - 'files/**' | |
| - '!.github/**' | |
| - '!files/**/*.sha256' # از اجرای مجدد به خاطر فایلهای 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" | |
| cat << 'EOF' > generate_individual_checksums.py | |
| import os | |
| import hashlib | |
| import sys | |
| def manage_individual_checksums(root_dir): | |
| # پیدا کردن تمام فایلهای اصلی (غیر از خود checksum ها) | |
| for dirpath, _, filenames in os.walk(root_dir): | |
| # از پردازش پوشه گیت صرف نظر میکنیم | |
| if ".git" in dirpath: | |
| continue | |
| for filename in filenames: | |
| # فقط فایلهایی را پردازش کن که checksum نیستند | |
| if filename.endswith('.sha256'): | |
| continue | |
| file_path = os.path.join(dirpath, filename) | |
| checksum_file_path = 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(checksum_file_path, "w", encoding="utf-8") as f_out: | |
| f_out.write(f"{digest} *{os.path.basename(file_path)}\n") | |
| print(f"--> Generated/Updated checksum for: {filename}") | |
| if __name__ == "__main__": | |
| if len(sys.argv) > 1: | |
| target_directory = sys.argv[1] | |
| if os.path.isdir(target_directory): | |
| manage_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_individual_checksums.py $TARGET_DIR | |
| - name: Commit and push if changed | |
| uses: stefanzweifel/git-auto-commit-action@v5 | |
| with: | |
| commit_message: "ci: Update individual file checksums" | |
| # الگو برای پیدا کردن تمام فایلهای .sha256 جدید یا تغییر یافته | |
| file_pattern: "files/**/*.sha256" | |
| commit_user_name: "GitHub Actions Bot" | |
| commit_user_email: "github-actions[bot]@users.noreply.github.com" |