【问题标题】:Run Python file in Jenkins on different OS在不同操作系统上的 Jenkins 中运行 Python 文件
【发布时间】:2019-03-01 14:48:50
【问题描述】:
我在 Windows 上设置了 Jenkins master,在 Linux 上设置了 Slave。我正在尝试运行一个 python 文件(我在两台机器上本地加载)。
所以我一直在使用“执行 Windows 批处理命令”,但对于 Linux 从站我不确定。有什么方法可以在图片中引入“Execute Shell”,以便作业可以根据它决定运行的操作系统来决定运行什么?
或者有没有更有效的方法在 Jenkins 的两个操作系统上运行 python 文件?请指教。
【问题讨论】:
标签:
python
linux
windows
jenkins
master-slave
【解决方案1】:
您可以在 JenkinsFile 中创建一个函数,以便在 Jenkins Pipelines 中使用它:
/* A Python command which can work identically on Windows and Linux. */
def python(script_and_args) {
if (isUnix()) {
sh 'python3 ' + script_and_args
}
else {
bat 'python ' + script_and_args
}
}
那么你可以在JenkinsFile的其他地方使用该函数,如下所示:
stage('Script-based stage') {
steps {
dir(path: 'my_working_directory') {
python('my_script.py --arguments')
}
}
}
我是 Jenkins 的新手,但这种方法对我很有效。
【解决方案2】:
如果您使用管道作业,这可能是您可以采取的一种方法。这还假设您已将两个代理标记为python
node('python') {
stage 'Run Python' {
if (isUnix()) {
sh 'python mypythonscript.py'
}
else {
bat 'python mypythonscript.py'
}
}
}