From 32e89424824c2021e08f2dab259bc756335a6006 Mon Sep 17 00:00:00 2001 From: Rob Hogan <2590098+robhogan@users.noreply.github.com> Date: Mon, 23 Nov 2026 15:54:12 +0000 Subject: [PATCH] [visual testing] Read the storage bucket from the visual config Retakes never run on pull requests from forks. Capture reads the public baseline URL from the `VISUAL_PUBLIC_BASE_URL` repository variable, and variables are empty in `pull_request` runs from forks, so `capture.js` skips the retake. That was the case in all 115 fork capture jobs from 2026-11-19 to 2026-11-23, and forks, Meta exports included, made 55 of the 77 PR reports in that time. All 3 transient differences reported on PRs then were on forks (two Apple Intelligence notifications and an unsettled scroll), while retakes replaced 6 bad captures on same-repo PRs and `main`. This names the bucket once, as `bucket` in `packages/rn-tester/visual-tests/config.json`, and works out its public URL from it. Capture, compare and approve all read it from there, and the upload steps read it with `jq`. The `VISUAL_BUCKET` and `VISUAL_PUBLIC_BASE_URL` variables are no longer used and can be deleted once this lands, so there is no second copy to drift. `VISUAL_GCP_PROJECT` and `VISUAL_WORKLOAD_IDENTITY_PROVIDER` stay, since they grant upload access and keep compare from running on forks of the repository. Changelog: [Internal] Test plan: New unit tests for the config, and actionlint reports nothing for the three workflows. Against the real bucket, with nothing uploaded and no GitHub writes: compare's `prepare` on main run 45069821697 found both baselines at distance 0 with no differences, as CI did, approve's `prepare` for #60201 got the fingerprint of its stored report, and capture's baseline lookup found the iOS baseline at the URL worked out from the config. --- .../__tests__/visual-report-test.js | 15 +++++++++++++++ .github/workflow-scripts/visual/approve-run.js | 14 +++++++++++--- .github/workflow-scripts/visual/capture.js | 15 ++++++++++----- .github/workflow-scripts/visual/compare-run.js | 13 ++++++++----- .github/workflow-scripts/visual/storage.js | 16 ++++++++++++++++ .github/workflows/visual-approve-rntester.yml | 7 ++++--- .github/workflows/visual-capture-rntester.yml | 5 +++-- .github/workflows/visual-compare-rntester.yml | 3 +-- packages/rn-tester/visual-tests/README.md | 2 ++ packages/rn-tester/visual-tests/config.json | 1 + 10 files changed, 71 insertions(+), 20 deletions(-) diff --git a/.github/workflow-scripts/__tests__/visual-report-test.js b/.github/workflow-scripts/__tests__/visual-report-test.js index 19276b83133e..7acaff1be20d 100644 --- a/.github/workflow-scripts/__tests__/visual-report-test.js +++ b/.github/workflow-scripts/__tests__/visual-report-test.js @@ -25,6 +25,7 @@ const { blobKey, publicUrl, reportPrefix, + storageLocation, } = require('../visual/storage'); const {SHA_A, SHA_B} = require('./__fixtures__/visual'); @@ -79,10 +80,24 @@ describe('visual reports and approval', () => { }), ], [() => approvalKey(1, '../x')], + [() => storageLocation({})], + [() => storageLocation({bucket: 'gs://rn-visual'})], ])('rejects invalid input', build => { expect(build).toThrow('Invalid'); }); + test('reads the bucket and its public URL from the visual config', () => { + expect(storageLocation({bucket: 'rn-visual'})).toEqual({ + bucket: 'rn-visual', + publicBaseUrl: 'https://storage.googleapis.com/rn-visual', + }); + expect(() => + storageLocation( + require('../../../packages/rn-tester/visual-tests/config.json'), + ), + ).not.toThrow(); + }); + test('encodes each path segment of public URLs', () => { expect(publicUrl('https://cdn.example/bucket/', 'a b/c#d.png')).toBe( 'https://cdn.example/bucket/a%20b/c%23d.png', diff --git a/.github/workflow-scripts/visual/approve-run.js b/.github/workflow-scripts/visual/approve-run.js index d144115805ae..d873659206c7 100644 --- a/.github/workflow-scripts/visual/approve-run.js +++ b/.github/workflow-scripts/visual/approve-run.js @@ -31,6 +31,7 @@ const { fetchJsonOrNull, listPrefixes, publicUrl, + storageLocation, } = require('./storage'); const fs = require('node:fs'); const path = require('node:path'); @@ -43,6 +44,12 @@ function requireEnv(name) { return value; } +function readStorageLocation() { + return storageLocation( + JSON.parse(fs.readFileSync(requireEnv('VISUAL_CONFIG'), 'utf8')), + ); +} + /** The results of the most recent comparison for a PR head, or null. */ async function latestResults({bucket, publicBaseUrl, prNumber, headSha}) { const runs = await listPrefixes( @@ -83,9 +90,10 @@ async function prepare() { const prNumber = event.pull_request.number; const headSha = event.pull_request.head.sha; + const {bucket, publicBaseUrl} = readStorageLocation(); const results = await latestResults({ - bucket: requireEnv('VISUAL_BUCKET'), - publicBaseUrl: requireEnv('VISUAL_PUBLIC_BASE_URL'), + bucket, + publicBaseUrl, prNumber, headSha, }); @@ -149,7 +157,7 @@ async function report() { ? null : await fetchJsonOrNull( publicUrl( - requireEnv('VISUAL_PUBLIC_BASE_URL'), + readStorageLocation().publicBaseUrl, approvalKey(prNumber, fingerprint), ), ); diff --git a/.github/workflow-scripts/visual/capture.js b/.github/workflow-scripts/visual/capture.js index f564130f514f..59b1099e7f6d 100644 --- a/.github/workflow-scripts/visual/capture.js +++ b/.github/workflow-scripts/visual/capture.js @@ -18,6 +18,7 @@ const {readAndroidEnvironment, readIosEnvironment} = require('./manifest'); const {retakeScreenshots} = require('./retake'); +const {storageLocation} = require('./storage'); const childProcess = require('node:child_process'); const fs = require('node:fs'); const path = require('node:path'); @@ -32,9 +33,10 @@ node capture.js [ios_udi Installs the app, then runs every flow in in one Maestro invocation, writing screenshots under . Failed flows are retried up to ${DEFAULT_ATTEMPTS} attempts in total (VISUAL_CAPTURE_ATTEMPTS). With -VISUAL_PUBLIC_BASE_URL set, screenshots that differ from the baseline or have -none are retaken once, and the IDs of new screenshots whose two captures -differ are written as a JSON array to VISUAL_UNSTABLE_PATH. +VISUAL_CONFIG set to the visual config, screenshots that differ from the +baseline in the bucket it names, or have none, are retaken once, and the IDs +of new screenshots whose two captures differ are written as a JSON array to +VISUAL_UNSTABLE_PATH. `; /** The flows directly inside `flowsDir`, sorted. Helpers live elsewhere. */ @@ -200,9 +202,12 @@ async function main(args = process.argv.slice(2)) { summary.push(...pending.map(flow => `- \`${flow}\``)); } - const publicBaseUrl = process.env.VISUAL_PUBLIC_BASE_URL; - if (publicBaseUrl != null && publicBaseUrl !== '') { + const configPath = process.env.VISUAL_CONFIG; + if (configPath != null && configPath !== '') { try { + const {publicBaseUrl} = storageLocation( + JSON.parse(fs.readFileSync(configPath, 'utf8')), + ); const {unstable, lines} = await retakeScreenshots({ platform, environment: diff --git a/.github/workflow-scripts/visual/compare-run.js b/.github/workflow-scripts/visual/compare-run.js index 57cd90f1f097..dbd192869635 100644 --- a/.github/workflow-scripts/visual/compare-run.js +++ b/.github/workflow-scripts/visual/compare-run.js @@ -44,6 +44,7 @@ const { findBaseline, publicUrl, reportPrefix, + storageLocation, } = require('./storage'); const {validateCapture} = require('./validate'); const fs = require('node:fs'); @@ -61,6 +62,10 @@ function requireEnv(name) { return value; } +function readConfig() { + return JSON.parse(fs.readFileSync(requireEnv('VISUAL_CONFIG'), 'utf8')); +} + function writeFile(filePath, contents) { fs.mkdirSync(path.dirname(filePath), {recursive: true}); fs.writeFileSync(filePath, contents); @@ -283,10 +288,8 @@ async function prepare() { const github = createGitHubClient(); const artifactsDir = requireEnv('VISUAL_ARTIFACTS_DIR'); const outDir = requireEnv('OUT_DIR'); - const publicBaseUrl = requireEnv('VISUAL_PUBLIC_BASE_URL'); - const config = JSON.parse( - fs.readFileSync(requireEnv('VISUAL_CONFIG'), 'utf8'), - ); + const config = readConfig(); + const {publicBaseUrl} = storageLocation(config); const context = await resolveContext(github); if (context == null) { @@ -381,7 +384,7 @@ function platformConclusion(platform) { async function report() { const github = createGitHubClient(); const outDir = requireEnv('OUT_DIR'); - const publicBaseUrl = requireEnv('VISUAL_PUBLIC_BASE_URL'); + const {publicBaseUrl} = storageLocation(readConfig()); const state = JSON.parse( fs.readFileSync(path.join(outDir, 'state.json'), 'utf8'), ); diff --git a/.github/workflow-scripts/visual/storage.js b/.github/workflow-scripts/visual/storage.js index bb46d6031437..f8dc2d46d6b2 100644 --- a/.github/workflow-scripts/visual/storage.js +++ b/.github/workflow-scripts/visual/storage.js @@ -79,6 +79,21 @@ function approvalKey(prNumber, fingerprint) { return `approvals/pull/${prNumber}/${fingerprint}.json`; } +/** + * The Google Cloud Storage bucket named in config.json, and the public URL + * its objects are read from. Uploads go to the same bucket. + */ +function storageLocation(config) { + const bucket = config?.bucket; + if ( + typeof bucket !== 'string' || + !/^[a-z0-9][a-z0-9._-]+[a-z0-9]$/.test(bucket) + ) { + throw new Error(`Invalid bucket in the visual config: ${String(bucket)}`); + } + return {bucket, publicBaseUrl: `https://storage.googleapis.com/${bucket}`}; +} + function publicUrl(publicBaseUrl, key) { return `${publicBaseUrl.replace(/\/$/, '')}/${key .split('/') @@ -176,4 +191,5 @@ module.exports = { listPrefixes, publicUrl, reportPrefix, + storageLocation, }; diff --git a/.github/workflows/visual-approve-rntester.yml b/.github/workflows/visual-approve-rntester.yml index 39c38c40a707..4325f634b110 100644 --- a/.github/workflows/visual-approve-rntester.yml +++ b/.github/workflows/visual-approve-rntester.yml @@ -17,8 +17,7 @@ concurrency: env: OUT_DIR: /tmp/visual-approval - VISUAL_BUCKET: ${{ vars.VISUAL_BUCKET }} - VISUAL_PUBLIC_BASE_URL: ${{ vars.VISUAL_PUBLIC_BASE_URL }} + VISUAL_CONFIG: packages/rn-tester/visual-tests/config.json jobs: approve: @@ -64,7 +63,9 @@ jobs: uses: google-github-actions/setup-gcloud@aa5489c8933f4cc7a4f7d45035b3b1440c9c10db # v3.0.1 - name: Record approval if: steps.staged.outputs.upload == 'true' - run: gcloud storage cp --recursive --no-clobber "$OUT_DIR/upload/*" "gs://$VISUAL_BUCKET/" + run: | + VISUAL_BUCKET=$(jq -er .bucket "$VISUAL_CONFIG") + gcloud storage cp --recursive --no-clobber "$OUT_DIR/upload/*" "gs://$VISUAL_BUCKET/" - name: Update gate env: GITHUB_TOKEN: ${{ github.token }} diff --git a/.github/workflows/visual-capture-rntester.yml b/.github/workflows/visual-capture-rntester.yml index b10cb7b71963..aea60c84a416 100644 --- a/.github/workflows/visual-capture-rntester.yml +++ b/.github/workflows/visual-capture-rntester.yml @@ -22,8 +22,9 @@ env: VISUAL_FLOWS: packages/rn-tester/visual-tests/flows VISUAL_OUTPUT_DIR: /tmp/visual-capture VISUAL_HEAD_SHA: ${{ github.event.pull_request.head.sha || github.sha }} - # Read-only, to retake screenshots that differ from the baseline. - VISUAL_PUBLIC_BASE_URL: ${{ vars.VISUAL_PUBLIC_BASE_URL }} + # Names the bucket that screenshots differing from the baseline are + # retaken against. Read-only, over its public URL. + VISUAL_CONFIG: packages/rn-tester/visual-tests/config.json # New screenshots whose two captures differ, for manifest.js. Outside the # output directory, so it isn't uploaded. VISUAL_UNSTABLE_PATH: /tmp/visual-unstable.json diff --git a/.github/workflows/visual-compare-rntester.yml b/.github/workflows/visual-compare-rntester.yml index fc3b753df8eb..451421432ed1 100644 --- a/.github/workflows/visual-compare-rntester.yml +++ b/.github/workflows/visual-compare-rntester.yml @@ -26,8 +26,6 @@ concurrency: env: OUT_DIR: /tmp/visual-out VISUAL_ARTIFACTS_DIR: /tmp/visual-artifacts - VISUAL_BUCKET: ${{ vars.VISUAL_BUCKET }} - VISUAL_PUBLIC_BASE_URL: ${{ vars.VISUAL_PUBLIC_BASE_URL }} VISUAL_CONFIG: packages/rn-tester/visual-tests/config.json jobs: @@ -136,6 +134,7 @@ jobs: - name: Upload report and baseline if: steps.staged.outputs.upload == 'true' run: | + VISUAL_BUCKET=$(jq -er .bucket "$VISUAL_CONFIG") # Objects are write-once; --no-clobber makes a rerun skip what an # earlier attempt already uploaded. gcloud storage cp --recursive --no-clobber "$OUT_DIR/upload/*" "gs://$VISUAL_BUCKET/" diff --git a/packages/rn-tester/visual-tests/README.md b/packages/rn-tester/visual-tests/README.md index 81c626864ab0..dd2f23c85cc7 100644 --- a/packages/rn-tester/visual-tests/README.md +++ b/packages/rn-tester/visual-tests/README.md @@ -14,6 +14,8 @@ Only `main` and pull requests into it are compared. Stable branches have no base The baseline for a PR is the one for the commit it's based on, or the nearest earlier commit on `main` that has one. Baselines are keyed by device environment too (OS image, screen size, density, font scale, locale and so on), so a change to the CI device starts a fresh baseline rather than failing every PR. +Baselines, reports and approvals are stored in the public Google Cloud Storage bucket named by `bucket` in `config.json`. Capture reads it over its public URL. Visual Compare RNTester and Visual Approve RNTester upload to it with the access granted by the repository's `VISUAL_GCP_PROJECT` and `VISUAL_WORKLOAD_IDENTITY_PROVIDER` variables, so moving the bucket means changing both. + ## Adding a test 1. Pick or add an RNTester example, and give its root view a `testID`. Keep the example's content fixed: no dates, random values, network images or animations that are still running when the screenshot is taken. diff --git a/packages/rn-tester/visual-tests/config.json b/packages/rn-tester/visual-tests/config.json index d6d45c7a70f6..e385df35d019 100644 --- a/packages/rn-tester/visual-tests/config.json +++ b/packages/rn-tester/visual-tests/config.json @@ -1,4 +1,5 @@ { + "bucket": "react-native-visual-testing", "defaults": { "threshold": 0, "includeAA": true,