first commit

This commit is contained in:
alan890104
2023-09-13 16:35:30 +08:00
commit 138cc3015f
5 changed files with 150 additions and 0 deletions

7
Dockerfile Normal file
View File

@@ -0,0 +1,7 @@
FROM python:3.10-slim
WORKDIR /app
COPY action.py /app
CMD ["python", "./action.py"]

21
LICENSE.md Normal file
View File

@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2023 Hsu, Yu Lun
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

69
README.md Normal file
View File

@@ -0,0 +1,69 @@
# Checksum Action
## Description
`checksum-action` is a GitHub Action that generates SHA256 checksums for multiple files in your GitHub repository. It allows you to specify a pattern for matching files and optionally specify a suffix for the generated checksum files.
## Inputs
### `pattern`
**Required** The pattern used to search for files (glob format). For example:
```yaml
pattern: "*.txt"
```
This would match all `.txt` files in the repository.
### `suffix`
**Optional** The suffix to append to the generated checksum files. Default is `checksum`.
For example, given a file `example.txt`, if the suffix is set to `checksum`, the generated checksum file will be `example.txt.checksum`.
## Example Usage
Here's a basic example that demonstrates how to use `checksum-action` in a workflow:
```yaml
name: Generate Checksums
on:
push:
branches:
- main
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Generate checksums
uses: Solratic/checksum-action@v1
with:
pattern: "<your folder>/*.txt"
suffix: "checksum"
```
This workflow will be triggered on a push to the `main` branch. It will:
1. Checkout the repository
2. Generate SHA256 checksums for all `.txt` files in the repository
3. Append `.checksum` to the generated checksum files.
4. You would see `<your folder>/<file>.txt.checksum` in your repository.
## Docker Container
This action runs on a Docker container, specified in the `Dockerfile` in this repository.
## Contributing
Feel free to open issues or pull requests if you want to improve this GitHub action.
## License
Please see the [LICENSE](LICENSE.md) file for details on how the code in this repo is licensed.

36
action.py Normal file
View File

@@ -0,0 +1,36 @@
import hashlib
import os
import glob
import argparse
def compute_checksum(file_path: str):
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)
return sha256_hash.hexdigest()
def main(pattern: str, suffix: str):
for filename in glob.glob(pattern):
parent, base = os.path.split(filename)
print(parent, base)
checksum = compute_checksum(filename)
with open(os.path.join(parent, f"{base}.{suffix}"), "w") as f:
f.write(checksum)
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Generate checksums for multiple files"
)
parser.add_argument(
"--pattern", required=True, help="Pattern to search for files (glob)"
)
parser.add_argument(
"--suffix", default="checksum", help="Suffix for the checksum files"
)
args = parser.parse_args()
main(pattern=args.pattern, suffix=args.suffix)

17
action.yaml Normal file
View File

@@ -0,0 +1,17 @@
name: 'checksum-action'
description: 'Generate checksums (SHA256) for multiple files'
inputs:
pattern:
description: 'Pattern to search for files (glob)'
required: true
suffix:
description: 'Suffix for the checksum files'
required: false
default: "checksum"
runs:
using: 'docker'
image: 'Dockerfile'
args:
- ${{ inputs.pattern }}
- ${{ inputs.suffix }}