'Github Actions | Port conflict on parallel runs

I have a React application that is built with Vite. If I don't specify a specific port for the preview application, the port is taken randomly. And then Cypress doesn't know which host/port to go to. If I specify a specific port I get an error that the port is already in use. I don't really know what to do and how to get around this.

My action config:

name: Node.js CI

# Controls when the workflow will run
on:
  # Triggers the workflow on push or pull request events but only for the master branch
  pull_request:
    branches: [ master ]

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  # This workflow contains a single job called "build"
  build:
    # The type of runner that the job will run on
    runs-on: ubuntu-latest

    strategy:
      fail-fast: false
      matrix:
        node-version: [16.x, 17.x]
        containers: [1, 2, 3, 4]

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
    - uses: actions/checkout@v3
    - name: Use Node.js ${{ matrix.node-version }}
      uses: actions/setup-node@v3
      with:
        node-version: ${{ matrix.node-version }}
    - run: npm ci
    - name: Cypress run
      uses: cypress-io/github-action@v2
      with:
        record: true
        parallel: true
        group: 'Actions example'
        build: npm run cypress:build
        start: npm run serve:ci
      env:
        CYPRESS_host: http://localhost
        CYPRESS_port: 41732
        CYPRESS_RECORD_KEY: ${{ secrets.CYPRESS_RECORD_KEY }}
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

package.json

...
"scripts": {
    "serve:ci": "vite preview --port=41732",
}
...

Error message

start server "npm run serve:ci command "npm run serve:ci"
current working directory "/home/runner/work/web-client-vite/web-client-vite"
/opt/hostedtoolcache/node/17.8.0/x64/bin/npm run serve:ci

> [email protected] serve:ci
> vite preview --port=41732

  > Local: http://localhost:41732/
  > Network: use `--host` to expose
[1850:0402/162628.909537:ERROR:bus.cc(392)] Failed to connect to the bus: Could not parse server address: Unknown address type (examples of valid types are "tcp" and on UNIX "unix")
[1850:0402/162628.909613:ERROR:bus.cc(392)] Failed to connect to the bus: Could not parse server address: Unknown address type (examples of valid types are "tcp" and on UNIX "unix")
[2031:0402/162628.932637:ERROR:gpu_init.cc(453)] Passthrough is not supported, GL is swiftshader, ANGLE is 
[2031:0402/162628.943271:ERROR:sandbox_linux.cc(374)] InitializeSandbox() called with multiple threads in process gpu-process.
Port 41732 is already in use.
Test run failed, code 1
More information might be available above
Cypress module has returned the following error message:
Could not find Cypress test run results
Error: Could not find Cypress test run results


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source