Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 30 additions & 5 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,37 @@ jobs:
'.github/workflows/test.yml', ...caller ? [caller] : []]);
selected = targets.filter(site => testChanges || changed(site, baseline, sha));
names = selected.map(site => site.context);
compatible = (site, revision) => {
// A rebase leaves previews built from commits outside the PR's
// history. Netlify skips a new build when their inputs did not
// change, so compare the inputs, not the history.
const isCommit = revision => {
try { git(['cat-file', '-e', `${revision}^{commit}`]); return true; }
catch { return false; }
};
// Only origin still has a commit a force-push removed. A failed
// fetch is tried again on the next poll.
const available = revision => {
if (isCommit(revision)) return true;
const auth = Buffer.from(`x-access-token:${core.getInput('github-token')}`).toString('base64');
try {
execFileSync('git', ['fetch', '--quiet', '--no-tags', '--no-write-fetch-head', 'origin', revision], {
cwd: root, stdio: 'ignore', timeout: Math.max(1000, Math.min(30000, deadline - Date.now())),
env: {...process.env, GIT_CONFIG_COUNT: '1', GIT_CONFIG_KEY_0: 'http.https://github.com/.extraheader',
GIT_CONFIG_VALUE_0: `AUTHORIZATION: basic ${auth}`},
});
} catch { return false; }
return isCommit(revision);
};
// Only the PR's own previews can predate a rebase; production must
// come from its history.
const inHistory = revision => {
try { git(['merge-base', '--is-ancestor', revision, sha]); return true; }
catch { return false; }
};
compatible = (site, revision, production = false) => {
if (!/^[0-9a-f]{40}$/.test(revision || '')) return false;
if (revision === sha) return true;
try { git(['merge-base', '--is-ancestor', revision, sha]); }
catch { return false; }
return !changed(site, revision, sha);
return (production ? inHistory(revision) : available(revision)) && !changed(site, revision, sha);
};
core.info(`Affected previews: ${names.join(', ') || 'none'}`);
if (!names.length) {
Expand Down Expand Up @@ -211,7 +236,7 @@ jobs:
// Static websites may have no first preview when their build was skipped.
// Signed-in apps must always use their isolated preview.
const production = await revisionAt(target.url);
if (compatible(target, production)) {
if (compatible(target, production, true)) {
urls[name] = target.url;
revisions[target.url] = production;
}
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ These callers use the Check workflow's optional `test-command` to run unit and s

Single-site callers using `preview-checks` also require `/.well-known/build.json` to identify the tested PR head. Every selected revision is checked again after Web waits for its shared-account lock.

Monorepos can give Test a `deployments` array of Netlify `context`, production `url`, repository-relative `config` and optional `base`. Preview selects targets by running each native `build.ignore` against the cumulative PR diff. Changes in `tests/`, `.github/workflows/test.yml` or the calling workflow select every target. Netlify uses the same ignore rule against its cached revision. Skipped previews are reusable only when `/.well-known/build.json` proves an ancestor with unchanged build inputs. Static websites may explicitly enable `production-fallback: true` for a skipped first preview; signed-in apps always require an isolated preview. `QA_DEPLOY_URLS` contains only selected targets, and repositories must exclude absent targets from their suites. An explicitly empty selection skips Web and passes Test; missing or failed selection never does.
Monorepos can give Test a `deployments` array of Netlify `context`, production `url`, repository-relative `config` and optional `base`. Preview selects targets by running each native `build.ignore` against the cumulative PR diff. Changes in `tests/`, `.github/workflows/test.yml` or the calling workflow select every target. Netlify uses the same ignore rule against its cached revision. Skipped previews are reusable only when `/.well-known/build.json` names a commit with unchanged build inputs, also one a rebase removed from the PR. Static websites may explicitly enable `production-fallback: true` for a skipped first preview; signed-in apps always require an isolated preview. `QA_DEPLOY_URLS` contains only selected targets, and repositories must exclude absent targets from their suites. An explicitly empty selection skips Web and passes Test; missing or failed selection never does.

After a push to main, Check runs source checks and unit/service tests again, Netlify runs the same build command, and Test live waits for the published revision before safe live HTTP/browser tests. Its `deployments` input lists HTTPS origins and their repository-relative Netlify config/base paths. Each build publishes `/.well-known/build.json` with its `COMMIT_REF`. For an older production deploy, Test live accepts it only if it is an ancestor of the requested revision and that site's existing Netlify `build.ignore` command confirms unchanged inputs. No Netlify token or second build is needed.

Expand Down
28 changes: 27 additions & 1 deletion tests/preview.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ async function scenario(options = {}) {
}},
}};
await execute(github, {repo: {owner: 'o', repo: 'r'}, payload: {pull_request: pull}},
{info() {}, setOutput: (name, value) => { outputs[name] = value; }},
{info() {}, getInput: () => '', setOutput: (name, value) => { outputs[name] = value; }},
{env: {...process.env, PREVIEW_CHECKS: JSON.stringify(names), DEPLOYMENTS: JSON.stringify(options.targets ?? []), GITHUB_WORKSPACE: options.root,
GITHUB_WORKFLOW_REF: options.workflowRef ?? ''}},
{now: () => clock}, callback => { clock += 60 * 1000; callback(); },
Expand Down Expand Up @@ -165,6 +165,32 @@ test('a later unrelated push still tests the cumulative PR change on a proven un
assert.deepEqual(JSON.parse(result.outputs['deploy-urls']), {'netlify/app/deploy-preview': appAlias});
});

test('a rebased PR reuses the preview built before the rebase when its inputs did not change', async t => {
const f = fixture(t);
const origin = mkdtempSync(join(tmpdir(), 'qa-origin-'));
t.after(() => rmSync(origin, {recursive: true, force: true}));
const git = (...args) => execFileSync('git', args, {cwd: f.root, encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe']}).trim();
execFileSync('git', ['init', '-q', '--bare', origin]);
git('remote', 'add', 'origin', origin);
const built = f.commit('apps/app/source.txt', 'app changed');
// Like GitHub, origin serves a force-pushed commit no branch points at.
execFileSync('git', ['-C', origin, 'config', 'uploadpack.allowAnySHA1InWant', 'true']);
git('push', '-q', 'origin', `${built}:refs/heads/before-rebase`);
git('push', '-q', 'origin', '--delete', 'before-rebase');
// The base moves on and the PR is rebased onto it; only origin keeps the
// commit Netlify built.
git('reset', '-q', '--hard', f.base);
const base = f.commit('README.md', 'base moved on');
const head = f.commit('apps/app/source.txt', 'app changed');
git('reflog', 'expire', '--expire=now', '--all');
git('gc', '-q', '--prune=now');
assert.throws(() => git('cat-file', '-e', `${built}^{commit}`));
const options = {...f, base, head, statuses: [skipped('netlify/app/deploy-preview')]};
const result = await scenario({...options, revisions: {[appAlias]: built}});
assert.deepEqual(JSON.parse(result.outputs['deploy-urls']), {'netlify/app/deploy-preview': appAlias});
await assert.rejects(scenario({...options, revisions: {[appAlias]: 'f'.repeat(40)}}), /within 12 minutes/);
});

test('a skipped or manually canceled changed app cannot reuse stale code or production', async t => {
const f = fixture(t);
const head = f.commit('apps/app/source.txt', 'app changed');
Expand Down