Forked from stephendonner/gist:2a5e5a140e4448b8de18f656b12bf00a
Last active
February 9, 2017 01:26
-
-
Save bitwiseman/3c01d3c6d61490c731786f28f5f4e783 to your computer and use it in GitHub Desktop.
This file contains 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
import groovy.json.JsonOutput | |
/** Tox environment */ | |
def config = 'e2e-tests/tox.ini' | |
def environment = 'tests' | |
/** Map of desired capabilities */ | |
def capabilities = [ | |
browserName: 'Firefox', | |
version: '47.0', | |
platform: 'Windows 10' | |
] | |
/** Write capabilities to JSON file | |
* | |
* @param desiredCapabilities capabilities to include in the file | |
*/ | |
def writeCapabilities(desiredCapabilities) { | |
def defaultCapabilities = [ | |
build: env.BUILD_TAG, | |
public: 'public restricted' | |
] | |
def capabilities = defaultCapabilities.clone() | |
capabilities.putAll(desiredCapabilities) | |
def json = JsonOutput.toJson([capabilities: capabilities]) | |
writeFile file: 'e2e-tests/capabilities.json', text: json | |
} | |
/** Run Tox | |
* | |
* @param environment test environment to run | |
*/ | |
def runTox(config, environment) { | |
def options = env.PYTEST_ADDOPTS ?: '' | |
def processes = env.PYTEST_PROCESSES ?: 'auto' | |
try { | |
wrap([$class: 'AnsiColorBuildWrapper']) { | |
withCredentials([[ | |
$class: 'StringBinding', | |
credentialsId: 'SAUCELABS_API_KEY', | |
variable: 'SAUCELABS_API_KEY']]) { | |
withEnv(["PYTEST_ADDOPTS=${options} " + | |
"-n=${processes} " + | |
"--driver=SauceLabs " + | |
"--variables=capabilities.json " + | |
"--color=yes"]) { | |
sh "tox -c ${config} -e ${environment}" | |
} | |
} | |
} | |
} catch(err) { | |
currentBuild.result = 'FAILURE' | |
throw err | |
} finally { | |
stash name: environment, includes: 'e2e-tests/results/**' | |
} | |
} | |
/** Send a notice to #fxtest-alerts on irc.mozilla.org with the build result | |
* | |
* @param result outcome of build | |
*/ | |
def ircNotification(result) { | |
def nick = "fxtest${BUILD_NUMBER}" | |
def channel = '#fx-test-alerts' | |
result = result.toUpperCase() | |
def message = "Project ${JOB_NAME} build #${BUILD_NUMBER}: ${result}: ${BUILD_URL}" | |
node { | |
sh """ | |
( | |
echo NICK ${nick} | |
echo USER ${nick} 8 * : ${nick} | |
sleep 5 | |
echo "JOIN ${channel}" | |
echo "NOTICE ${channel} :${message}" | |
echo QUIT | |
) | openssl s_client -connect irc.mozilla.org:6697 | |
""" | |
} | |
} | |
stage('Checkout') { | |
node { | |
timestamps { | |
deleteDir() | |
checkout scm | |
stash name: 'workspace' | |
} | |
} | |
} | |
stage('Lint') { | |
node { | |
timestamps { | |
deleteDir() | |
unstash 'workspace' | |
sh "tox -c ${config} -e flake8" | |
} | |
} | |
} | |
try { | |
stage('Test') { | |
node { | |
timeout(time: 1, unit: 'HOURS') { | |
timestamps { | |
deleteDir() | |
unstash 'workspace' | |
try { | |
writeCapabilities(capabilities) | |
runTox(config, environment) | |
} catch(err) { | |
currentBuild.result = 'FAILURE' | |
throw err | |
} finally { | |
stash name: environment, includes: 'e2e-tests/results/**' | |
} | |
} | |
} | |
} | |
} | |
} catch(err) { | |
currentBuild.result = 'FAILURE' | |
ircNotification(currentBuild.result) | |
mail( | |
body: "${BUILD_URL}", | |
from: "[email protected]", | |
replyTo: "[email protected]", | |
subject: "Build failed in Jenkins: ${JOB_NAME} #${BUILD_NUMBER}", | |
to: "[email protected]") | |
throw err | |
} finally { | |
stage('Results') { | |
node { | |
deleteDir() | |
unstash environment | |
publishHTML(target: [ | |
allowMissing: false, | |
alwaysLinkToLastBuild: true, | |
keepAll: true, | |
reportDir: 'e2e-tests/results', | |
reportFiles: "${environment}.html", | |
reportName: 'HTML Report']) | |
junit 'e2e-tests/results/*.xml' | |
archiveArtifacts 'e2e-tests/results/*' | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment