我在 Windows 上设置了 Jenkins master,在 Linux 上设置了 Slave。我正在尝试运行一个 python 文件(我在本地两台机器上加载了该文件)。
因此,我一直在使用“执行 Windows 批处理命令”,但对于 Linux 从属设备,我不确定。有没有办法将“执行Shell”引入图片中,以便作业可以根据它决定运行的操作系统来决定运行什么?
或者是否有更有效的方法在 Jenkins 的两个操作系统上运行 python 文件?请指教。最佳答案
您可以在 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 的新手,但这种方法对我来说很有效。
关于python - 在不同操作系统上的 Jenkins 中运行 Python 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52507318/