mirror of
https://github.com/rlespinasse/github-slug-action.git
synced 2026-05-17 18:35:07 +00:00
ci(check-variables): print a warning if a GITHUB_* variable from this action already exists (#155)
This commit is contained in:
committed by
GitHub
parent
797666298f
commit
ba00826602
139
.github/workflows/check-variables.yml
vendored
Normal file
139
.github/workflows/check-variables.yml
vendored
Normal file
@@ -0,0 +1,139 @@
|
||||
---
|
||||
name: Compare GitHub Variables
|
||||
|
||||
on:
|
||||
workflow_dispatch: # Manual trigger
|
||||
schedule:
|
||||
- cron: "0 0 * * 1" # Weekly on Monday at midnight
|
||||
pull_request:
|
||||
|
||||
permissions: read-all
|
||||
|
||||
jobs:
|
||||
compare-variables:
|
||||
runs-on: ubuntu-latest
|
||||
permissions:
|
||||
issues: write
|
||||
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Load environment variables for testing
|
||||
uses: rlespinasse/github-slug-action@v5
|
||||
with:
|
||||
prefix: TEST_
|
||||
|
||||
- name: Compare GitHub variables
|
||||
id: compare
|
||||
run: |
|
||||
# Initialize arrays to store results
|
||||
MISMATCHED_VARS=()
|
||||
TEST_VALUES=()
|
||||
ACTUAL_VALUES=()
|
||||
|
||||
# Get all environment variables
|
||||
ALL_ENV=$(env)
|
||||
|
||||
# Extract TEST_GITHUB variables
|
||||
TEST_VARS=$(echo "$ALL_ENV" | grep "^TEST_GITHUB_" | cut -d= -f1)
|
||||
|
||||
# Check for mismatches
|
||||
for TEST_VAR in $TEST_VARS; do
|
||||
# Get the corresponding GITHUB variable name
|
||||
GITHUB_VAR=${TEST_VAR/TEST_GITHUB_/GITHUB_}
|
||||
|
||||
# Get the values
|
||||
TEST_VALUE="${!TEST_VAR}"
|
||||
|
||||
# Check if the GITHUB variable exists
|
||||
if [[ -n "${!GITHUB_VAR+x}" ]]; then
|
||||
GITHUB_VALUE="${!GITHUB_VAR}"
|
||||
|
||||
# Compare values
|
||||
if [[ "$TEST_VALUE" != "$GITHUB_VALUE" ]]; then
|
||||
MISMATCHED_VARS+=("$TEST_VAR vs $GITHUB_VAR")
|
||||
TEST_VALUES+=("$TEST_VALUE")
|
||||
ACTUAL_VALUES+=("$GITHUB_VALUE")
|
||||
echo "Mismatch found: $TEST_VAR=$TEST_VALUE, $GITHUB_VAR=$GITHUB_VALUE"
|
||||
else
|
||||
echo "Match: $TEST_VAR=$TEST_VALUE, $GITHUB_VAR=$GITHUB_VALUE"
|
||||
fi
|
||||
else
|
||||
echo "Skipping $TEST_VAR as $GITHUB_VAR does not exist"
|
||||
fi
|
||||
done
|
||||
|
||||
# Set output for next steps
|
||||
if [[ ${#MISMATCHED_VARS[@]} -gt 0 ]]; then
|
||||
echo "has_mismatches=true" >>"$GITHUB_OUTPUT"
|
||||
|
||||
# Create a JSON array of mismatched variables for the issue
|
||||
MISMATCHED_JSON=$(printf '"%s",' "${MISMATCHED_VARS[@]}" | sed 's/,$//')
|
||||
MISMATCHED_JSON="[$MISMATCHED_JSON]"
|
||||
echo "mismatched_vars=$MISMATCHED_JSON" >>"$GITHUB_OUTPUT"
|
||||
|
||||
# Create a JSON array of test values
|
||||
TEST_JSON=$(printf '"%s",' "${TEST_VALUES[@]}" | sed 's/,$//')
|
||||
TEST_JSON="[$TEST_JSON]"
|
||||
echo "test_values=$TEST_JSON" >>"$GITHUB_OUTPUT"
|
||||
|
||||
# Create a JSON array of actual values
|
||||
ACTUAL_JSON=$(printf '"%s",' "${ACTUAL_VALUES[@]}" | sed 's/,$//')
|
||||
ACTUAL_JSON="[$ACTUAL_JSON]"
|
||||
echo "actual_values=$ACTUAL_JSON" >>"$GITHUB_OUTPUT"
|
||||
|
||||
# Create summary table
|
||||
{
|
||||
echo "## GitHub Variable Mismatches";
|
||||
echo "| Variable Pair | TEST Value | GITHUB Value |";
|
||||
echo "| ------------- | ---------- | ------------ |";
|
||||
} >>"$GITHUB_STEP_SUMMARY"
|
||||
|
||||
for i in "${!MISMATCHED_VARS[@]}"; do
|
||||
echo "| ${MISMATCHED_VARS[$i]} | ${TEST_VALUES[$i]} | ${ACTUAL_VALUES[$i]} |" >>"$GITHUB_STEP_SUMMARY"
|
||||
done
|
||||
else
|
||||
echo "has_mismatches=false" >>"$GITHUB_OUTPUT"
|
||||
echo "## All GitHub Variables Match" >>"$GITHUB_STEP_SUMMARY"
|
||||
echo "All action GITHUB_ variables match their official GITHUB_ counterparts." >>"$GITHUB_STEP_SUMMARY"
|
||||
fi
|
||||
shell: bash
|
||||
|
||||
- name: Create issue for mismatches
|
||||
if: steps.compare.outputs.has_mismatches == 'true' && github.event_name != 'pull_request'
|
||||
uses: actions/github-script@v6
|
||||
with:
|
||||
github-token: ${{ secrets.GITHUB_TOKEN }}
|
||||
script: |
|
||||
const mismatchedVars = JSON.parse('${{ steps.compare.outputs.mismatched_vars }}');
|
||||
const testValues = JSON.parse('${{ steps.compare.outputs.test_values }}');
|
||||
const actualValues = JSON.parse('${{ steps.compare.outputs.actual_values }}');
|
||||
|
||||
// Create table for issue body
|
||||
let tableBody = '| Variable Pair | TEST Value | GITHUB Value |\n';
|
||||
tableBody += '| ------------- | ---------- | ------------ |\n';
|
||||
|
||||
for (let i = 0; i < mismatchedVars.length; i++) {
|
||||
tableBody += `| ${mismatchedVars[i]} | ${testValues[i]} | ${actualValues[i]} |\n`;
|
||||
}
|
||||
|
||||
// Create the issue
|
||||
const issueTitle = `GitHub Variable Mismatches Detected - ${new Date().toISOString().split('T')[0]}`;
|
||||
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.`;
|
||||
|
||||
await github.rest.issues.create({
|
||||
owner: context.repo.owner,
|
||||
repo: context.repo.repo,
|
||||
title: issueTitle,
|
||||
body: issueBody,
|
||||
labels: ['bug']
|
||||
});
|
||||
|
||||
console.log('Issue created for GitHub variable mismatches');
|
||||
|
||||
- name: Fail workflow if mismatches detected
|
||||
if: steps.compare.outputs.has_mismatches == 'true' && github.event_name == 'pull_request'
|
||||
run: |
|
||||
echo "::error::GitHub variable mismatches detected! See the summary for details."
|
||||
exit 1
|
||||
Reference in New Issue
Block a user