Forum Discussion

depreston's avatar
depreston
Copper Contributor
Apr 30, 2024

Get content from text file and save it into a variable - Azure YML

I have a txt file and I'm trying to save all the content in a variable but I'm only getting the first line of the content into the variable

 

greetings.txt

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

azure-pipeline.yml

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

# Save text file content 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"

Results after 'Another task' is executed:

Actual results:

- hello: 123

Expected results:

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

What I need to update so I can get the expected results?

1 Reply

  • How about this:

     

    variables:
      - name: MY_TEXT_FILE
        value: 'greetings.txt'
        readonly: true
    
      - 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 encoded=$(echo "$testcontent" | base64 -w 0)
            echo "##vso[task.setvariable variable=GREETINGS_CONTENT]$encoded"
    
      - task: Bash@3
        displayName: 'Decode and print variable'
        inputs:
          targetType: inline
          script: |
            decoded=$(echo "$GREETINGS_CONTENT" | base64 -d)
            echo "My greetings are: $decoded"

     

Resources