|
1 | | -name: Generate and Manage Individual File Checksums |
| 1 | +name: Generate Checksums and Upload as Artifact |
2 | 2 |
|
3 | 3 | on: |
4 | 4 | push: |
|
7 | 7 | paths: |
8 | 8 | - 'files/**' |
9 | 9 | - '!.github/**' |
10 | | - - '!files/**/*.sha256' # از اجرای مجدد به خاطر فایلهای checksum جلوگیری میکند |
| 10 | + - '!files/**/*.sha256' |
11 | 11 |
|
12 | 12 | workflow_dispatch: |
13 | 13 |
|
14 | 14 | jobs: |
15 | | - generate-checksums: |
16 | | - permissions: |
17 | | - contents: write |
18 | | - |
| 15 | + generate-and-upload: |
19 | 16 | runs-on: ubuntu-latest |
20 | | - |
21 | 17 | steps: |
22 | 18 | - name: Checkout repository |
23 | 19 | uses: actions/checkout@v4 |
24 | 20 |
|
25 | | - - name: Generate and Save Individual Checksums |
26 | | - id: generate_checksums |
| 21 | + - name: Generate Checksum Files |
27 | 22 | run: | |
28 | | - TARGET_DIR="files" |
| 23 | + echo "--- Starting Checksum Generation ---" |
| 24 | + # 1. یک پوشه تمیز برای نگهداری فایلهای خروجی میسازیم |
| 25 | + mkdir -p generated_checksums |
| 26 | +
|
| 27 | + # 2. تمام فایلهای داخل پوشه 'files' را پیدا میکنیم |
| 28 | + find files -type f -not -name "*.sha256" | while read -r file; do |
| 29 | + echo "Processing file: $file" |
| 30 | + checksum=$(sha256sum "$file" | awk '{print $1}') |
| 31 | + base_filename=$(basename "$file") |
| 32 | + |
| 33 | + # 3. ساختار پوشه اصلی را در پوشه خروجی بازسازی میکنیم |
| 34 | + # برای مثال، اگر فایل در 'files/sub/a.txt' باشد، |
| 35 | + # مسیر نسبی 'sub' خواهد بود. |
| 36 | + relative_dir=$(dirname "${file#files/}") |
| 37 | + |
| 38 | + # اگر فایل در ریشه 'files' باشد، مسیر نسبی '.' میشود که مشکلی ندارد |
| 39 | + if [ "$relative_dir" != "." ]; then |
| 40 | + mkdir -p "generated_checksums/$relative_dir" |
| 41 | + output_path="generated_checksums/$relative_dir/$base_filename.sha256" |
| 42 | + else |
| 43 | + output_path="generated_checksums/$base_filename.sha256" |
| 44 | + fi |
| 45 | + |
| 46 | + # 4. فایل checksum را در پوشه خروجی مینویسیم |
| 47 | + echo "$checksum *$base_filename" > "$output_path" |
| 48 | + echo "--> Created: $output_path" |
| 49 | + done |
| 50 | + echo "--- Generation Complete ---" |
29 | 51 | |
30 | | - cat << 'EOF' > generate_individual_checksums.py |
31 | | - import os |
32 | | - import hashlib |
33 | | - import sys |
34 | | -
|
35 | | - def manage_individual_checksums(root_dir): |
36 | | - # پیدا کردن تمام فایلهای اصلی (غیر از خود checksum ها) |
37 | | - for dirpath, _, filenames in os.walk(root_dir): |
38 | | - # از پردازش پوشه گیت صرف نظر میکنیم |
39 | | - if ".git" in dirpath: |
40 | | - continue |
41 | | - |
42 | | - for filename in filenames: |
43 | | - # فقط فایلهایی را پردازش کن که checksum نیستند |
44 | | - if filename.endswith('.sha256'): |
45 | | - continue |
46 | | -
|
47 | | - file_path = os.path.join(dirpath, filename) |
48 | | - checksum_file_path = f"{file_path}.sha256" |
49 | | - |
50 | | - # محاسبه هش SHA256 |
51 | | - sha256_hash = hashlib.sha256() |
52 | | - with open(file_path, "rb") as f: |
53 | | - for byte_block in iter(lambda: f.read(4096), b""): |
54 | | - sha256_hash.update(byte_block) |
55 | | - |
56 | | - digest = sha256_hash.hexdigest() |
57 | | - |
58 | | - # نوشتن هش در فایل .sha256 مخصوص همان فایل |
59 | | - # فرمت استاندارد: هش *نام_فایل |
60 | | - with open(checksum_file_path, "w", encoding="utf-8") as f_out: |
61 | | - f_out.write(f"{digest} *{os.path.basename(file_path)}\n") |
62 | | - |
63 | | - print(f"--> Generated/Updated checksum for: {filename}") |
64 | | -
|
65 | | - if __name__ == "__main__": |
66 | | - if len(sys.argv) > 1: |
67 | | - target_directory = sys.argv[1] |
68 | | - if os.path.isdir(target_directory): |
69 | | - manage_individual_checksums(target_directory) |
70 | | - else: |
71 | | - print(f"Error: Directory '{target_directory}' not found.") |
72 | | - sys.exit(1) |
73 | | - else: |
74 | | - print("Error: Please provide the target directory.") |
75 | | - sys.exit(1) |
76 | | - EOF |
77 | | -
|
78 | | - # اجرای اسکریپت پایتون |
79 | | - python generate_individual_checksums.py $TARGET_DIR |
| 52 | + echo "--- Listing generated files for artifact ---" |
| 53 | + ls -R generated_checksums |
80 | 54 |
|
81 | | - - name: Commit and push if changed |
82 | | - uses: stefanzweifel/git-auto-commit-action@v5 |
| 55 | + - name: Upload Checksum Files as Artifact |
| 56 | + uses: actions/upload-artifact@v4 |
83 | 57 | with: |
84 | | - commit_message: "ci: Update individual file checksums" |
85 | | - # الگو برای پیدا کردن تمام فایلهای .sha256 جدید یا تغییر یافته |
86 | | - file_pattern: "files/**/*.sha256" |
87 | | - commit_user_name: "GitHub Actions Bot" |
88 | | - commit_user_email: "github-actions[bot]@users.noreply.github.com" |
| 58 | + # نام فایل زیپی که دانلود خواهید کرد |
| 59 | + name: checksum-files |
| 60 | + # مسیری که فایلهای ساخته شده در آن قرار دارند |
| 61 | + path: generated_checksums/ |
| 62 | + # فایلها تا ۵ روز برای دانلود باقی میمانند |
| 63 | + retention-days: 5 |
0 commit comments