feat: 初始化文件哈希 GitHub Action 项目
This commit is contained in:
46
.github/workflows/ci.yml
vendored
Normal file
46
.github/workflows/ci.yml
vendored
Normal file
@@ -0,0 +1,46 @@
|
||||
name: CI
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [main]
|
||||
pull_request:
|
||||
branches: [main]
|
||||
|
||||
jobs:
|
||||
lint-and-test:
|
||||
runs-on: ubuntu-latest
|
||||
name: Lint and Test
|
||||
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Setup Node.js
|
||||
uses: actions/setup-node@v4
|
||||
with:
|
||||
node-version: '20'
|
||||
|
||||
- name: Setup pnpm
|
||||
uses: pnpm/action-setup@v4
|
||||
with:
|
||||
version: 9
|
||||
|
||||
- name: Install dependencies
|
||||
run: pnpm install --frozen-lockfile
|
||||
|
||||
- name: Check JavaScript Syntax
|
||||
run: |
|
||||
echo "🔍 Checking JavaScript syntax..."
|
||||
node -c index.js
|
||||
echo "✅ All JavaScript files have valid syntax"
|
||||
|
||||
- name: Run Tests
|
||||
run: |
|
||||
echo "🧪 Running test suite..."
|
||||
pnpm test
|
||||
|
||||
- name: Test Action Execution
|
||||
uses: ./
|
||||
with:
|
||||
files: 'examples/*'
|
||||
algorithm: 'sha256'
|
||||
157
.github/workflows/examples.yml
vendored
Normal file
157
.github/workflows/examples.yml
vendored
Normal file
@@ -0,0 +1,157 @@
|
||||
name: Usage Examples
|
||||
|
||||
on:
|
||||
workflow_dispatch:
|
||||
schedule:
|
||||
- cron: '0 0 * * 0' # Weekly on Sunday
|
||||
|
||||
jobs:
|
||||
example-basic:
|
||||
runs-on: ubuntu-latest
|
||||
name: Basic Usage Example
|
||||
|
||||
steps:
|
||||
- name: Setup pnpm
|
||||
uses: pnpm/action-setup@v4
|
||||
with:
|
||||
version: 9
|
||||
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Calculate hash of a single file
|
||||
id: single-file
|
||||
uses: ./
|
||||
with:
|
||||
files: 'README.md'
|
||||
algorithm: 'sha256'
|
||||
|
||||
- name: Display result
|
||||
run: |
|
||||
echo "📄 Single file hash:"
|
||||
echo "Hash: ${{ steps.single-file.outputs.hash }}"
|
||||
echo "Files processed: ${{ steps.single-file.outputs.file-count }}"
|
||||
|
||||
example-multiple:
|
||||
runs-on: ubuntu-latest
|
||||
name: Multiple Files Example
|
||||
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Calculate hash of multiple files
|
||||
id: multiple-files
|
||||
uses: ./
|
||||
with:
|
||||
files: 'examples/*'
|
||||
algorithm: 'md5'
|
||||
|
||||
- name: Display result
|
||||
run: |
|
||||
echo "📁 Multiple files hash:"
|
||||
echo "Hash: ${{ steps.multiple-files.outputs.hash }}"
|
||||
echo "Files processed: ${{ steps.multiple-files.outputs.file-count }}"
|
||||
|
||||
example-patterns:
|
||||
runs-on: ubuntu-latest
|
||||
name: Pattern Matching Example
|
||||
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Calculate hash of JavaScript files
|
||||
id: js-files
|
||||
uses: ./
|
||||
with:
|
||||
files: '**/*.js'
|
||||
algorithm: 'sha1'
|
||||
|
||||
- name: Display result
|
||||
run: |
|
||||
echo "🔧 JavaScript files hash:"
|
||||
echo "Hash: ${{ steps.js-files.outputs.hash }}"
|
||||
echo "Files processed: ${{ steps.js-files.outputs.file-count }}"
|
||||
|
||||
- name: Calculate hash of JSON files
|
||||
id: json-files
|
||||
uses: ./
|
||||
with:
|
||||
files: '**/*.json'
|
||||
algorithm: 'sha256'
|
||||
|
||||
- name: Display result
|
||||
run: |
|
||||
echo "📋 JSON files hash:"
|
||||
echo "Hash: ${{ steps.json-files.outputs.hash }}"
|
||||
echo "Files processed: ${{ steps.json-files.outputs.file-count }}"
|
||||
|
||||
example-algorithms:
|
||||
runs-on: ubuntu-latest
|
||||
name: Different Algorithms Example
|
||||
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: MD5 Hash
|
||||
id: md5
|
||||
uses: ./
|
||||
with:
|
||||
files: 'action.yml'
|
||||
algorithm: 'md5'
|
||||
|
||||
- name: SHA1 Hash
|
||||
id: sha1
|
||||
uses: ./
|
||||
with:
|
||||
files: 'action.yml'
|
||||
algorithm: 'sha1'
|
||||
|
||||
- name: SHA256 Hash
|
||||
id: sha256
|
||||
uses: ./
|
||||
with:
|
||||
files: 'action.yml'
|
||||
algorithm: 'sha256'
|
||||
|
||||
- name: SHA512 Hash
|
||||
id: sha512
|
||||
uses: ./
|
||||
with:
|
||||
files: 'action.yml'
|
||||
algorithm: 'sha512'
|
||||
|
||||
- name: Display all results
|
||||
run: |
|
||||
echo "🔐 Different algorithm results for action.yml:"
|
||||
echo "MD5: ${{ steps.md5.outputs.hash }}"
|
||||
echo "SHA1: ${{ steps.sha1.outputs.hash }}"
|
||||
echo "SHA256: ${{ steps.sha256.outputs.hash }}"
|
||||
echo "SHA512: ${{ steps.sha512.outputs.hash }}"
|
||||
|
||||
example-error-handling:
|
||||
runs-on: ubuntu-latest
|
||||
name: Error Handling Example
|
||||
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Handle missing files (expected to fail)
|
||||
id: graceful
|
||||
continue-on-error: true
|
||||
uses: ./
|
||||
with:
|
||||
files: 'missing-*.txt'
|
||||
algorithm: 'sha256'
|
||||
|
||||
- name: Check graceful result
|
||||
run: |
|
||||
if [ "${{ steps.graceful.outcome }}" == "failure" ]; then
|
||||
echo "✅ Correctly failed when files are missing"
|
||||
else
|
||||
echo "❌ Should have failed but didn't"
|
||||
exit 1
|
||||
fi
|
||||
148
.github/workflows/test.yml
vendored
Normal file
148
.github/workflows/test.yml
vendored
Normal file
@@ -0,0 +1,148 @@
|
||||
name: Test Files Hash Action
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [main, develop]
|
||||
pull_request:
|
||||
branches: [main]
|
||||
workflow_dispatch:
|
||||
|
||||
jobs:
|
||||
test:
|
||||
runs-on: ubuntu-latest
|
||||
name: Test Action
|
||||
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Setup Node.js
|
||||
uses: actions/setup-node@v4
|
||||
with:
|
||||
node-version: '20'
|
||||
|
||||
- name: Run Unit Tests
|
||||
run: node run-tests.js
|
||||
|
||||
- name: Test Action - Single File
|
||||
id: test-single
|
||||
uses: ./
|
||||
with:
|
||||
files: 'examples/sample1.txt'
|
||||
algorithm: 'sha256'
|
||||
|
||||
- name: Verify Single File Output
|
||||
run: |
|
||||
echo "Hash: ${{ steps.test-single.outputs.hash }}"
|
||||
echo "File Count: ${{ steps.test-single.outputs.file-count }}"
|
||||
if [ "${{ steps.test-single.outputs.file-count }}" != "1" ]; then
|
||||
echo "❌ Expected file count 1, got ${{ steps.test-single.outputs.file-count }}"
|
||||
exit 1
|
||||
fi
|
||||
if [ -z "${{ steps.test-single.outputs.hash }}" ]; then
|
||||
echo "❌ Hash output is empty"
|
||||
exit 1
|
||||
fi
|
||||
echo "✅ Single file test passed"
|
||||
|
||||
- name: Test Action - Multiple Files
|
||||
id: test-multiple
|
||||
uses: ./
|
||||
with:
|
||||
files: 'examples/*.txt'
|
||||
algorithm: 'md5'
|
||||
|
||||
- name: Verify Multiple Files Output
|
||||
run: |
|
||||
echo "Hash: ${{ steps.test-multiple.outputs.hash }}"
|
||||
echo "File Count: ${{ steps.test-multiple.outputs.file-count }}"
|
||||
if [ "${{ steps.test-multiple.outputs.file-count }}" != "1" ]; then
|
||||
echo "❌ Expected file count 1, got ${{ steps.test-multiple.outputs.file-count }}"
|
||||
exit 1
|
||||
fi
|
||||
echo "✅ Multiple files test passed"
|
||||
|
||||
- name: Test Action - All Examples
|
||||
id: test-all
|
||||
uses: ./
|
||||
with:
|
||||
files: 'examples/*'
|
||||
algorithm: 'sha1'
|
||||
|
||||
- name: Verify All Files Output
|
||||
run: |
|
||||
echo "Hash: ${{ steps.test-all.outputs.hash }}"
|
||||
echo "File Count: ${{ steps.test-all.outputs.file-count }}"
|
||||
if [ "${{ steps.test-all.outputs.file-count }}" != "3" ]; then
|
||||
echo "❌ Expected file count 3, got ${{ steps.test-all.outputs.file-count }}"
|
||||
exit 1
|
||||
fi
|
||||
echo "✅ All files test passed"
|
||||
|
||||
- name: Test Action - Different Algorithm
|
||||
id: test-sha512
|
||||
uses: ./
|
||||
with:
|
||||
files: 'README.md'
|
||||
algorithm: 'sha512'
|
||||
|
||||
- name: Verify SHA512 Output
|
||||
run: |
|
||||
echo "Hash: ${{ steps.test-sha512.outputs.hash }}"
|
||||
echo "File Count: ${{ steps.test-sha512.outputs.file-count }}"
|
||||
if [ "${{ steps.test-sha512.outputs.file-count }}" != "1" ]; then
|
||||
echo "❌ Expected file count 1, got ${{ steps.test-sha512.outputs.file-count }}"
|
||||
exit 1
|
||||
fi
|
||||
# SHA512 hash should be 128 characters long
|
||||
hash_length=$(echo "${{ steps.test-sha512.outputs.hash }}" | wc -c)
|
||||
if [ $hash_length -ne 129 ]; then # 128 + 1 for newline
|
||||
echo "❌ SHA512 hash length should be 128, got $((hash_length-1))"
|
||||
exit 1
|
||||
fi
|
||||
echo "✅ SHA512 test passed"
|
||||
|
||||
- name: Test Action - Missing Files (should fail)
|
||||
id: test-missing
|
||||
continue-on-error: true
|
||||
uses: ./
|
||||
with:
|
||||
files: 'non-existent-file.txt'
|
||||
algorithm: 'sha256'
|
||||
|
||||
- name: Verify Missing Files Handling
|
||||
run: |
|
||||
if [ "${{ steps.test-missing.outcome }}" == "success" ]; then
|
||||
echo "❌ Expected action to fail on missing file"
|
||||
exit 1
|
||||
fi
|
||||
echo "✅ Missing files test passed"
|
||||
|
||||
test-matrix:
|
||||
runs-on: ${{ matrix.os }}
|
||||
strategy:
|
||||
matrix:
|
||||
os: [ubuntu-latest, windows-latest, macos-latest]
|
||||
node-version: ['18', '20']
|
||||
name: Test on ${{ matrix.os }} with Node ${{ matrix.node-version }}
|
||||
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Setup pnpm
|
||||
uses: pnpm/action-setup@v4
|
||||
with:
|
||||
version: 9
|
||||
|
||||
- name: Setup Node.js ${{ matrix.node-version }}
|
||||
uses: actions/setup-node@v4
|
||||
with:
|
||||
node-version: ${{ matrix.node-version }}
|
||||
cache: 'pnpm'
|
||||
|
||||
- name: Test Action
|
||||
uses: ./
|
||||
with:
|
||||
files: 'examples/sample1.txt'
|
||||
algorithm: 'sha256'
|
||||
Reference in New Issue
Block a user