Skip to content

Commit 11e20f8

Browse files
committed
refactor(ci): improve badge action with explicit filename configuration
- Replace brittle string manipulation with explicit matrix filename parameters - Add filename input parameter to update-test-badge action - Update matrix configuration to declare explicit filenames: * test-results-linux.json * test-results-windows.json * test-results-macos.json - Consolidate to single Gist (472c59b7c2a1898c48a29f3c88897c5a) with multiple files - Remove platform string transformation logic from action - Update documentation with explicit filename advantages - Improve action reusability and predictability Resolves brittle filename generation and improves maintainability
1 parent 431ba00 commit 11e20f8

File tree

3 files changed

+84
-40
lines changed

3 files changed

+84
-40
lines changed

.github/actions/update-test-badge/README.md

Lines changed: 53 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
# Update Test Results Badge Action
22

3-
A reusable GitHub Action that updates test result badges by uploading test data to GitHub Gists and displaying badge URLs for README files.
3+
A reusable GitHub Action that updates test result badges by uploading test data to GitHub Gist files and displaying badge URLs for README files.
44

55
## Purpose
66

77
This action simplifies the process of maintaining dynamic test result badges by:
8-
98
- Creating structured JSON data from test results
10-
- Uploading the data to platform-specific GitHub Gists
9+
- Uploading the data to platform-specific files in a single GitHub Gist
1110
- Providing ready-to-use badge URLs for documentation
1211

