首页 > 解决方案 > 在 Jenkinsfile 管道 groovy 中获取 python 脚本的输出结果

问题描述

我正在尝试将 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

詹金斯告诉无法打开文件“...” [Errno 2] 没有这样的文件或目录

我怎样才能做到这一点 ?

谢谢

标签: pythonjenkinsjenkins-pipelinesh

解决方案


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 工作的代码(当我克隆所需的 python 脚本时,scripts-ci 在文件夹中搜索。

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

推荐阅读