Generate Checksums and Upload as Artifact #6
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/**' | |
| - '!**/*.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 generating checksums | |
| run: | | |
| echo "--- Listing files in 'files' directory before script execution ---" | |
| ls -R files || echo "Directory 'files' not found or is empty." | |
| - name: 2. Generate and Save Individual Checksums | |
| id: generate_checksums | |
| run: | | |
| TARGET_DIR="files" | |
| cat << 'EOF' > generate_checksums.py | |
| import os | |
| import hashlib | |
| import sys | |
| def create_individual_checksums(root_dir): | |
| processed_files_count = 0 | |
| for dirpath, _, filenames in os.walk(root_dir): | |
| for filename in filenames: | |
| if filename.endswith('.sha256') or ".git" in dirpath: | |
| continue | |
| file_path = os.path.join(dirpath, filename) | |
| output_filepath = f"{file_path}.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() | |
| with open(output_filepath, "w", encoding="utf-8") as f_out: | |
| f_out.write(f"{digest} *{os.path.basename(file_path)}\n") | |
| print(f"--> Generated: {output_filepath}") | |
| processed_files_count += 1 | |
| if processed_files_count == 0: | |
| print("No files found to process.") | |
| else: | |
| print(f"\nSuccessfully processed {processed_files_count} file(s).") | |
| if __name__ == "__main__": | |
| if len(sys.argv) > 1: | |
| target_directory = sys.argv[1] | |
| if os.path.isdir(target_directory): | |
| create_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_checksums.py $TARGET_DIR | |
| - name: 3. List files AFTER generating checksums | |
| run: | | |
| echo "--- Listing files in 'files' directory after script execution ---" | |
| ls -R files || echo "Directory 'files' not found." | |
| - name: 4. Check for changes to commit | |
| id: check_changes | |
| run: | | |
| echo "--- Checking for files matching 'files/**/*.sha256' ---" | |
| git status --porcelain | |
| MODIFIED_FILES=$(git status --porcelain | grep -o "files/.*\.sha256" || echo "") | |
| if [ -n "$MODIFIED_FILES" ]; then | |
| echo "Found changes to commit:" | |
| echo "$MODIFIED_FILES" | |
| else | |
| echo "No new or modified .sha256 files to commit." | |
| fi | |
| - name: 5. Commit and push if changed | |
| uses: stefanzweifel/git-auto-commit-action@v5 | |
| with: | |
| commit_message: "ci: Update file checksums" | |
| file_pattern: "files/**/*.sha256" | |
| commit_user_name: "GitHub Actions Bot" | |
| commit_user_email: "github-actions[bot]@users.noreply.github.com" |