在 Jenkinsfile 管道 groovy 中获取 python 脚本的输出结果

Get the ouput result for a python script in a Jenkinsfile pipeline groovy

我正在尝试将 python 脚本的输出分配给变量。

这是我的设置

stage('Deployment on FTP') {
    steps {
        script {
            def warName = sh(script: 'python ../scripts/myPythonScriptHere.py', returnStdout: true)
        }
        ...
    }
}

这是我的配置(在 linux 服务器上):

ci/
    -- prod/
                -- Jenkinsfile.groovy
    -- scripts/
                -- myPythonScriptHere.py

Jenkins 提示无法打开文件“...”[Errno 2] 没有那个文件或目录

我该怎么做?

谢谢

您在这里所做的声明本身就是不正确的。在 sh(script: ....) 函数内部需要一个 shell 脚本。这里 pythons 的执行将不起作用。要获得 python 的输出,您必须编写一个 shell 脚本来为您完成这项工作。这意味着,您将必须在 sh(script:...) 中编写一个 shell 脚本,它将执行您的 python 脚本并将其输出捕获到一个变量中并 return 它。例如:

                  steps {
                    script {
                       def warName = sh(script: '#!bin/bash
                       var output = python ../scripts/myPythonScriptHere.py
                       return output', returnStdout: true)
        }
    }
}```
stage('Deployment on FTP') {
        steps {
            script {
                env.WAR_NAME = sh(script: "python scripts-ci/scripts/build/front/scripts/lib/war_name.py ${customerProfileId} ${FORCE_CUSTOMER_NAME}", returnStdout: true).toString().trim()
            }
        }
    }

这是我当前运行的 Jenkinsfile 的代码(脚本-ci 当我克隆了我需要的 python 脚本时,文件夹会响起。

        stage("Git checkout") {
          steps {
            dir("scripts-ci") {
                git branch: "${GIT_BRANCH_CI}",
                credentialsId: "${GIT_CREDENTIALS_ID}",
                url: "${GIT_URL_CI}"
            }
        }
    }