在我的项目中,我可以像这样获取变量组中定义的变量的值,没有问题
steps:
- checkout: none
- bash: |
echo $(shared-smtp-user)
但是,在代码的后面,我需要将变量组中一些变量的值放入文件中,我做了这样的事情
parameters:
# The targetVariables to put inside secrets/, for example
# targetVariables:
# - shared-azure-devops-pat
# - shared-ldap-password
# - shared-ldap-user
# - shared-smtp-user
- name: targetVariables
type: object
jobs:
- job: echo_secret_files
displayName: 🗣️ Echo secret files
steps:
- checkout: none
- bash: |
echo "Echoing targetVariables into secrets/"
echo "using targetVariables ${{ convertToJson(parameters.targetVariables) }}"
mkdir -p ./secrets/
echo $(shared-smtp-user) # THIS WORKS !!!
displayName: Display information about the secrets being used
- ${{ each variableName in parameters.targetVariables }}:
- bash: |
echo $SECRET_VALUE > ./secrets/${variableName}.txt
env:
SECRET_VALUE: "$( ${{ variableName }} )" # THIS IS NOT WORKING!!!
displayName: Generate file for ${{ variableName }}
- bash: tree secrets/ -a
displayName: Display the generated secrets
出于某种奇怪的原因,我无法使用此语法向
SECRET_VALUE
赋值 "$( ${{ variableName }} )"
我想要做什么,对于
parameters.targetVariables
内的每个项目,将SECRET_VALUE
的值设置为$(variableName)
,然后将$SECRET_VALUE
回显到文件中。
有什么办法可以做到
$(myVariableNameHere)
,但没有实际上硬编码myVariableNameHere
,而是使用each
循环中的变量(称为variableName
)?
我使用 Linux 构建代理运行了一些测试。
而不是在
()
之间添加空格:
SECRET_VALUE: "$( ${{ variableName }} )"
尝试:
SECRET_VALUE: $(${{ variableName }})
trigger: none
pool:
vmImage: 'ubuntu-latest'
parameters:
- name: variables
displayName: Name of variables
type: object
default:
- foo
variables:
- name: foo
value: Foo Value
jobs:
- job: A
displayName: Job A
steps:
- checkout: none
- ${{ each variable in parameters.variables }}:
- script: echo "${{ variable }}=$CURRENT_VAR" # Output: foo=$( foo )
displayName: 'Echo ${{ variable }} with spaces'
env:
CURRENT_VAR: $( ${{ variable }} )
- script: echo "${{ variable }}=$CURRENT_VAR" # Output: foo=Foo Value
displayName: 'Echo ${{ variable }} WITHOUT spaces'
env:
CURRENT_VAR: $(${{ variable }})