The Simplest CI Setup For Running Cypress Tests

Run Cypress tests in parallel with zero configuration by reusing a GitHubActions workflow.

Recently GitHub Actions CI service has introduced Reusable workflows which are ... amazing. You can create a workflow with multiple jobs and abstract all the testing details, and then call that workflow from another user workflow, passing inputs as parameters. Here is the entire workflow for installing NPM dependencies, caching them, then running Cypress tests in parallel across 3 machines using Cypress Dashboard parallelization:

.github/workflows/ci.yml
1
2
3
4
5
6
7
8
9
10
11
12
name: ci
on: [push]
jobs:
test:
# https://github.com/bahmutov/cypress-workflows
uses: bahmutov/cypress-workflows/.github/workflows/parallel.yml@v1
with:
n: 3
group: parallel tests
tag: parallel
secrets:
recordKey: ${{ secrets.CYPRESS_RECORD_KEY }}

That is it - the reusable public workflow parallel.yml is versioned too, so you know what parameter is expects.

The reusable workflow automatically creates several subjobs to run the tests in parallel

You can find my public Cypress workflows in the repo bahmutov/cypress-workflows and the example application in bahmutov/cypress-workflows-example

No more worrying about the specifics of installing and calling a testing tool, no more frustration. Just a single logical call to the workflow that takes care of the details. Truly the simplest way to write CI scripts today, I ❤️ it.

Bonus: check out my open source workshop Cypress on CI that explains how to run Cypress on a regular CI, using GitHub Actions, CircleCI, and Netlify Build.

Here is another example from bahmutov/cypress-3rd-party-script-example: let's say we deploy a site to GitHub Pages, and now want to run the local tests first before the deploy. We just add one more job with 3 lines of YML code:

.github/workflows/deploy.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
name: deploy
on: [push]

jobs:
# make sure the local tests pass
test:
uses: bahmutov/cypress-workflows/.github/workflows/standard.yml@v1

deploy:
needs: [test]
runs-on: ubuntu-20.04
# only deploy from the main branch
if: github.ref == 'refs/heads/main'
steps:
- name: Checkout 🛎️
uses: actions/checkout@v2

# https://github.com/marketplace/actions/github-pages-action
- name: Deploy 🚀
uses: peaceiris/actions-gh-pages@v3
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./public

So so simple, it hurts.