|
| 1 | +name: Generate Individual File Checksums |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - main |
| 7 | + paths: |
| 8 | + - 'files/**' |
| 9 | + - '!.github/**' |
| 10 | + |
| 11 | + workflow_dispatch: |
| 12 | + |
| 13 | +jobs: |
| 14 | + generate-checksums: |
| 15 | + permissions: |
| 16 | + contents: write |
| 17 | + |
| 18 | + runs-on: ubuntu-latest |
| 19 | + |
| 20 | + steps: |
| 21 | + - name: Checkout repository |
| 22 | + uses: actions/checkout@v4 |
| 23 | + |
| 24 | + - name: Generate Individual Checksums |
| 25 | + id: generate_checksums |
| 26 | + run: | |
| 27 | + TARGET_DIR="files" |
| 28 | + |
| 29 | + cat << 'EOF' > generate_checksums.py |
| 30 | + import os |
| 31 | + import hashlib |
| 32 | +
|
| 33 | + def generate_sha256(file_path): |
| 34 | + sha256_hash = hashlib.sha256() |
| 35 | + with open(file_path, "rb") as f: |
| 36 | + for byte_block in iter(lambda: f.read(4096), b""): |
| 37 | + sha256_hash.update(byte_block) |
| 38 | + return sha256_hash.hexdigest() |
| 39 | +
|
| 40 | + def create_individual_checksums(root_dir): |
| 41 | + for dirpath, _, filenames in os.walk(root_dir): |
| 42 | + for filename in filenames: |
| 43 | + if filename.endswith('.sha256') or '.git' in dirpath: |
| 44 | + continue |
| 45 | + |
| 46 | + file_path = os.path.join(dirpath, filename) |
| 47 | + digest = generate_sha256(file_path) |
| 48 | +
|
| 49 | + checksum_file = file_path + '.sha256' |
| 50 | + with open(checksum_file, "w", encoding="utf-8") as f: |
| 51 | + f.write(f"{digest} *{filename}\n") |
| 52 | +
|
| 53 | + print(f"Created: {checksum_file}") |
| 54 | +
|
| 55 | + if __name__ == "__main__": |
| 56 | + create_individual_checksums("files") |
| 57 | + EOF |
| 58 | +
|
| 59 | + python generate_checksums.py |
| 60 | +
|
| 61 | + - name: Commit and push if changed |
| 62 | + uses: stefanzweifel/git-auto-commit-action@v5 |
| 63 | + with: |
| 64 | + commit_message: "ci: Update individual file checksums" |
| 65 | + file_pattern: files/**/*.sha256 |
| 66 | + commit_user_name: "GitHub Actions Bot" |
| 67 | + commit_user_email: "github-actions[bot]@users.noreply.github.com" |
0 commit comments