cypress-io/github-action

repository·master·Indexed 23 days ago

https://github.com/cypress-io/github-action

GitHub Action for running Cypress end-to-end and component tests. It automates dependency installation, caching, server startup, and result recording to Cypress Cloud. The action supports multiple package managers (npm, pnpm, Yarn Classic), various browsers (Chrome, Firefox, Edge, Electron), and can be run in Docker containers using cypress/browsers or cypress/included images.

Tokens
14.2K
Snippets
51
Records
69
Agent score
81%

What's inside cypress-io/github-action

  1. Overview of cypress-io/github-action

    master

    The cypress-io/github-action is a GitHub Action designed to run Cypress End-to-End or Component tests within GitHub Actions CI workflows. It provides several high-level features to simplify test execution, including:

    • Simplified Execution: Single-line calls to install dependencies and run tests.
    • App Coordination: Built-in options to build the application, start a server, and wait for a specific URL to be available before running tests.
    • Dependency Management: Automatic dependency installation based on npm, pnpm, or Yarn Classic lock files.
    • Caching: Caching of the Cypress binary and dependencies (for npm and Yarn Classic) to reduce bandwidth and speed up runs.
    • Cypress Cloud Integration: Support for recording test results to Cypress Cloud, including parallel execution.
    • Observability: Generation of job summaries for quick access to results and built-in debugging utilities like a ping utility for server reachability.
    • Environment Independence: Docker compatibility to ensure stable environments regardless of GitHub-hosted runner changes.
  2. Node.js runtime requirements

    master

    Node.js is required to run this action.

    • Action Runtime: github-action@v7 uses node24 to run the action itself.
    • Command Execution: Options like install-command, build, start, and command are executed using the Node.js version present on the GitHub runner.

    To use a specific Node.js version for your build/install commands, use actions/setup-node before calling the Cypress action.

  3. Print Cypress and OS information

    master

    To debug your environment, you can run npx cypress info to print Cypress and OS information, including detected browsers.

    If you are not using the build parameter, you can simply pass npx cypress info to the build option.

    If you are already using the build parameter for your application, you should split the action into two steps: one for installation (runTests: false) and one for running the info command, followed by the actual test run (install: false).

    # Option 1: Using the build parameter directly
    - name: Cypress run
      uses: cypress-io/github-action@v7
      with:
        build: npx cypress info
    
    # Option 2: Splitting steps if 'build' is already used for your app
    - name: Cypress install
      uses: cypress-io/github-action@v7
      with:
        runTests: false
    - name: Cypress info
      run: npx cypress info
    - name: Cypress run
      uses: cypress-io/github-action@v7
      with:
        install: false
        # rest of your parameters
  4. Store Cypress videos and screenshots as GitHub Artifacts

    master

    If you are not recording to Cypress Cloud, you can use actions/upload-artifact to save videos and screenshots generated during the test run.

    To save space, you can use the GitHub Actions if: failure() conditional to only upload screenshots when a test fails.

    - uses: actions/checkout@v7
    - uses: cypress-io/github-action@v7
    # after the test run completes store videos and any screenshots
    - uses: actions/upload-artifact@v7
      # add the line below to store screenshots only on failures
      # if: failure()
      with:
        name: cypress-screenshots
        path: cypress/screenshots
        if-no-files-found: ignore # 'warn' or 'error' are also available, defaults to `warn`
    - uses: actions/upload-artifact@v7
      with:
        name: cypress-videos
        path: cypress/videos
        if-no-files-found: ignore
  5. Configure Cypress with Yarn Plug'n'Play (PnP)

    master

    When using Yarn Modern with Plug'n'Play enabled, you must use the command parameter to invoke yarn directly instead of the default npx.

    Caution: Using the command parameter causes all other parameters to be ignored.

          - name: Cypress run
            uses: cypress-io/github-action@v7
            with:
              working-directory: examples/yarn-modern-pnp
              install-command: yarn install
              command: yarn run --binaries-only cypress run
  6. Configure Cypress with Yarn Workspaces

    master

    The action automatically discovers Yarn workspaces. When running tests located in a subfolder of a workspace, specify the working-directory pointing to that subfolder.

          - uses: cypress-io/github-action@v7
            with:
              working-directory: examples/start-and-yarn-workspaces/workspace-1
              build: yarn run build
              start: yarn start
              wait-on: 'http://localhost:5000'
  7. Customize the `ci-build-id` for parallel runs

    master

    When running parallel tests, you can overwrite the ci-build-id used to link separate machines into a single run in Cypress Cloud.

    Best Practice: To avoid "Build already finished" errors when re-running a workflow, generate a unique ID in a separate job using a timestamp or UUID, then pass it to the testing jobs.

    Example of a robust parallel setup:

    jobs:
      prepare:
        outputs:
          uuid: ${{ steps.uuid.outputs.value }}
        steps:
          - name: Generate unique ID 💎
            id: uuid
            run: echo "value=sha-$GITHUB_SHA-time-$(date +\"%s\")" >> $GITHUB_OUTPUT
    
      smoke-tests:
        needs: ['prepare']
        steps:
          - uses: actions/checkout@v7
          - uses: cypress-io/github-action@v7
            with:
              record: true
              parallel: true
              ci-build-id: ${{ needs.prepare.outputs.uuid }}
            env:
              CYPRESS_RECORD_KEY: ${{ secrets.EXAMPLE_RECORDING_KEY }}
              GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  8. Split Installation and Test Execution

    master

    To run additional commands (like linting) between dependency installation and test execution, or to separate jobs, use the install and runTests parameters.

    • To only install dependencies: set runTests: false.
    • To only run tests (assuming dependencies are already installed): set install: false.
          - name: Install dependencies
            uses: cypress-io/github-action@v7
            with:
              runTests: false
          - run: yarn lint
          - name: Run e2e tests
            uses: cypress-io/github-action@v7
            with:
              install: false
              working-directory: e2e
  9. Run Cypress tests in parallel with Cypress Cloud

    master

    To load balance tests across multiple machines, use a GitHub Actions strategy: matrix to spawn multiple containers.

    Requirements:

    • A Cypress Cloud account.
    • Set record: true and parallel: true in the action configuration.
    • Provide a group name to group the parallel runs.
    • Pass CYPRESS_RECORD_KEY and GITHUB_TOKEN in the env section.

    Important: Set fail-fast: false in your matrix strategy. If one container fails and kills the job immediately, it can leave Cypress Cloud processes hanging.

    Note: The action does not create containers; it uses the GitHub matrix to link containers into a single logical Cypress Cloud run.

    name: Parallel Cypress Tests
    on: push
    jobs:
      test:
        name: Cypress run
        runs-on: ubuntu-24.04
        strategy:
          fail-fast: false
          matrix:
            containers: [1, 2, 3]
        steps:
          - name: Checkout
            uses: actions/checkout@v7
          - name: Cypress run
            uses: cypress-io/github-action@v7
            with:
              record: true
              parallel: true
              group: 'Actions example'
            env:
              CYPRESS_RECORD_KEY: ${{ secrets.EXAMPLE_RECORDING_KEY }}
              GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  10. Install dependencies with caching

    master

    The action automatically installs local dependencies using lock files. Ensure exactly one lock file is present at the root or in the working directory.

    Lock filePackage ManagerInstallation command
    package-lock.jsonnpmnpm ci
    pnpm-lock.yamlpnpmpnpm install --frozen-lockfile
    yarn.lockYarn Classicyarn --frozen-lockfile

    Note on Caching:

    • Cypress binary: Cached in $HOME/.cache/Cypress (or CYPRESS_CACHE_FOLDER) with label cypress-<platform-and-architecture>-hash.
    • npm: Cached in $HOME/.npm with label npm-<platform-and-architecture>-hash.
    • Yarn Classic: Cached in $HOME/.cache/yarn with label npm-<platform-and-architecture>-hash.
    • pnpm & Yarn Modern: The action does not include built-in caching for these. Use actions/setup-node to enable caching for these package managers.