Commit 12208162 authored by Hendrik Liebau's avatar Hendrik Liebau Committed by Hendrik Liebau

[ci] Skip irrelevant jobs for test-only PRs

This PR optimizes GitHub Actions workflows for PRs that only change test
files in `test/e2e`, `test/development`, `test/integration`,
`test/production`, or `test/unit` directories. For test-only PRs, only
essential jobs run: builds, linting, type checking, unit test jobs, and
flake detection and deployment tests for the affected changes. This
reduces CI costs and execution time. The implementation follows the
existing docs-only pattern for consistency. It also enhances the logic
to detect related tests when fixtures or supporting files are changed by
searching upward to find the nearest directory containing test files.
parent 5c045fb2
This diff is collapsed.
......@@ -46,20 +46,24 @@ jobs:
fetch-depth: 25
- name: Check non-docs only change
run: echo "DOCS_CHANGE<<EOF" >> $GITHUB_OUTPUT; echo "$(node scripts/run-for-change.mjs --not --type docs --exec echo 'nope')" >> $GITHUB_OUTPUT; echo 'EOF' >> $GITHUB_OUTPUT
run: echo "DOCS_ONLY<<EOF" >> $GITHUB_OUTPUT; echo "$(node scripts/run-for-change.mjs --not --type docs --exec echo 'false')" >> $GITHUB_OUTPUT; echo 'EOF' >> $GITHUB_OUTPUT
id: docs-change
- name: Check for test-only change
run: echo "TEST_ONLY<<EOF" >> $GITHUB_OUTPUT; echo "$(node scripts/run-for-change.mjs --not --type test --exec echo 'false')" >> $GITHUB_OUTPUT; echo 'EOF' >> $GITHUB_OUTPUT
id: test-only-change
- uses: actions/download-artifact@v4
if: ${{ steps.docs-change.outputs.DOCS_CHANGE == 'nope' }}
if: ${{ steps.docs-change.outputs.DOCS_ONLY == 'false' && steps.test-only-change.outputs.TEST_ONLY == 'false' }}
with:
name: next-swc-binary
path: packages/next-swc/native
- run: cp -r packages/next-swc/native .github/actions/next-stats-action/native
if: ${{ steps.docs-change.outputs.DOCS_CHANGE == 'nope' }}
if: ${{ steps.docs-change.outputs.DOCS_ONLY == 'false' && steps.test-only-change.outputs.TEST_ONLY == 'false' }}
- uses: ./.github/actions/next-stats-action
if: ${{ steps.docs-change.outputs.DOCS_CHANGE == 'nope' }}
if: ${{ steps.docs-change.outputs.DOCS_ONLY == 'false' && steps.test-only-change.outputs.TEST_ONLY == 'false' }}
env:
# This uses the webpack bundle analyzer and for consistent results we need to use webpack.
# Once there is an equivalent analyzer for turbopack, we can remove this.
......
......@@ -42,8 +42,42 @@ export default async function getChangedTests() {
// run each test 3 times in each test mode (if E2E) with no-retrying
// and if any fail it's flakey
const devTests = []
const prodTests = []
const devTests = new Set()
const prodTests = new Set()
// Helper function to add test file to appropriate lists
const addTestFile = (testFile) => {
if (
testFile.startsWith('test/e2e/') ||
testFile.startsWith('test/integration/')
) {
devTests.add(testFile)
prodTests.add(testFile)
} else if (testFile.startsWith('test/prod')) {
prodTests.add(testFile)
} else if (testFile.startsWith('test/development')) {
devTests.add(testFile)
}
}
// Helper function to find test files in directory
const findTestFilesInDir = async (dir) => {
try {
const entries = await fs.readdir(path.join(process.cwd(), dir), {
withFileTypes: true,
})
const testFiles = []
for (const entry of entries) {
const fullPath = path.join(dir, entry.name).replace(/\\/g, '/')
if (entry.isFile() && entry.name.match(/\.test\.(js|ts|tsx)$/)) {
testFiles.push(fullPath)
}
}
return testFiles
} catch (err) {
return []
}
}
for (let file of changedFiles) {
// normalize slashes
......@@ -54,17 +88,64 @@ export default async function getChangedTests() {
.catch(() => false)
if (fileExists && file.match(/^test\/.*?\.test\.(js|ts|tsx)$/)) {
if (
file.startsWith('test/e2e/') ||
file.startsWith('test/integration/')
) {
devTests.push(file)
prodTests.push(file)
} else if (file.startsWith('test/prod')) {
prodTests.push(file)
} else if (file.startsWith('test/development')) {
devTests.push(file)
// Direct test file change
addTestFile(file)
} else if (
fileExists &&
file.match(/^test\/(e2e|development|integration|production)\//)
) {
// Changed file in specific test directories
// Search upward from the changed file to find the directory containing test files
const parts = file.split('/')
const testCategory = parts[1] // e2e, development, integration, or production
const testCategoryPrefix = `test/${testCategory}`
// Helper to recursively find all test files in a directory
const findTestFilesRecursive = async (dir) => {
const testFiles = []
try {
const entries = await fs.readdir(path.join(process.cwd(), dir), {
withFileTypes: true,
})
for (const entry of entries) {
const fullPath = path.join(dir, entry.name).replace(/\\/g, '/')
if (entry.isDirectory()) {
// Recursively search subdirectories
const subTests = await findTestFilesRecursive(fullPath)
testFiles.push(...subTests)
} else if (entry.name.match(/\.test\.(js|ts|tsx)$/)) {
testFiles.push(fullPath)
}
}
} catch (err) {
// Directory doesn't exist or can't be read
}
return testFiles
}
// Search upward from the changed file's directory to find where test files are
// Start from the parent directory of the changed file, working upward
// Stop when we find a directory containing test files
let testRootDir = null
for (let i = parts.length - 1; i >= 2; i--) {
const currentDir = parts.slice(0, i).join('/')
const testFiles = await findTestFilesInDir(currentDir)
if (testFiles.length > 0) {
testRootDir = currentDir
break
}
}
// If we found a directory with test files, search it recursively
// Otherwise fall back to the test category directory
if (!testRootDir) {
testRootDir = testCategoryPrefix
}
const testFiles = await findTestFilesRecursive(testRootDir)
testFiles.forEach(addTestFile)
}
}
......@@ -72,13 +153,17 @@ export default async function getChangedTests() {
'Detected tests:',
JSON.stringify(
{
devTests,
prodTests,
devTests: Array.from(devTests),
prodTests: Array.from(prodTests),
},
null,
2
)
)
return { devTests, prodTests, commitSha }
return {
devTests: Array.from(devTests),
prodTests: Array.from(prodTests),
commitSha,
}
}
......@@ -54,6 +54,13 @@ const CHANGE_ITEM_GROUPS = {
'test/integration/create-next-app',
'scripts/send-trace-to-jaeger',
],
test: [
'test/e2e',
'test/development',
'test/integration',
'test/production',
'test/unit',
],
}
async function main() {
......
......@@ -6,23 +6,26 @@ import getChangedTests from './get-changed-tests.mjs'
/**
* Run tests for added/changed tests in the current branch
* CLI Options:
* --mode: test mode (dev, deploy, start)
* --mode: test mode (dev, start, deploy)
* - dev: flake detection in dev mode (runs tests 3 times)
* - start: flake detection in prod mode (runs tests 3 times)
* - deploy: deploy tests (runs tests 1 time with deployed tarball)
* --group: current group number / total groups
* --flake-detection: run tests multiple times to detect flaky
* --with-webpack: run tests with Webpack (in addition to Turbopack for flake detection)
*/
async function main() {
let argv = await yargs(process.argv.slice(2))
.string('mode')
.string('group')
.boolean('flake-detection').argv
.boolean('with-webpack').argv
let testMode = argv.mode
const isFlakeDetectionMode = argv['flake-detection']
const attempts = isFlakeDetectionMode ? 3 : 1
const attempts = testMode === 'deploy' ? 1 : 3
const withWebpack = argv['with-webpack']
if (testMode && !['dev', 'deploy', 'start'].includes(testMode)) {
throw new Error(
`Invalid test mode: ${testMode}. Must be one of: dev, deploy, start`
`Invalid test mode: ${testMode}. Must be one of: dev, start, deploy`
)
}
......@@ -143,27 +146,32 @@ async function main() {
)
}
if (isFlakeDetectionMode && testMode !== 'deploy') {
// Run Webpack tests for deploy mode, or if the --with-webpack flag is set.
if (testMode === 'deploy' || withWebpack) {
for (let i = 0; i < attempts; i++) {
console.log(
`\n\nRun ${i + 1}/${attempts} for ${testMode} tests (Turbopack)`
`\n\nRun ${i + 1}/${attempts} for ${testMode} tests (Webpack)`
)
await execa('node', [...RUN_TESTS_ARGS, ...currentTests], {
...EXECA_OPTS_STDIO,
env: {
...process.env,
NEXT_EXTERNAL_TESTS_FILTERS,
NEXT_TEST_MODE: testMode,
NEXT_TEST_VERSION: nextTestVersion,
IS_TURBOPACK_TEST: '1',
TURBOPACK_BUILD: testMode === 'start' ? '1' : undefined,
TURBOPACK_DEV: testMode === 'dev' ? '1' : undefined,
IS_WEBPACK_TEST: '1',
},
})
}
} else {
for (let i = 0; i < attempts; i++) {
console.log(`\n\nRun ${i + 1}/${attempts} for ${testMode} tests`)
}
// TODO: We should also run deploy tests with Turbopack.
if (testMode !== 'deploy') {
for (let i = 0; i < attempts; i++) {
console.log(
`\n\nRun ${i + 1}/${attempts} for ${testMode} tests (Turbopack)`
)
await execa('node', [...RUN_TESTS_ARGS, ...currentTests], {
...EXECA_OPTS_STDIO,
env: {
......@@ -171,7 +179,9 @@ async function main() {
NEXT_EXTERNAL_TESTS_FILTERS,
NEXT_TEST_MODE: testMode,
NEXT_TEST_VERSION: nextTestVersion,
IS_WEBPACK_TEST: '1',
IS_TURBOPACK_TEST: '1',
TURBOPACK_BUILD: testMode === 'start' ? '1' : undefined,
TURBOPACK_DEV: testMode === 'dev' ? '1' : undefined,
},
})
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment