Running cypress test in Jenkins pipeline

Jenkins setup:

Jenkins: 2.448
OS: Linux - 4.19.0-17-amd64
Java: 17.0.10 - Debian (OpenJDK 64-Bit Server VM)

Addons at pastebin: ant:497.v94e7d9fffa_b_9antisamy-markup-formatter:162.v0e6ec0fcfcf6apache-htt - Pastebin.com

Hello all, I am trying to implement a pipeline that will run cypress tests before deploying a containerized version of the FE.

I have the following Pipeline

pipeline {
    agent {
        label '{functional_node}'
    }
        stages {
            stage('TEST') {
                agent {
                        docker {
                            image 'cypress/base:20.9.0'
                        }
                }
                steps{
                    git branch: 'development',
                    credentialsId: '{redacted}',
                    url: '{URL}'

                    sh 'ng serve'
                    sh 'npm i'
                    sh 'npm run cypress:run'
                }

            }

            stage('Build and Deploy Docker') {
                steps {
                    agent {
                        label '{functional_node}'
                    }
                    git branch: 'development',
                    credentialsId: '{redacted}',
                    url: '{URL}'

                    sh 'cp -f /apps/jenkins/workspace/"${JOB_NAME}"/src/environments/environment.ts /apps/jenkins/workspace/"${JOB_NAME}"/src/environments/environment.prod.ts'
                    sh 'docker build . -t {just_a_name}:latest '
                    sh 'docker rm -vf $(docker ps -a -q --filter="name={just_a_name}")'
                    sh 'docker run -d --restart unless-stopped -p 90:80 --name {just_a_name} {just_a_name}:latest'
                    sh 'docker system prune -f'
                }
            }
        }
}

The Git Repo:
The repo is connected and can be read from without issue it contains the Dockerfile used to ultimately create the docker image and then run it. Further it has a package.json which outlines the following to lines in the scripts section:

"cypress:open": "cypress open",
"cypress:run":"cypress run"

Important Vars substituted:

  • {functional_node} is a dockerhost which contains several different running containers of various applications and this is where ultimately I would like the final tested FE application to end up running.

I thought this would begin the pipe from my {functional_node} use the docker that exists there to pull down the dockerhub image for cypress, fetch the code base, and then run my declared sh commands hopefully starting the angular serve, installing all npm packages, and finally running the cypress run script.

This is not what happens instead I see this:

/apps/jenkins/workspace/cypress-testing@tmp/durable-77aede15/script.sh.copy: 1: /apps/jenkins/workspace/cypress-testing@tmp/durable-77aede15/script.sh.copy: docker: not found

And… I can’t really make heads or tails of what’s happening to any assistance would be really appreciated.

Seems docker is not in the path.

Note:
Here

sh 'cp -f /apps/jenkins/workspace/"${JOB_NAME}"/src/environments/environment.ts /apps/jenkins/workspace/"${JOB_NAME}"/src/environments/environment.prod.ts'

the usage of ${JOB_NAME} might lead to unexpected results. There is no guarantee that this is the workspace of your job, e.g. when you have parallel execution the path is different (appended with @2 for the second parallel build). There might be also plugins which result in a different path being used.

This should be replaced with

sh 'cp -f src/environments/environment.ts src/environments/environment.prod.ts'

The sh step will always be executed in the current directory, which is the jobs workspace when you don’t use the dir step.

So I did some more research on this and you’re right Docker is not on the Jenkins Controller node. It would be an easy fix to simply install Docker but I am curious if I can instead leverage existing Jenkins Nodes I already have setup to run the container and test inside.

After a lot of reading and poking I have kind of seemed to settle on the need to use a scripted pipeline setup as that will allow me to use the:

docker.image(node:latest).inside {
   [...]
}

As the ultimate goal is still to:

  1. Begin the job
  2. From a connected Jenkins Node (with docker installed) start an ephemeral container, probably node:latest
  3. Pull down all source code from git
  4. Run cypress tests
  5. Have the results show up in the build log
  6. If they pass, destroy the testing container
  7. On the main Jenkins Node (from step 2) now pull down the code from git
  8. Run the Docker build
  9. Run Docker Compose and have the tested application live on this Docker Host.

Is there a good doc for beginning with scripted pipelines as the information seems really scattered and difficult to piece together.