Cypress baseUrl mismatch (CYPRESS_BASE_URL ignored) in CI
Cypress resolves baseUrl from config, the CYPRESS_BASE_URL env var, and --config baseUrl=..., in increasing precedence. When CI sets one but the run reads another, visits target the wrong host, causing connection or unexpected-content failures that look like app bugs.
What this error means
Tests pass against the intended environment locally but in CI hit the wrong host (production instead of preview, or localhost instead of a deploy URL), often surfacing as ECONNREFUSED or wrong-page assertions.
cy.visit() failed trying to load:
https://staging.example.com/
(expected the preview URL set in CYPRESS_BASE_URL, but the config baseUrl
http://localhost:3000 was used instead)Common causes
The env var name or scope is wrong
Setting BASE_URL instead of CYPRESS_BASE_URL, or exporting it in a step where Cypress does not run, leaves the config default in effect.
A --config flag overrides the env var
A --config baseUrl=... on the command line takes precedence, silently winning over the env var you set.
How to fix it
Set CYPRESS_BASE_URL in the run step
Export the correctly prefixed variable in the same step that runs Cypress so it overrides the config default.
- run: npx cypress run
env:
CYPRESS_BASE_URL: ${{ steps.deploy.outputs.preview-url }}Pass baseUrl explicitly and consistently
Use one mechanism: either the env var or --config, not both, so precedence is unambiguous.
npx cypress run --config baseUrl=$PREVIEW_URLHow to prevent it
- Use the
CYPRESS_prefix for env-var overrides. - Pick one override mechanism per pipeline to avoid precedence surprises.
- Echo the resolved baseUrl at run start to confirm the target.