Generate Checksums and Upload as Artifact #8
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 File Checksums | |
| on: | |
| push: | |
| branches: | |
| - main | |
| paths: | |
| - 'files/**' | |
| - '!.github/**' | |
| - '!files/**/*.sha256' | |
| workflow_dispatch: | |
| jobs: | |
| generate-checksums: | |
| permissions: | |
| contents: write | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout repository | |
| uses: actions/checkout@v4 | |
| - name: 1. List files BEFORE script runs | |
| run: | | |
| echo "--- Listing contents of 'files' directory ---" | |
| ls -R files || echo "Directory 'files' is empty or does not exist." | |
| echo "---------------------------------------------" | |
| - name: 2. Generate and Remove Stale Checksums | |
| run: | | |
| TARGET_DIR="files" | |
| cat << 'EOF' > manage_checksums.py | |
| import os | |
| import hashlib | |
| import sys | |
| def manage_checksums(root_dir): | |
| source_files = set() | |
| for dirpath, _, filenames in os.walk(root_dir): | |
| if ".git" in dirpath: | |
| continue | |
| for filename in filenames: | |
| if not filename.endswith('.sha256'): | |
| source_files.add(os.path.join(dirpath, filename)) | |
| existing_checksum_files = set() | |
| for dirpath, _, filenames in os.walk(root_dir): | |
| if ".git" in dirpath: | |
| continue | |
| for filename in filenames: | |
| if filename.endswith('.sha256'): | |
| existing_checksum_files.add(os.path.join(dirpath, filename)) | |
| # Generate or update checksums for all source files | |
| for file_path in source_files: | |
| 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) | |
| checksum_file_path = f"{file_path}.sha256" | |
| base_filename = os.path.basename(file_path) | |
| with open(checksum_file_path, "w", encoding="utf-8") as f: | |
| f.write(f"{sha256_hash.hexdigest()} *{base_filename}\n") | |
| print(f"Generated/Updated checksum for {file_path}") | |
| # Find and remove stale checksum files | |
| required_checksum_files = {f"{f}.sha256" for f in source_files} | |
| stale_files = existing_checksum_files - required_checksum_files | |
| for stale_file in stale_files: | |
| if os.path.exists(stale_file): | |
| os.remove(stale_file) | |
| print(f"Removed stale checksum file: {stale_file}") | |
| if __name__ == "__main__": | |
| if len(sys.argv) != 2: | |
| print("Usage: python manage_checksums.py <target_directory>") | |
| sys.exit(1) | |
| target_directory = sys.argv[1] | |
| if os.path.isdir(target_directory): | |
| manage_checksums(target_directory) | |
| else: | |
| print(f"Error: Directory '{target_directory}' not found.") | |
| sys.exit(1) | |
| EOF | |
| python manage_checksums.py $TARGET_DIR | |
| - name: 3. List files AFTER script runs | |
| run: | | |
| echo "--- Listing contents of 'files' directory after script ---" | |
| ls -R files || echo "Directory 'files' is empty or does not exist." | |
| echo "--------------------------------------------------------" | |
| - name: 4. Check Git status for changes | |
| run: | | |
| echo "--- Checking for uncommitted changes ---" | |
| git status --porcelain | |
| echo "----------------------------------------" | |
| - name: 5. Commit and push if changed | |
| uses: stefanzweifel/git-auto-commit-action@v5 | |
| with: | |
| commit_message: "ci: Update individual file checksums" | |
| file_pattern: "files/**/*.sha256" | |
| commit_user_name: "GitHub Actions Bot" | |
| commit_user_email: "github-actions[bot]@users.noreply.github.com" |