Skip to content

Commit e5cf55e

Browse files
authored
Update 2.yml
1 parent f0c367d commit e5cf55e

1 file changed

Lines changed: 42 additions & 67 deletions

File tree

.github/workflows/2.yml

Lines changed: 42 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Generate and Manage Individual File Checksums
1+
name: Generate Checksums and Upload as Artifact
22

33
on:
44
push:
@@ -7,82 +7,57 @@ on:
77
paths:
88
- 'files/**'
99
- '!.github/**'
10-
- '!files/**/*.sha256' # از اجرای مجدد به خاطر فایل‌های checksum جلوگیری می‌کند
10+
- '!files/**/*.sha256'
1111

1212
workflow_dispatch:
1313

1414
jobs:
15-
generate-checksums:
16-
permissions:
17-
contents: write
18-
15+
generate-and-upload:
1916
runs-on: ubuntu-latest
20-
2117
steps:
2218
- name: Checkout repository
2319
uses: actions/checkout@v4
2420

25-
- name: Generate and Save Individual Checksums
26-
id: generate_checksums
21+
- name: Generate Checksum Files
2722
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 ---"
2951
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
8054
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
8357
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

Comments
 (0)