ADO YAML 管道 |如何根据另一个变量的值从变量组中获取秘密变量

问题描述 投票:0回答:1

在我的项目中,我可以像这样获取变量组中定义的变量的值,没有问题

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
)?

azure-devops azure-pipelines azure-pipelines-yaml
1个回答
0
投票

我使用 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 }})
© www.soinside.com 2019 - 2024. All rights reserved.