You can put retry inside the spec.ts files or in the config.json to use Cypress's built in retry mechanism. The problem is very often we see test cases fail the first time and all subsequent retries, but when re-running the e2e command they pass. This solves that by re-running the e2e command on the spec files that failed. Also, putting retry(2) in Cypress's config.json doubles the time the e2e stage takes in the pipeline if most of the tests are broken or otherwise failing.
Re-running all the tests if there is a failure will make the stage take twice as long. On top of that, if any tests that passed the first run fail in the second run, then the stage will fail, whereas we want the e2e stage to pass even if some tests passed in only one of the runs.
Nrwl Nx's e2e command, and perhaps Cypress's own commands, only provide a filter for spec files. There is a package called cypress-grep that could be used. It could be a nice improvement, although it might make this gist even more complex.