Jenkins流水线:运行Python脚本

4

我想通过JenkinsFile运行一个Python文件或一组Python命令。我该如何处理?

我运行下面的代码,但我的Jenkins任务从未完成。

pipeline {
    agent { docker { image 'python:3.5.1' } }
    stages {
        stage('build') {
            steps {
                sh 'python --version'
            }
        }
    }
}

由于您正在使用Jenkins的“Hello World”示例之一(https://jenkins.io/doc/pipeline/tour/hello-world/),因此您的代码应该可以正常工作。您的Jenkinsfile是否放置在您的存储库中?或者您能否提供更多的Jenkins输出? - Michael Kemmerzell
是的,我正在使用与Windows机器中提到的相同的Jenkins示例。原因:java.io.IOException: 无法运行程序“nohup”(在目录“D:\ OCIPlatformIDC \ jenkins \ workspace \ githubpipeline”中):CreateProcess错误=2,系统找不到指定的文件。 - Pritish
你的Docker主机在哪里?是和Jenkins运行在同一台Windows机器上吗?并且你是否已经在Jenkins全局配置中进行了配置? - SunilThorat
作业的控制台日志是什么? - Matt Schuchard
1个回答

1
我通过以下步骤使你的流水线工作起来:
  1. 创建一个包含你在问题中发布的代码的 Pipeline 工作。
pipeline {
    agent { docker { image 'python:3.5.1' } }
    stages {
        stage('build') {
            steps {
                sh 'python --version'
            }
        }
    }
}

enter image description here

在虚拟机控制台上安装Docker并将Jenkins用户添加到Docker组中。
sudo apt install docker.io
sudo systemctl enable docker
sudo systemctl start docker
docker --version
> Docker version 18.09.2, build 6247962
sudo usermod -a -G docker jenkins
  1. 运行作业,您必须获得以下截图中的成功Python版本

enter image description here

如有需要,请随意提问。


Jenkins已安装在Windows上,同时也安装了Docker。那么我是否需要将Jenkins用户添加到docker组中?我在Jenkins构建过程中遇到以下错误: Caused: java.io.IOException: Cannot run program "nohup" (in directory "D:\OCIPlatformIDC\jenkins\workspace\githubpipeline"): CreateProcess error=2, The system cannot find the file specified. - Pritish
请看这里:https://dev59.com/hFcO5IYBdhLWcg3w3VEc - Mo Ziauddin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接