Created
October 11, 2019 13:50
-
-
Save emilorol/a71ac96ee0c9a03e17834db34e89fb7f to your computer and use it in GitHub Desktop.
Selenium docker grid in Jenkins
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/groovy | |
| package com.company.devops; | |
| def withDockerNetwork(Closure inner) { | |
| try { | |
| networkId = UUID.randomUUID().toString() | |
| sh "docker network create ${networkId}" | |
| inner.call(networkId) | |
| } finally { | |
| sh "docker network rm ${networkId}" | |
| } | |
| } | |
| def execute() { | |
| node { | |
| properties([ | |
| parameters([ | |
| string(defaultValue: "[email protected]:qa/SeleniumFramework.git", description: 'Repository to Selenium tests', name: 'Repository'), | |
| string(defaultValue: "master", description: 'Branch to use for features to run', name: 'Branch'), | |
| choice(choices: ['qa1', 'qa2', 'qa3', 'qa4','staging', 'prod', 'preprod'], description: 'The branch or tag the test is being run in.', name: 'Domain'), | |
| choice(choices: ['Chrome', 'Firefox'], description: 'The branch or tag to compare against.', name: 'Browser'), | |
| string(defaultValue: "product_compare", description: 'Tags to test', name: 'Tags'), | |
| booleanParam(defaultValue: false, description: 'Debug maven command', name: 'debugMaven'), | |
| ]) | |
| ]) | |
| def nexus = 'nexus.internal.com:8443' | |
| def dockerHubImage = 'company/devops:selenium-hub' | |
| def runInBrowser = "$Browser".toString().toLowerCase() | |
| def dockerNodeImage = "company/devops:selenium-node-$runInBrowser" | |
| def runInDebugMode = ""; | |
| if (params.debugMaven) { runInDebugMode = "-X"; } | |
| def docker_common_args = "--ipc=host " + | |
| "-v /var/run/docker.sock:/var/run/docker.sock " + | |
| "--name selenium-node-$runInBrowser " | |
| def docker_hub_args = docker_common_args + | |
| "-e HOME=${env.WORKSPACE} " + | |
| "-v $HOME:/home/deploy/src/SeleniumFramework " + | |
| "--name selenium-hub " | |
| try { | |
| stage('Checkout') { | |
| cleanWs() | |
| sh "git clone $Repository -b $Branch ." | |
| sh "pwd" | |
| } | |
| stage('Build Grid') { | |
| docker.withRegistry("https://" + nexus) { | |
| docker.image(dockerHubImage).pull() | |
| docker.image(dockerNodeImage).pull() | |
| } | |
| } | |
| stage("Run Cursory Test") { | |
| withDockerNetwork { network -> | |
| docker.image(nexus + "/" + dockerNodeImage).withRun(docker_common_args + " --network ${network}") { container -> | |
| docker.image(nexus + "/" + dockerHubImage).inside(docker_hub_args + " --network ${network}") { | |
| sh "/usr/bin/java -jar /usr/lib/selenium/selenium-server-standalone.jar -role hub -hubConfig /opt/docker-files/config.json &" | |
| sh "mvn -version" | |
| sh "mvn -B -f ./pom.xml verify $runInDebugMode -Pparallel-cuke -Dtags=@$Tags -Denvironment=com-$Domain -Dgrid=http://selenium-hub:4444/wd/hub -Dbrowser=$Browser" | |
| } | |
| } | |
| } | |
| } | |
| } | |
| catch (exception) { | |
| echo "Failure: $exception" | |
| currentBuild.result = 'FAILURE' | |
| } | |
| finally { | |
| cleanWs() | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment