python - 在不同操作系统上的 Jenkins 中运行 Python 文件
问题描述
我在 Windows 上设置了我的 Jenkins master,在 Linux 上设置了 Slave。我正在尝试运行一个 python 文件(我在两台机器上本地加载)。
因此,对于我一直在使用“执行 Windows 批处理命令”但对于 Linux 从站我不确定。有没有办法将“执行外壳”引入图片中,以便作业可以根据它决定运行的操作系统来决定运行什么?
或者有没有更有效的方法在 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 的新手,但这种方法对我来说效果很好。
推荐阅读
- ffmpeg - 有没有办法用 FFMPEG 处理不连续性?
- c++ - std::is_invocable<...> 检查成员函数
- php - SQL 从 id 中选择名字
- javascript - 如何在 Azure Maps 的地图视图边界中获取所有可见点
- python - 如何在 Python(plotly.express,px)中使用 Plotly Expess 仅显示图例的子集?
- c# - 如何从虚拟机连接 C# 和 MySQL?
- json - 无法从 JSON 中删除或拼接行
- java - RecycleView 和 CardView 项目、边距和居中
- database - Postgis,将点转换为线
- r - 如何在 ROCit 中计算 ROC?