Generate File Checksums #3
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 File Checksums | |
| on: | |
| push: | |
| branches: | |
| - main | |
| paths: | |
| - 'files/**' | |
| - '!.github/**' | |
| workflow_dispatch: | |
| jobs: | |
| generate-checksums: | |
| # --- این بخش اضافه شده است --- | |
| # به این جاب اجازه نوشتن در ریپازیتوری را میدهد | |
| permissions: | |
| contents: write | |
| # ----------------------------- | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout repository | |
| uses: actions/checkout@v4 | |
| - name: Generate and Save Checksums | |
| id: generate_checksums | |
| run: | | |
| TARGET_DIR="files" | |
| OUTPUT_FILE="checksums.sha256" | |
| cat << 'EOF' > generate_checksums.py | |
| import os | |
| import hashlib | |
| import sys | |
| def create_checksums(root_dir, output_filename): | |
| checksums = {} | |
| output_filepath = os.path.join(root_dir, output_filename) | |
| for dirpath, _, filenames in os.walk(root_dir): | |
| for filename in filenames: | |
| file_path = os.path.join(dirpath, filename) | |
| if file_path == output_filepath or ".git" in file_path: | |
| continue | |
| 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) | |
| relative_path = os.path.relpath(file_path, root_dir) | |
| checksums[relative_path] = sha256_hash.hexdigest() | |
| with open(output_filepath, "w", encoding="utf-8") as f: | |
| for path, digest in sorted(checksums.items()): | |
| f.write(f"{digest} *{path.replace(os.sep, '/')}\n") | |
| print(f"Checksums saved to {output_filepath}") | |
| if __name__ == "__main__": | |
| target_directory = sys.argv[1] | |
| output_file_name = sys.argv[2] | |
| if os.path.isdir(target_directory): | |
| create_checksums(target_directory, output_file_name) | |
| else: | |
| print(f"Error: Directory '{target_directory}' not found.") | |
| sys.exit(1) | |
| EOF | |
| python generate_checksums.py $TARGET_DIR $OUTPUT_FILE | |
| - name: Commit and push if changed | |
| uses: stefanzweifel/git-auto-commit-action@v5 | |
| with: | |
| commit_message: "ci: Update file checksums" | |
| file_pattern: files/checksums.sha256 | |
| commit_user_name: "GitHub Actions Bot" | |
| commit_user_email: "github-actions[bot]@users.noreply.github.com" |