|
| 1 | +/** |
| 2 | + * setupCopilot.js |
| 3 | + * |
| 4 | + * Reads versions from: |
| 5 | + * - versions.json at repo root (if present), else |
| 6 | + * - directories under mc/ |
| 7 | + * |
| 8 | + * For each version it runs: |
| 9 | + * ./gradlew :mc:<version>:runServer --stacktrace |
| 10 | + * |
| 11 | + * Uses child_process.execSync(cmd, { stdio: 'inherit' }) so logs stream to the action console. |
| 12 | + * Errors are caught, logged, and the loop continues. At the end the script writes |
| 13 | + * success_versions and failed_versions to $GITHUB_OUTPUT (if available) for downstream steps. |
| 14 | + * |
| 15 | + * To force the action to fail if any version errors, set env FAIL_ON_ERROR=true in the workflow. |
| 16 | + */ |
| 17 | + |
| 18 | +const cp = require('child_process') |
| 19 | +const fs = require('fs') |
| 20 | +const { join } = require('path') |
| 21 | + |
| 22 | +const failOnError = process.env.FAIL_ON_ERROR === 'true' |
| 23 | + |
| 24 | +// Save some time by skipping old versions as we are not parallelizing builds |
| 25 | +const versions = require('../../versions.json') |
| 26 | +const SKIP_VERSIONS = versions.slice(0, versions.indexOf('1.21.3')) |
| 27 | + |
| 28 | +async function main () { |
| 29 | + |
| 30 | + if (!Array.isArray(versions) || versions.length === 0) { |
| 31 | + console.error('No versions found (no VERSION env, no versions.json, no mc/* directories).') |
| 32 | + process.exit(1) |
| 33 | + } |
| 34 | + |
| 35 | + console.log('Versions to build:', versions.join(', ')) |
| 36 | + |
| 37 | + process.chdir(join(__dirname, '../../')) |
| 38 | + |
| 39 | + const successes = [] |
| 40 | + const failures = [] |
| 41 | + |
| 42 | + for (const v of versions) { |
| 43 | + if (SKIP_VERSIONS.includes(v)) { |
| 44 | + console.log(`Skipping old version ${v}...`) |
| 45 | + continue |
| 46 | + } |
| 47 | + console.log('') |
| 48 | + console.log('=== Building version:', v, '===') |
| 49 | + const cmd = `./gradlew :mc:${v}:runServer --stacktrace` |
| 50 | + try { |
| 51 | + // stream output to the runner logs |
| 52 | + cp.execSync(cmd, { stdio: 'inherit' }) |
| 53 | + successes.push(v) |
| 54 | + console.log(`✅ ${v} succeeded`) |
| 55 | + } catch (err) { |
| 56 | + failures.push(v) |
| 57 | + console.error(`❌ ${v} failed (continuing to next version)`) |
| 58 | + // continue to next version |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + // Write outputs for downstream jobs, if GITHUB_OUTPUT available |
| 63 | + const ghOut = process.env.GITHUB_OUTPUT |
| 64 | + if (ghOut) { |
| 65 | + try { |
| 66 | + fs.appendFileSync(ghOut, `success_versions=${JSON.stringify(successes)}\n`) |
| 67 | + fs.appendFileSync(ghOut, `failed_versions=${JSON.stringify(failures)}\n`) |
| 68 | + } catch (e) { |
| 69 | + console.warn('Could not write to GITHUB_OUTPUT:', e && e.message) |
| 70 | + } |
| 71 | + } else { |
| 72 | + console.log('GITHUB_OUTPUT not found; printing summaries to stdout instead.') |
| 73 | + console.log('success_versions=', JSON.stringify(successes)) |
| 74 | + console.log('failed_versions=', JSON.stringify(failures)) |
| 75 | + } |
| 76 | + |
| 77 | + console.log('') |
| 78 | + console.log('Summary:') |
| 79 | + console.log(' succeeded:', JSON.stringify(successes)) |
| 80 | + console.log(' failed: ', JSON.stringify(failures)) |
| 81 | + |
| 82 | + if (failOnError && failures.length > 0) { |
| 83 | + console.error('FAIL_ON_ERROR is true and some builds failed — exiting with non-zero code.') |
| 84 | + process.exit(1) |
| 85 | + } |
| 86 | + |
| 87 | + // otherwise exit 0 so the workflow continues (Copilot can triage failures) |
| 88 | + process.exit(0) |
| 89 | +} |
| 90 | + |
| 91 | +main() |
0 commit comments