Jenkins pipeline using browser proxy in Docker container

def vividusNetwork = 'vividus-grid'(1)
def vividusHub = 'vividus-hub'(2)
def vividusBrowser = 'vividus-browser'(3)
def vividusProxyHost = 'vividus-proxy-host'(4)
def seleniumVersion = '4.5.0'(5)
def overridings = """
                        proxy.host=${vividusProxyHost}(4)
                        proxy.connectable-host=${vividusProxyHost}(4)
                        configuration.suites=web_app
                        configuration.profiles=web/docker,web/desktop/chrome
                        selenium.grid.url=http://${vividusHub}:4444/wd/hub(2)
                  """
def projectName = 'vividus-sample-tests'(6)
def scmCredentialId = 'vividus'(7)
def gitUrl = 'git@github.com:vividus-framework/vividus-sample-tests.git'(8)

pipeline {
  agent {
    label 'docker'
  }

  stages {
    stage('Start Selenium Grid') {
        steps {
           sh "docker network create ${vividusNetwork}"(1)
           sh "docker run -d --net ${vividusNetwork} --name ${vividusHub} selenium/hub:${seleniumVersion}"(2)
           sh "docker run -d --net ${vividusNetwork} -e SE_EVENT_BUS_HOST=${vividusHub} -e SE_EVENT_BUS_PUBLISH_PORT=4442 -e SE_EVENT_BUS_SUBSCRIBE_PORT=4443 -e SCREEN_WIDTH=1920 -e SCREEN_HEIGHT=1080 --shm-size=2g --name ${vividusBrowser} selenium/node-chrome:${seleniumVersion}"(3)(5)
        }
    }

    stage('Load Source') {
        steps {
            checkout([$class: 'GitSCM',
                branches: [[name: 'main']],
                doGenerateSubmoduleConfigurations: false,
                extensions: [
                    [$class: 'RelativeTargetDirectory', relativeTargetDir: projectName],(6)
                    [$class: 'SubmoduleOption',
                    disableSubmodules: false,
                    parentCredentials: true,
                    recursiveSubmodules: true,
                    reference: '',
                    trackingSubmodules: false]
                ],
                submoduleCfg: [],
                userRemoteConfigs: [[credentialsId: scmCredentialId, url: gitUrl]]])(7) (8)
        }
    }

    stage('Run Tests Using Docker') {
        steps {
                sh "docker ps -a"
                withDockerContainer(image: 'eclipse-temurin:17', args: "--net ${vividusNetwork} --name ${vividusProxyHost}") {(1) (4)
                dir(projectName) {(6)
                    sh """ (9)
                        echo -e "${overridings}" > overriding.properties
                        mv -f overriding.properties src/main/resources/overriding.properties
                        ./gradlew runStories
                     """
                    archiveArtifacts 'output/reports/**'
                }
            }
        }
    }
  }

  post {
        always {
            publishHTML([
                allowMissing: false,
                alwaysLinkToLastBuild: true,
                keepAll: true,
                reportDir: "${projectName}/output/reports/allure",(6)
                reportFiles: 'index.html',
                reportName: 'Allure Report',
                reportTitles: ''
            ])
            cleanWs()
            sh "docker rm -vf ${vividusBrowser} || true"
            sh "docker rm -vf ${vividusHub} || true"
            sh "docker network rm ${vividusNetwork} || true"
        }
    }
}
1 vividusNetwork - name of docker network
2 vividusHub - name of docker hub; this variable value is part of parameter for property selenium.grid.url
3 vividusBrowser - name of docker container with browser
4 vividusProxyHost - parameter, which is passed as name of docker container in dockerFingerprintRun for run tests; need to assign this variable as value for proxy.host and proxy.connectable-host properties
5 seleniumVersion - grid version
6 projectName - test project name
7 scmCredentialId - credentials for interacting with test repository
8 gitUrl - git url to test project
9 The tests are executed via gradle. The exit code of the command will be 0 - if all the tests are passed, 1 - if there is any error during tasks execution, or any of the tests not in passed status.
For more information about pipeline syntax and jenkins directives read jenkins documentation.