我想为某些 Azure DevOps 管道变量创建用户输入,并在 Python 脚本中获取这些变量。
下面是我的yml文件
parameters:
- name: Action123
displayName: 'Select Action'
type: string
default: 'enable'
values:
- 'enable'
- 'disable'
trigger: none
stages:
- stage: Create
pool:
name: sdasdasd
demands:
- agent.name -equals sdasdasd
jobs:
- job: BuildJob
steps:
- script: echo Building!
- task: Bash@3
inputs:
targetType: 'inline'
script: |
export Action123=$(Action123)
python job/job-action.py
- task: Bash@3
inputs:
targetType: 'inline'
script: |
echo 'Action123 Taken =========>>>' + $(Action123)
displayName: Summary for Dev Action Utility
这是我的Python脚本
import requests
import json
import os
Action123 = os.getenv('Action123')
print("Action123 ===========>>> " + Action123) #This is not getting printed
我不确定哪里出错了,尝试了多次,但变量Action123为空。
如果我这么做了,那就奇怪了
print(os.environ)
这是给予
{'otherdields & value,
'Action123': 'enable',
'otherdields & value}
哪里有错误请指正
谢谢
通过
os.getenv()
获取DevOps变量值到python脚本的方式是正确的。
由于您使用的是自托管代理池,并且变量 Action123 为空,因此您可以在 yaml 中显式定义它,以确保给出该值。
parameters:
- name: Action123
displayName: 'Select Action'
type: string
default: 'enable'
values:
- 'enable'
- 'disable'
variables:
- name: Action123 # define the variable to make sure it has the value.
value: ${{ parameters.Action123 }}
trigger: none
stages:
- stage: Create
jobs:
- job: BuildJob
steps:
- script: echo Building!
- task: Bash@3
inputs:
targetType: 'inline'
script: |
export Action123=$(Action123)
python job/job-action.py
- task: Bash@3
inputs:
targetType: 'inline'
script: |
echo 'Action123 Taken =========>>>' + $(Action123)
displayName: Summary for Dev Action Utility
我的输出: