通过Jenkins pipeline实现调用python脚本,Pipeline使用Pipeline script from SCM具体实现参考以下代码:
echo "enable_build_package: ${enable_build_package}"
pipeline{
agent { label "xxxxxxx" }
stages{
stage("Build Package"){
when {
expression {
if ( enable_build_package == "true" ){
echo "Build Package"
return true
}else{
echo "Skip Build Package"
return false
}
}
}
steps{
echo "Start Build Package"
}
}
stage("Upload Package to Nexus"){
when {
expression {
if ( enable_upload_nexus == "true" ){
echo "Upload Package to Nexus"
return true
}else{
echo "Skip Upload Package to Nexus"
return false
}
}
}
steps{
echo "Start Upload Package to Nexus"
}
}
stage("Running Tower Ansible for python script"){
steps{
withCredentials([usernamePassword(credentialsId: 'xxxxxxx', passwordVariable: 'password', usernameVariable: 'username')]) {
sh '/usr/local/bin/python2.7 --version'
sh "/usr/local/bin/pip2.7 list"
sh "whereis virtualenv"
sh "/usr/local/bin/virtualenv env"
sh "ls env/bin"
echo "${workspace}"
sh 'env/bin/pip install requests -i https://xxxxxxx:8084/nexus/repository/pypi/simple --cert cert/truststore-2.0.11.pem'
sh "env/bin/pip list"
sh "env/bin/python script/tower_api.py --user ${username} --password ${password} --template allen"
}
}
}
}
}