首页 > 解决方案 > 在不同操作系统上的 Jenkins 中运行 Python 文件

问题描述

我在 Windows 上设置了我的 Jenkins master,在 Linux 上设置了 Slave。我正在尝试运行一个 python 文件(我在两台机器上本地加载)。

因此,对于我一直在使用“执行 Windows 批处理命令”但对于 Linux 从站我不确定。有没有办法将“执行外壳”引入图片中,以便作业可以根据它决定运行的操作系统来决定运行什么?

或者有没有更有效的方法在 Jenkins 的两个操作系统上运行 python 文件?请指教。

标签: pythonlinuxwindowsjenkinsmaster-slave

解决方案


您可以在您的中创建一个函数,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 的新手,但这种方法对我来说效果很好。


推荐阅读