1312
## Usage
@@ -17,7 +16,8 @@ This action simplifies the process of maintaining dynamic test result badges by:
1716
uses: ./.github/actions/update-test-badge
1817
with:
1918
platform: "Linux"
20-
gist_id: "c149767013f99f00791256d9036ef71b"
19+
gist_id: "472c59b7c2a1898c48a29f3c88897c5a"
20+
filename: "test-results-linux.json"
2121
gist_token: ${{ secrets.GIST_SECRET }}
2222
test_passed: 1099
2323
test_failed: 0
@@ -29,12 +29,24 @@ This action simplifies the process of maintaining dynamic test result badges by:
2929
server_url: ${{ github.server_url }}
3030
```
3131
32+
## Gist Structure
33+
34+
This action uses a **single Gist** with **multiple files** for different platforms:
35+
36+
```
37+
Gist ID: 472c59b7c2a1898c48a29f3c88897c5a
38+
├── test-results-linux.json
39+
├── test-results-windows.json
40+
└── test-results-macos.json
41+
```
42+
3243
## Inputs
3344

3445
| Input | Description | Required | Default |
3546
|-------|-------------|----------|---------|
3647
| `platform` | Platform name (Linux, Windows, macOS) || - |
3748
| `gist_id` | GitHub Gist ID for storing test results || - |
49+
| `filename` | Filename for platform-specific JSON (e.g., test-results-linux.json) || - |
3850
| `gist_token` | GitHub token with gist permissions || - |
3951
| `test_passed` | Number of passed tests || - |
4052
| `test_failed` | Number of failed tests || - |
@@ -49,14 +61,13 @@ This action simplifies the process of maintaining dynamic test result badges by:
4961
## Outputs
5062

5163
This action produces:
52-
53-
- **Gist Update**: Updates the specified Gist with test result JSON
64+
- **Gist File Update**: Updates the platform-specific file in the single Gist
5465
- **Console Output**: Displays badge URLs ready for README usage
5566
- **Debug Info**: Shows HTTP status and error details
5667

5768
## Generated JSON Format
5869

59-
The action creates JSON data in this format:
70+
The action creates JSON data in this format for each platform file:
6071

6172
```json
6273
{
@@ -78,46 +89,53 @@ The action creates JSON data in this format:
7889
- **Non-essential**: Uses `continue-on-error: true` to prevent workflow failures
7990
- **Graceful degradation**: Provides detailed error messages without stopping execution
8091
- **HTTP status reporting**: Shows API response codes for debugging
92+
- **File-specific updates**: Only updates the specific platform file, doesn't affect other platform data
8193

8294
## Integration with Badge API
8395

8496
This action is designed to work with the LocalStack .NET Client Badge API that:
85-
86-
- Reads from the updated Gists
97+
- Reads from the updated Gist files
8798
- Generates shields.io-compatible badge JSON
8899
- Provides redirect endpoints to test result pages
89100

90101
## Matrix Integration Example
91102

92103
```yaml
104+
env:
105+
BADGE_GIST_ID: "472c59b7c2a1898c48a29f3c88897c5a"
106+
93107
strategy:
94108
matrix:
95109
include:
96110
- os: ubuntu-22.04
97111
name: "Linux"
98-
gist_id: "c149767013f99f00791256d9036ef71b"
112+
filename: "test-results-linux.json"
99113
- os: windows-latest
100114
name: "Windows"
101-
gist_id: "3640d86bbf37520844f737e6a76b4d90"
115+
filename: "test-results-windows.json"
102116
- os: macos-latest
103117
name: "macOS"
104-
gist_id: "db58d93cf17ee5db079d06e3bfa4c069"
118+
filename: "test-results-macos.json"
105119

106120
steps:
107121
- name: "Update Test Results Badge"
108122
uses: ./.github/actions/update-test-badge
109123
with:
110124
platform: ${{ matrix.name }}
111-
gist_id: ${{ matrix.gist_id }}
125+
gist_id: ${{ env.BADGE_GIST_ID }}
126+
filename: ${{ matrix.filename }}
112127
gist_token: ${{ secrets.GIST_SECRET }}
113128
# ... other inputs
114129
```
115130

116131
## Required Setup
117132

118-
1. **Create GitHub Gists** for each platform
133+
1. **Create single GitHub Gist** with platform-specific files:
134+
- `test-results-linux.json`
135+
- `test-results-windows.json`
136+
- `test-results-macos.json`
119137
2. **Generate GitHub PAT** with `gist` scope
120-
3. **Add to repository secrets** as `GIST_TOKEN`
138+
3. **Add to repository secrets** as `GIST_SECRET`
121139
4. **Deploy Badge API** to consume the Gist data
122140

123141
## Badge URLs Generated
@@ -128,17 +146,33 @@ The action displays ready-to-use markdown for README files:
128146
[![Test Results (Linux)](https://your-api-domain/badge/tests/linux)](https://your-api-domain/redirect/tests/linux)
129147
```
130148

149+
## Advantages of Explicit Filename Configuration
150+
151+
-**No String Manipulation**: Eliminates brittle string transformation logic
152+
-**Declarative**: Filenames are explicitly declared in workflow configuration
153+
-**Predictable**: No risk of unexpected filename generation
154+
-**Reusable**: Action works with any filename structure
155+
-**Debuggable**: Easy to see exactly what files will be created
156+
-**Flexible**: Supports any naming convention without code changes
157+
158+
## Advantages of Single Gist Approach
159+
160+
-**Simplified Management**: One Gist to manage instead of three
161+
-**Atomic Operations**: All platform data in one place
162+
-**Better Organization**: Clear file structure with descriptive names
163+
-**Easier Debugging**: Single location to check all test data
164+
-**Cost Efficient**: Fewer API calls and resources
165+
131166
## Troubleshooting
132167

133168
**Common Issues:**
134-
135-
- **403 Forbidden**: Check `GIST_TOKEN` permissions
169+
- **403 Forbidden**: Check `GIST_SECRET` permissions
136170
- **404 Not Found**: Verify `gist_id` is correct
137171
- **JSON Errors**: Ensure `jq` is available in runner
172+
- **File Missing**: Gist files are created automatically on first update
138173

139174
**Debug Steps:**
140-
141175
1. Check action output for HTTP status codes
142176
2. Verify Gist exists and is publicly accessible
143177
3. Confirm token has proper `gist` scope
144-
4. Test Gist update manually with curl
178+
4. Check individual file URLs: `https://gist.githubusercontent.com/{gist_id}/raw/test-results-{platform}.json`

.github/actions/update-test-badge/action.yml

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ inputs:
99
gist_id:
1010
description: 'GitHub Gist ID for storing test results'
1111
required: true
12+
filename:
13+
description: 'Filename for the platform-specific JSON file (e.g., test-results-linux.json)'
14+
required: true
1215
gist_token:
1316
description: 'GitHub token with gist permissions'
1417
required: true
@@ -48,6 +51,9 @@ runs:
4851
- name: 'Update Test Results Badge Data'
4952
shell: bash
5053
run: |
54+
# Use explicit filename from input
55+
FILENAME="${{ inputs.filename }}"
56+
5157
# Calculate totals
5258
TOTAL=$((${{ inputs.test_passed }} + ${{ inputs.test_failed }} + ${{ inputs.test_skipped }}))
5359
@@ -70,14 +76,14 @@ runs:
7076
echo "📊 Generated test results JSON for ${{ inputs.platform }}:"
7177
cat test-results.json | jq '.' 2>/dev/null || cat test-results.json
7278
73-
# Upload to platform-specific Gist
74-
echo "📤 Uploading to Gist: ${{ inputs.gist_id }}"
79+
# Upload to single Gist with platform-specific filename
80+
echo "📤 Uploading to Gist: ${{ inputs.gist_id }} (file: ${FILENAME})"
7581
76-
# Create gist update payload
82+
# Create gist update payload - only update the specific platform file
7783
cat > gist-payload.json << EOF
7884
{
7985
"files": {
80-
"test-results.json": {
86+
"${FILENAME}": {
8187
"content": $(cat test-results.json | jq -R -s '.')
8288
}
8389
}
@@ -94,9 +100,9 @@ runs:
94100
-o response.json)
95101
96102
if [ "$HTTP_STATUS" -eq 200 ]; then
97-
echo "✅ Successfully updated Gist (HTTP $HTTP_STATUS)"
103+
echo "✅ Successfully updated Gist file ${FILENAME} (HTTP $HTTP_STATUS)"
98104
else
99-
echo "⚠️ Failed to update Gist (HTTP $HTTP_STATUS)"
105+
echo "⚠️ Failed to update Gist file ${FILENAME} (HTTP $HTTP_STATUS)"
100106
echo "Response:"
101107
cat response.json 2>/dev/null || echo "No response body"
102108
fi
@@ -105,6 +111,7 @@ runs:
105111
shell: bash
106112
run: |
107113
PLATFORM_LOWER=$(echo "${{ inputs.platform }}" | tr '[:upper:]' '[:lower:]')
114+
FILENAME="${{ inputs.filename }}"
108115
109116
echo "🎯 Badge URL for ${{ inputs.platform }}:"
110117
echo ""
@@ -114,4 +121,5 @@ runs:
114121
echo "**Raw URLs:**"
115122
echo "- Badge: https://${{ inputs.api_domain }}/badge/tests/${PLATFORM_LOWER}"
116123
echo "- Redirect: https://${{ inputs.api_domain }}/redirect/tests/${PLATFORM_LOWER}"
117-
echo "- Gist: https://gist.github.com/${{ inputs.gist_id }}"
124+
echo "- Gist: https://gist.github.com/${{ inputs.gist_id }}"
125+
echo "- Gist File: https://gist.githubusercontent.com/${{ inputs.gist_id }}/raw/${FILENAME}"

.github/workflows/ci-cd.yml

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ jobs:
2727
runs-on: ${{ matrix.os }}
2828
env:
2929
NUGET_PACKAGES: ${{ contains(matrix.os, 'windows') && format('{0}\.nuget\packages', github.workspace) || format('{0}/.nuget/packages', github.workspace) }}
30+
BADGE_GIST_ID: "472c59b7c2a1898c48a29f3c88897c5a"
3031

3132
strategy:
3233
fail-fast: false
@@ -35,23 +36,23 @@ jobs:
3536
- os: windows-latest
3637
name: "Windows"
3738
script: "./build.ps1"
38-
gist_id: "3640d86bbf37520844f737e6a76b4d90"
39+
filename: "test-results-windows.json"
3940

4041
- os: ubuntu-22.04
4142
name: "Linux"
4243
script: "./build.sh"
43-
gist_id: "c149767013f99f00791256d9036ef71b"
44+
filename: "test-results-linux.json"
4445

4546
- os: macos-latest
4647
name: "macOS"
4748
script: "./build.sh"
48-
gist_id: "db58d93cf17ee5db079d06e3bfa4c069"
49+
filename: "test-results-macos.json"
4950

5051
steps:
5152
- name: "Checkout"
5253
uses: actions/checkout@v4
5354
with:
54-
fetch-depth: 0 # Full history for better caching
55+
fetch-depth: 0 # Full history for better caching
5556

5657
- name: "Setup .NET SDK"
5758
uses: actions/setup-dotnet@v4
@@ -83,19 +84,20 @@ jobs:
8384
uses: dorny/test-reporter@v1
8485
if: success() || failure()
8586
with:
86-
name: 'Test Results (${{ matrix.name }})'
87-
path: '**/TestResults/*.trx'
88-
reporter: 'dotnet-trx'
87+
name: "Test Results (${{ matrix.name }})"
88+
path: "**/TestResults/*.trx"
89+
reporter: "dotnet-trx"
8990
fail-on-error: true
9091
max-annotations: 50
9192

9293
- name: "Update Test Results Badge"
93-
if: always() # Run even if tests failed or were skipped
94-
continue-on-error: true # Don't fail workflow if badge update fails
94+
if: always() # Run even if tests failed or were skipped
95+
continue-on-error: true # Don't fail workflow if badge update fails
9596
uses: ./.github/actions/update-test-badge
9697
with:
9798
platform: ${{ matrix.name }}
98-
gist_id: ${{ matrix.gist_id }}
99+
gist_id: ${{ env.BADGE_GIST_ID }}
100+
filename: ${{ matrix.filename }}
99101
gist_token: ${{ secrets.GIST_SECRET }}
100102
test_passed: ${{ steps.test-results.outputs.passed || 0 }}
101103
test_failed: ${{ steps.test-results.outputs.failed || 0 }}
@@ -156,20 +158,20 @@ jobs:
156158
run: chmod +x ./build.sh
157159

158160
- name: "Setup GitHub Packages Configuration"
159-
run: |
161+
run: |
160162
echo "🔐 Adding GitHub Packages authentication..."
161163
dotnet nuget add source https://nuget.pkg.github.com/${{ github.repository_owner }}/index.json \
162164
--name github-packages \
163165
--username ${{ github.actor }} \
164166
--password ${{ secrets.GITHUB_TOKEN }} \
165167
--store-password-in-clear-text
166-
168+
167169
echo "🔧 Original nuget.config..."
168170
cat nuget.config
169-
171+
170172
echo "📝 Backing up original nuget.config..."
171173
cp nuget.config nuget.config.backup
172-
174+
173175
echo "🔧 Using GitHub-optimized nuget.config..."
174176
cp .github/nuget.config nuget.config
175177

0 commit comments

Comments
 (0)