fix(gha): cleaner and working matrix (#62081)

This commit is contained in:
Mrugesh Mohapatra
2025-09-08 12:10:41 +05:30
committed by GitHub
parent fb854f2552
commit ab3bab0967

View File

@@ -58,48 +58,87 @@ jobs:
matrix: ${{ steps.matrix.outputs.matrix }}
steps:
- name: Setup Matrix
uses: actions/github-script@v7
id: matrix
run: |
TARGET_LANG="${{ inputs.target_language || 'all' }}"
echo "Target language: $TARGET_LANG"
env:
TARGET_LANG: ${{ inputs.target_language }}
with:
script: |
// Constants
const NODE_VERSION = 22;
if [[ "$TARGET_LANG" == "all" ]]; then
# Build all languages
MATRIX='{
"node-version": [22],
"include": [
{"lang-name-full": "english", "lang-name-short": "eng"},
{"lang-name-full": "chinese", "lang-name-short": "chn"},
{"lang-name-full": "espanol", "lang-name-short": "esp"},
{"lang-name-full": "chinese-traditional", "lang-name-short": "cnt"},
{"lang-name-full": "italian", "lang-name-short": "ita"},
{"lang-name-full": "portuguese", "lang-name-short": "por"},
{"lang-name-full": "ukrainian", "lang-name-short": "ukr"},
{"lang-name-full": "japanese", "lang-name-short": "jpn"},
{"lang-name-full": "german", "lang-name-short": "ger"},
{"lang-name-full": "swahili", "lang-name-short": "swa"}
]
}'
else
# Build single language
case "$TARGET_LANG" in
"english") SHORT="eng" ;;
"chinese") SHORT="chn" ;;
"espanol") SHORT="esp" ;;
"chinese-traditional") SHORT="cnt" ;;
"italian") SHORT="ita" ;;
"portuguese") SHORT="por" ;;
"ukrainian") SHORT="ukr" ;;
"japanese") SHORT="jpn" ;;
"german") SHORT="ger" ;;
"swahili") SHORT="swa" ;;
*) echo "Error: Unknown language $TARGET_LANG"; exit 1 ;;
esac
MATRIX="{\"node-version\": [22], \"include\": [{\"lang-name-full\": \"$TARGET_LANG\", \"lang-name-short\": \"$SHORT\"}]}"
fi
// Input sanitization and validation
const rawTargetLang = process.env.TARGET_LANG || 'all';
const targetLang = rawTargetLang.trim().toLowerCase();
console.log(`Target language: ${targetLang}`);
echo "matrix=$MATRIX" >> $GITHUB_OUTPUT
// Language mappings (single source of truth)
const languageMap = {
'english': 'eng',
'chinese': 'chn',
'espanol': 'esp',
'chinese-traditional': 'cnt',
'italian': 'ita',
'portuguese': 'por',
'ukrainian': 'ukr',
'japanese': 'jpn',
'german': 'ger',
'swahili': 'swa'
};
const allLanguages = Object.keys(languageMap);
let matrix;
if (targetLang === 'all') {
console.log('Building matrix for all languages');
console.log(`Available languages: ${allLanguages.join(', ')}`);
// Build include array for all languages
const include = allLanguages.map(lang => ({
'node-version': NODE_VERSION,
'lang-name-full': lang,
'lang-name-short': languageMap[lang]
}));
matrix = {
include: include
};
} else {
console.log(`Building matrix for single language: ${targetLang}`);
// Validate language selection
if (!languageMap[targetLang]) {
const errorMsg = `Unknown language '${targetLang}'. Available: ${allLanguages.join(', ')}`;
console.error(errorMsg);
core.setFailed(errorMsg);
return;
}
console.log(`Processing: ${targetLang} -> ${languageMap[targetLang]}`);
// Create single language matrix
matrix = {
include: [{
'node-version': NODE_VERSION,
'lang-name-full': targetLang,
'lang-name-short': languageMap[targetLang]
}]
};
}
// Final validation
if (!matrix || !matrix.include || matrix.include.length === 0) {
core.setFailed('Generated matrix is empty or invalid');
return;
}
console.log('Generated matrix:');
console.log(JSON.stringify(matrix, null, 2));
console.log(`Matrix will create ${matrix.include.length} job(s)`);
// Set output for GitHub Actions
core.setOutput('matrix', JSON.stringify(matrix));
client:
name: Clients - [${{ needs.setup-jobs.outputs.tgt_env_short }}] [${{ matrix.lang-name-short }}]
@@ -107,7 +146,7 @@ jobs:
runs-on: ubuntu-22.04
strategy:
fail-fast: false
matrix: ${{ fromJSON(needs.setup-matrix.outputs.matrix) }}
matrix: ${{ fromJson(needs.setup-matrix.outputs.matrix) }}
permissions:
deployments: write
contents: read