从 Github 回购中检出 jenkinsfile 时,jenkinsfile call/start 是否可以来自同一个回购的 python 脚本?

When checking out a jenkinsfile from a Github repo, can the jenkinsfile call/start a python script from that same Repo?

我正在从 SCM 启动 Jenkins 管道脚本,Jenkins 能够读取 jenkinsfile 但找不到该存储库中的其余文件。这是否需要以不同的方式完成,甚至可能?

Github 回购:

-jenkinsfile
-python_scripts/generate_json.py
node ('windows7') {
    stage('Generate json file'){
        bat 'dir'
        bat 'dir ..'
        bat 'python ./python_scripts/generate_json.py'
    }
}

无法找到 python 脚本:

J:\Jenkins_Slave\workspace\[redacted]\test_pipeline>python ./python_scripts/generate_json.py 
python: can't open file './python_scripts/generate_json.py': [Errno 2] No such file or directory

我也禁用了轻量级结帐,但这没有什么区别

如果这是您的整个管道,您实际上错过了检查源代码的步骤:

node ('windows7') {
    stage('Generate json file'){
        checkout scm
        bat 'dir'
        bat 'dir ..'
        bat 'python ./python_scripts/generate_json.py'
    }
}

实际上,我想这就是 dir 电话应该已经向您展示的内容,对吧?

您需要添加一个步骤来检查您正在 运行构建的节点上的源代码。读取 Jenkinsfile 的初始签出在主节点上,如果切换到其他节点,则该工作区中没有文件。 将此添加到您的 node-Block.

    stage('Checkout sources') {
        checkout scm // Checks out the repo where the Jenkinsfile is located
    }

扩展它:当 jenkins 管道读取 Jenkinsfile 时,它​​首先在 jenkins master 上检查它,评估要做什么和在哪里,即它将读取 node('Windows7') 并尝试获取一个在标签为 Windows7 的节点上释放执行器。然后它将执行切换到该节点上的 运行。

在您的情况下,它检查您的 git 存储库,并切换到具有空工作空间的 windows 节点,因为初始检查是在主节点上进行的。这只是为了读取 Jenkinsfile,所以请检查轻量级,它使用较少的资源。