Unverified Commit 1a808968 authored by Sebastian "Sebbie" Silbermann's avatar Sebastian "Sebbie" Silbermann Committed by GitHub

[15.5.x][ci] Run flake detection and new deploy tests when merged and on backport branches (#98019)

parent 6d9b5d8e
...@@ -50,7 +50,11 @@ jobs: ...@@ -50,7 +50,11 @@ jobs:
steps: steps:
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
with: with:
fetch-depth: 1 # Changing the checked out ref has wide implications:
# Be aware that all git-diff operations in the repo rely on the
# default behavior of GitHub actions (ref on push, merge-commit on PR).
# 2 last commits so that we can create a diff with HEAD~1
fetch-depth: 2
- run: echo "${{ github.event.after }}" - run: echo "${{ github.event.after }}"
- name: Setup node - name: Setup node
uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6.4.0 uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6.4.0
......
...@@ -614,9 +614,7 @@ jobs: ...@@ -614,9 +614,7 @@ jobs:
test-new-tests-dev: test-new-tests-dev:
name: Test new tests for flakes (dev) name: Test new tests for flakes (dev)
needs: ['optimize-ci', 'changes', 'build-native', 'build-next'] needs: ['optimize-ci', 'changes', 'build-native', 'build-next']
# test-new-tests-if if: ${{ needs.optimize-ci.outputs.skip == 'false' && needs.changes.outputs.docs-only == 'false' }}
if: false
# test-new-tests-end-if
strategy: strategy:
fail-fast: false fail-fast: false
...@@ -642,9 +640,7 @@ jobs: ...@@ -642,9 +640,7 @@ jobs:
test-new-tests-start: test-new-tests-start:
name: Test new tests for flakes (prod) name: Test new tests for flakes (prod)
needs: ['optimize-ci', 'changes', 'build-native', 'build-next'] needs: ['optimize-ci', 'changes', 'build-native', 'build-next']
# test-new-tests-if if: ${{ needs.optimize-ci.outputs.skip == 'false' && needs.changes.outputs.docs-only == 'false' }}
if: false
# test-new-tests-end-if
strategy: strategy:
fail-fast: false fail-fast: false
...@@ -668,10 +664,8 @@ jobs: ...@@ -668,10 +664,8 @@ jobs:
test-new-tests-deploy: test-new-tests-deploy:
name: Test new tests when deployed name: Test new tests when deployed
needs: ['optimize-ci', 'test-prod', 'test-new-tests-start'] needs: ['optimize-ci', 'changes', 'test-prod', 'test-new-tests-start']
# test-new-tests-if if: ${{ needs.optimize-ci.outputs.skip == 'false' && needs.changes.outputs.docs-only == 'false' && needs.changes.outputs.is-release == 'false' }}
if: false
# test-new-tests-end-if
strategy: strategy:
fail-fast: false fail-fast: false
......
...@@ -194,6 +194,9 @@ jobs: ...@@ -194,6 +194,9 @@ jobs:
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
with: with:
# Changing the checked out ref has wide implications:
# Be aware that all git-diff operations in the repo rely on the
# default behavior of GitHub actions (ref on push, merge-commit on PR).
fetch-depth: 25 fetch-depth: 25
# local action -> needs to run after checkout # local action -> needs to run after checkout
......
...@@ -77,12 +77,6 @@ async function main() { ...@@ -77,12 +77,6 @@ async function main() {
.replace(`['canary']`, `['${branchName}']`) .replace(`['canary']`, `['${branchName}']`)
.replace(/[\s]{1,}('test-new-tests-.+',)/g, '') .replace(/[\s]{1,}('test-new-tests-.+',)/g, '')
buildAndTest = buildAndTest.replace(
/(^[ \t]*)# test-new-tests-if\n(^[ \t]*)if:.*\n(^[ \t]*)# test-new-tests-end-if/gm,
(_, indent1, indent2, indent3) =>
`${indent1}# test-new-tests-if\n${indent2}if: false\n${indent3}# test-new-tests-end-if`
)
await fs.promises.writeFile(buildAndTestPath, buildAndTest) await fs.promises.writeFile(buildAndTestPath, buildAndTest)
await execa(`git add .`, { await execa(`git add .`, {
......
...@@ -2,83 +2,19 @@ ...@@ -2,83 +2,19 @@
import fs from 'fs/promises' import fs from 'fs/promises'
import execa from 'execa' import execa from 'execa'
import path from 'path' import path from 'path'
import { getDiffRevision, getGitInfo } from './git-info.js'
/** /**
* Detects changed tests files by comparing the current branch with `origin/canary` * Detects changed (see {@link getDiffRevision}) tests files.
* Returns tests separated by test mode (dev/prod), as well as the corresponding commit hash * Returns tests separated by test mode (dev/prod), as well as the corresponding commit hash
* that the current branch is pointing to * that the current branch is pointing to
*/ */
export default async function getChangedTests() { export default async function getChangedTests() {
let eventData = {}
/** @type import('execa').Options */ /** @type import('execa').Options */
const EXECA_OPTS = { shell: true } const EXECA_OPTS = { shell: true }
/** @type import('execa').Options */
const EXECA_OPTS_STDIO = { ...EXECA_OPTS, stdio: 'inherit' }
try {
eventData =
JSON.parse(
await fs.readFile(process.env.GITHUB_EVENT_PATH || '', 'utf8')
)['pull_request'] || {}
} catch (_) {}
const branchName =
eventData?.head?.ref ||
process.env.GITHUB_REF_NAME ||
(await execa('git rev-parse --abbrev-ref HEAD', EXECA_OPTS)).stdout
const remoteUrl =
eventData?.head?.repo?.full_name ||
process.env.GITHUB_REPOSITORY ||
(await execa('git remote get-url origin', EXECA_OPTS)).stdout
const commitSha =
eventData?.head?.sha ||
process.env.GITHUB_SHA ||
(await execa('git rev-parse HEAD', EXECA_OPTS)).stdout
const isCanary = const { branchName, remoteUrl, commitSha } = await getGitInfo()
branchName.trim() === 'canary' && remoteUrl.includes('vercel/next.js') const diffRevision = await getDiffRevision()
if (isCanary) {
console.log(`Skipping flake detection for canary`)
return { devTests: [], prodTests: [] }
}
let diffRevision
if (
process.env.GITHUB_ACTIONS === 'true' &&
process.env.GITHUB_EVENT_NAME === 'pull_request'
) {
// GH Actions for `pull_request` run on the merge commit so HEAD~1:
// 1. includes all changes in the PR
// e.g. in
// A-B-C-main - F
// \ /
// D-E-branch
// GH actions for `branch` runs on F, so a diff for HEAD~1 includes the diff of D and E combined
// 2. Includes all changes of the commit for pushes
diffRevision = 'HEAD~1'
} else {
try {
await execa(
'git remote set-branches --add origin canary',
EXECA_OPTS_STDIO
)
await execa('git fetch origin canary --depth=20', EXECA_OPTS_STDIO)
} catch (err) {
console.error(await execa('git remote -v', EXECA_OPTS_STDIO))
console.error(`Failed to fetch origin/canary`, err)
}
// TODO: We should diff against the merge base with origin/canary not directly against origin/canary.
// A --- B ---- origin/canary
// \
// \-- C ---- HEAD
// `git diff origin/canary` includes B and C
// But we should only include C.
diffRevision = 'origin/canary'
}
const changesResult = await execa( const changesResult = await execa(
`git diff ${diffRevision} --name-only`, `git diff ${diffRevision} --name-only`,
...@@ -91,7 +27,6 @@ export default async function getChangedTests() { ...@@ -91,7 +27,6 @@ export default async function getChangedTests() {
{ {
branchName, branchName,
remoteUrl, remoteUrl,
isCanary,
commitSha, commitSha,
}, },
`\ngit diff:\n${changesResult.stderr}\n${changesResult.stdout}` `\ngit diff:\n${changesResult.stderr}\n${changesResult.stdout}`
......
// @ts-check
const fs = require('fs/promises')
const { promisify } = require('util')
const { exec: execOrig } = require('child_process')
const exec = promisify(execOrig)
/**
* Gets git repository information from the environment
* @returns {Promise<{branchName: string, remoteUrl: string, commitSha: string}>}
*/
async function getGitInfo() {
let eventData = {}
try {
eventData =
JSON.parse(
await fs.readFile(process.env.GITHUB_EVENT_PATH || '', 'utf8')
)['pull_request'] || {}
} catch (_) {}
const branchName =
eventData?.head?.ref ||
process.env.GITHUB_REF_NAME ||
(await exec('git rev-parse --abbrev-ref HEAD')).stdout.trim()
const remoteUrl =
eventData?.head?.repo?.full_name ||
process.env.GITHUB_REPOSITORY ||
(await exec('git remote get-url origin')).stdout.trim()
const commitSha =
eventData?.head?.sha ||
process.env.GITHUB_SHA ||
(await exec('git rev-parse HEAD')).stdout.trim()
return { branchName, remoteUrl, commitSha }
}
/**
* Determines the appropriate git diff revision based on the environment
* @returns {Promise<string>} The git revision to diff against
*/
async function getDiffRevision() {
if (process.env.GITHUB_ACTIONS === 'true') {
const eventName = process.env.GITHUB_EVENT_NAME
switch (eventName) {
// GH Actions for `pull_request` run on the merge commit by default so HEAD~1:
// 1. includes all changes in the PR
// e.g. in
// A-B-C-main - F
// \ /
// D-E-branch
// GH actions for `branch` runs on F, so a diff for HEAD~1 includes the diff of D and E combined
// 2. Includes all changes of the commit for pushes (assuming the push event is from a squash merge)
case 'pull_request':
case 'push':
return 'HEAD~1'
default:
throw new Error(`Unsupported GITHUB_EVENT_NAME: ${eventName}`)
}
} else {
try {
await exec('git remote set-branches --add origin canary')
await exec('git fetch origin canary --depth=20')
} catch (err) {
const remoteInfo = await exec('git remote -v')
console.error(remoteInfo.stdout)
console.error(remoteInfo.stderr)
console.error(`Failed to fetch origin/canary`, err)
}
// TODO: We should diff against the merge base with origin/canary not directly against origin/canary.
// A --- B ---- origin/canary
// \
// \-- C ---- HEAD
// `git diff origin/canary` includes B and C
// But we should only include C.
return 'origin/canary'
}
}
module.exports = { getDiffRevision, getGitInfo }
const { promisify } = require('util') const { promisify } = require('util')
const { exec: execOrig, spawn } = require('child_process') const { exec: execOrig, spawn } = require('child_process')
const { getDiffRevision, getGitInfo } = require('./git-info.js')
const exec = promisify(execOrig) const exec = promisify(execOrig)
...@@ -55,44 +56,17 @@ const CHANGE_ITEM_GROUPS = { ...@@ -55,44 +56,17 @@ const CHANGE_ITEM_GROUPS = {
} }
async function main() { async function main() {
let eventData = {} const { branchName, remoteUrl } = await getGitInfo()
const diffRevision = await getDiffRevision()
try {
eventData = require(process.env.GITHUB_EVENT_PATH)['pull_request'] || {} const changesResult = await exec(
} catch (_) {} `git diff ${diffRevision} --name-only`
).catch((err) => {
const branchName = console.error(err)
eventData?.head?.ref || return { stdout: '' }
process.env.GITHUB_REF_NAME || })
(await exec('git rev-parse --abbrev-ref HEAD')).stdout
console.error({ branchName, remoteUrl, changesResult })
const remoteUrl =
eventData?.head?.repo?.full_name ||
process.env.GITHUB_REPOSITORY ||
(await exec('git remote get-url origin')).stdout
const isCanary =
branchName.trim() === 'canary' && remoteUrl.includes('vercel/next.js')
try {
await exec('git remote set-branches --add origin canary')
await exec('git fetch origin canary --depth=20')
} catch (err) {
console.error(await exec('git remote -v'))
console.error(`Failed to fetch origin/canary`, err)
}
// if we are on the canary branch only diff current commit
const toDiff = isCanary
? `${process.env.GITHUB_SHA || 'canary'}~`
: 'origin/canary...'
const changesResult = await exec(`git diff ${toDiff} --name-only`).catch(
(err) => {
console.error(err)
return { stdout: '' }
}
)
console.error({ branchName, remoteUrl, isCanary, changesResult })
const changedFilesOutput = changesResult.stdout const changedFilesOutput = changesResult.stdout
const typeIndex = process.argv.indexOf('--type') const typeIndex = process.argv.indexOf('--type')
......
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