无法使用 os.environ 在 Python 脚本中访问 Jenkins 环境变量

Can't access Jenkins environment variables in Python script with os.environ

我正在尝试在我的 Jenkins 作业中 运行 python 脚本,该脚本依赖于之前设置的 Jenkins 环境变量。环境变量在我的 Jenkinsfile 中设置,当我回显它们时它们就在那里。

但是当我尝试使用 os.environ["VARIABLE"].

访问这些变量时,我的 python 脚本失败了

这是我的 python 脚本中的设置方式:
svn_branch = os.environ["SVN_BRANCH"]

它失败并出现此错误,就好像找不到该变量一样:

  File "c:\jenkins\workspace\test_jenkins_build.py", line 68, in <module>
    svn_branch = os.environ["SVN_BRANCH"]
  File "C:\Users\build\AppData\Local\Programs\Python\Python39\lib\os.py", line 679, in __getitem__
    raise KeyError(key) from None
KeyError: 'SVN_BRANCH'

有没有办法让 python 脚本访问 Jenkins 环境变量?感谢您提供的所有帮助!

请看下面的例子:Jenkinsfile

stage('stage 1') {
              steps {
           script {
           dir ("C:\python-workspace\"){
               def result
               // Set your environment variable
               env.SVN_BRANCH= "app"
               result = bat label: 'Execute python script..', returnStatus: true, script: "hello.py "
              }
           }         
         }
   }

Python 脚本:

#!/usr/bin/python
import os
branch=os.environ['SVN_BRANCH']
print (branch)