How to run Jenkins parallel cypress on different agents? How to run Jenkins parallel cypress on different agents? docker docker

How to run Jenkins parallel cypress on different agents?


I was able to get this to work by explicityly defining the agent within each parallel stage:

parallel {    stage('cypress tester A') {        agent {            node: {                label "slave-1"            }        }        steps {            echo "Running build ${env.BUILD_ID}"            sh "npm run cypress:run"        }    }    stage('cypress tester B') {        agent {            node: {                label "slave-2"            }        }        steps {            echo "Running build ${env.BUILD_ID}"            sh "npm run cypress:run"        }    }    stage('cypress tester C') {        agent {            node: {                label "slave-3"            }        }        steps {            echo "Running build ${env.BUILD_ID}"            sh "npm run cypress:run"        }    }}

However, one disadvantage I found is now that you're running cypress in each individual node/virtual machine, cypress needs to know where to find the running instance of your application. Cypress looks into cypress.json at baseUrl to see where to find your app. Its common to use a localhost address for development, which means cypress runnning on slave-1 will look for an app running on localhost of slave-1 - but there isn't one, so it will fail.

For simplicity's sake, I just did an npm install and npm start & npx wait-on http://localhost:3000 in each node:

stage('cypress tester A') {    agent {        node: {            label "slave-1"        }    }    steps {        echo "Running build ${env.BUILD_ID}"        sh "npm install --silent"        sh "npm start & npx wait-on http://localhost:3000"        sh "npm run cypress:run"    }}

This is obviously not very efficient because you have to install and run the app on each node. However, you could potentially set up a previous stage on a dedicated node (say, slave-0) to install and serve your project, and use that. Within your Jenkinsfile, you'll need to know the IP of that slave-0, or you could get it dynamically within your Jenkinsfile. Then instead of installing and running your project on slave-1, 2 and 3, you would install and run it just on slave-0, and use the CYPRESS_BASE_URL env variable to tell cypress where to find the running instance of your app. If the IP of slave-0 is 2222.2222.2222.2222, you might try something like this:

pipeline {    stage ('Serve your project'){       agent {           label 'slave-0'       }       steps {           sh 'npm install --silent'           sh 'npm start & npx wait-on http://localhost:3000'       }    }    stage('Cypress'){        environment {            CYPRESS_BASE_URL=2222.2222.2222.2222:3000            // other env vars        }        parallel {            stage {               agent {                   label 'slave-1'               }               steps {                   echo "Running build ${env.BUILD_ID}"                   sh "npm run cypress:run"               }            }            // more parallel stages        }    }}

There's a lot of variations you can do, but hopefully that will get you started.