从文本文件获取内容并将其保存到变量中 - Azure YML

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

我有一个txt文件,我试图将所有内容保存在变量中,但我只将内容的第一行放入变量中

问候语.txt

- hello: 123 
- hello: 456
- hello: 789 

azure-pipeline.yml

variables:
- name: MY_TEXT_FILE
  value: 'greetings.txt'
  readonly: true

# Save text file in this variable
- name: GREETINGS_CONTENT
  value: ''

   steps:
    - task: Bash@3
      displayName: 'Save text file content in a variable'
      inputs:
        targetType: 'inline'
        script: |
          echo "##vso[task.setvariable variable=GREETINGS_CONTENT]$(cat $MY_TEXT_FILE)"

    - task: Bash@3
      displayName: 'Another task'
      inputs:
        targetType: inline
        script: |
          # This is only printing the first line of the txt file
          # I want to save ALL the txt file content in the variable
          echo "My greetings are: $GREETINGS_CONTENT"

执行'另一个任务'后的结果:

实际结果:

- hello: 123

预期结果:

- hello: 123 
- hello: 456
- hello: 789

我需要更新什么才能获得预期的结果?

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

DevOps 中

multi-line variables
有一些限制,请检查此主题的用户语音票证:构建和发布中的多行变量

为了解决这个问题,我们可以将值转换为base64类型。然后在下一个任务中解码变量值。

variables:
- name: MY_TEXT_FILE
  value: 'greetings.txt'
  readonly: true

# Save text file in this variable
- name: GREETINGS_CONTENT
  value: ''

steps:
- task: Bash@3
  displayName: 'Save text file content in a variable'
  inputs:
    targetType: 'inline'
    script: |
      testcontent=$(cat $MY_TEXT_FILE)
      export test=$(echo "$testcontent"| base64 -w 0)
      echo "##vso[task.setvariable variable=GREETINGS_CONTENT]$test"

- task: Bash@3
  displayName: 'Another task'
  inputs:
    targetType: inline
    script: |
      # This is only printing the first line of the txt file
      # I want to save ALL the txt file content in the variable
      export content=$(echo "$(GREETINGS_CONTENT)" | base64 -d -w 0)
      echo $content

enter image description here

请查看类似门票以供参考。

© www.soinside.com 2019 - 2024. All rights reserved.