Skip to content

Commit ba00826

Browse files
authored
ci(check-variables): print a warning if a GITHUB_* variable from this action already exists (#155)
1 parent 7976662 commit ba00826

File tree

1 file changed

+139
-0
lines changed

1 file changed

+139
-0
lines changed

.github/workflows/check-variables.yml

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
---
2+
name: Compare GitHub Variables
3+
4+
on:
5+
workflow_dispatch: # Manual trigger
6+
schedule:
7+
- cron: "0 0 * * 1" # Weekly on Monday at midnight
8+
pull_request:
9+
10+
permissions: read-all
11+
12+
jobs:
13+
compare-variables:
14+
runs-on: ubuntu-latest
15+
permissions:
16+
issues: write
17+
18+
steps:
19+
- name: Checkout repository
20+
uses: actions/checkout@v4
21+
22+
- name: Load environment variables for testing
23+
uses: rlespinasse/github-slug-action@v5
24+
with:
25+
prefix: TEST_
26+
27+
- name: Compare GitHub variables
28+
id: compare
29+
run: |
30+
# Initialize arrays to store results
31+
MISMATCHED_VARS=()
32+
TEST_VALUES=()
33+
ACTUAL_VALUES=()
34+
35+
# Get all environment variables
36+
ALL_ENV=$(env)
37+
38+
# Extract TEST_GITHUB variables
39+
TEST_VARS=$(echo "$ALL_ENV" | grep "^TEST_GITHUB_" | cut -d= -f1)
40+
41+
# Check for mismatches
42+
for TEST_VAR in $TEST_VARS; do
43+
# Get the corresponding GITHUB variable name
44+
GITHUB_VAR=${TEST_VAR/TEST_GITHUB_/GITHUB_}
45+
46+
# Get the values
47+
TEST_VALUE="${!TEST_VAR}"
48+
49+
# Check if the GITHUB variable exists
50+
if [[ -n "${!GITHUB_VAR+x}" ]]; then
51+
GITHUB_VALUE="${!GITHUB_VAR}"
52+
53+
# Compare values
54+
if [[ "$TEST_VALUE" != "$GITHUB_VALUE" ]]; then
55+
MISMATCHED_VARS+=("$TEST_VAR vs $GITHUB_VAR")
56+
TEST_VALUES+=("$TEST_VALUE")
57+
ACTUAL_VALUES+=("$GITHUB_VALUE")
58+
echo "Mismatch found: $TEST_VAR=$TEST_VALUE, $GITHUB_VAR=$GITHUB_VALUE"
59+
else
60+
echo "Match: $TEST_VAR=$TEST_VALUE, $GITHUB_VAR=$GITHUB_VALUE"
61+
fi
62+
else
63+
echo "Skipping $TEST_VAR as $GITHUB_VAR does not exist"
64+
fi
65+
done
66+
67+
# Set output for next steps
68+
if [[ ${#MISMATCHED_VARS[@]} -gt 0 ]]; then
69+
echo "has_mismatches=true" >>"$GITHUB_OUTPUT"
70+
71+
# Create a JSON array of mismatched variables for the issue
72+
MISMATCHED_JSON=$(printf '"%s",' "${MISMATCHED_VARS[@]}" | sed 's/,$//')
73+
MISMATCHED_JSON="[$MISMATCHED_JSON]"
74+
echo "mismatched_vars=$MISMATCHED_JSON" >>"$GITHUB_OUTPUT"
75+
76+
# Create a JSON array of test values
77+
TEST_JSON=$(printf '"%s",' "${TEST_VALUES[@]}" | sed 's/,$//')
78+
TEST_JSON="[$TEST_JSON]"
79+
echo "test_values=$TEST_JSON" >>"$GITHUB_OUTPUT"
80+
81+
# Create a JSON array of actual values
82+
ACTUAL_JSON=$(printf '"%s",' "${ACTUAL_VALUES[@]}" | sed 's/,$//')
83+
ACTUAL_JSON="[$ACTUAL_JSON]"
84+
echo "actual_values=$ACTUAL_JSON" >>"$GITHUB_OUTPUT"
85+
86+
# Create summary table
87+
{
88+
echo "## GitHub Variable Mismatches";
89+
echo "| Variable Pair | TEST Value | GITHUB Value |";
90+
echo "| ------------- | ---------- | ------------ |";
91+
} >>"$GITHUB_STEP_SUMMARY"
92+
93+
for i in "${!MISMATCHED_VARS[@]}"; do
94+
echo "| ${MISMATCHED_VARS[$i]} | ${TEST_VALUES[$i]} | ${ACTUAL_VALUES[$i]} |" >>"$GITHUB_STEP_SUMMARY"
95+
done
96+
else
97+
echo "has_mismatches=false" >>"$GITHUB_OUTPUT"
98+
echo "## All GitHub Variables Match" >>"$GITHUB_STEP_SUMMARY"
99+
echo "All action GITHUB_ variables match their official GITHUB_ counterparts." >>"$GITHUB_STEP_SUMMARY"
100+
fi
101+
shell: bash
102+
103+
- name: Create issue for mismatches
104+
if: steps.compare.outputs.has_mismatches == 'true' && github.event_name != 'pull_request'
105+
uses: actions/github-script@v6
106+
with:
107+
github-token: ${{ secrets.GITHUB_TOKEN }}
108+
script: |
109+
const mismatchedVars = JSON.parse('${{ steps.compare.outputs.mismatched_vars }}');
110+
const testValues = JSON.parse('${{ steps.compare.outputs.test_values }}');
111+
const actualValues = JSON.parse('${{ steps.compare.outputs.actual_values }}');
112+
113+
// Create table for issue body
114+
let tableBody = '| Variable Pair | TEST Value | GITHUB Value |\n';
115+
tableBody += '| ------------- | ---------- | ------------ |\n';
116+
117+
for (let i = 0; i < mismatchedVars.length; i++) {
118+
tableBody += `| ${mismatchedVars[i]} | ${testValues[i]} | ${actualValues[i]} |\n`;
119+
}
120+
121+
// Create the issue
122+
const issueTitle = `GitHub Variable Mismatches Detected - ${new Date().toISOString().split('T')[0]}`;
123+
const issueBody = `## GitHub Variable Mismatches Detected\n\nThe following mismatches were found between the action **GITHUB_** variables and their official **GITHUB_** counterparts:\n\n${tableBody}\n\nPlease review these discrepancies and update the test variables as needed.`;
124+
125+
await github.rest.issues.create({
126+
owner: context.repo.owner,
127+
repo: context.repo.repo,
128+
title: issueTitle,
129+
body: issueBody,
130+
labels: ['bug']
131+
});
132+
133+
console.log('Issue created for GitHub variable mismatches');
134+
135+
- name: Fail workflow if mismatches detected
136+
if: steps.compare.outputs.has_mismatches == 'true' && github.event_name == 'pull_request'
137+
run: |
138+
echo "::error::GitHub variable mismatches detected! See the summary for details."
139+
exit 1

0 commit comments

Comments
 (0)