Created
November 12, 2019 09:03
-
-
Save dzerrenner/644e5f1f2c91e1625b2ab0a18032ab41 to your computer and use it in GitHub Desktop.
Jenkins: execute pytest on remote machines
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
library 'openstack' | |
def target_ip | |
def vm_id | |
pipeline { | |
agent any | |
parameters { | |
choice(choices: "IntegEnv\ProdEnv\TestEnv", description: "Environment", name: 'TESTENV') | |
} | |
stages { | |
stage ('Start Virtual Machine') { | |
steps { | |
script { | |
vm_id = openstack.getVMIdByName("testautomat-${params.TESTENV}") | |
target_ip = openstack.getVMIpById(vm_id) | |
} | |
echo "waiting for ${target_ip} to be up" | |
timeout(5) { //minutes | |
waitUntil { | |
script { | |
def r = sh script: "nc -zv ${target_ip} 22", returnStatus: true | |
return (r == 0); | |
} | |
} | |
} | |
} | |
} | |
stage('Prepare Test Runner') { | |
steps { | |
sh "ssh -i /home/jenkins/myKey jenkins@${target_ip} \"rm -rf /opt/smoketest && cd /opt && mkdir smoketest && git clone ssh://jenkins/home/git/gitea-repositories/smoketest.git\"" | |
sh "ssh -i /home/jenkins/myKey jenkins@${target_ip} \"cd /opt/smoketest && pip install -r requirements.txt\"" | |
} | |
} | |
stage('Run Test') { | |
steps { | |
sh "ssh -i /home/jenkins/myKey jenkins@${target_ip} \"/opt/smoketest/run_test.sh ${params.TESTENV}\"" | |
} | |
} | |
stage('Stop VM') { | |
steps { | |
echo "Stopping VM" | |
script { | |
openstack.stopVM(vm_id) | |
} | |
} | |
} | |
} | |
post { | |
failure { | |
mail to: 's166205@hft-leipzig.de, stakeholder@company.com', | |
subject: "Failed Pipeline: ${currentBuild.fullDisplayName}", | |
body: "Something is wrong with ${env.BUILD_URL}" | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment