Aleksandar Ivanov
Aleksandar Ivanov

Reputation: 317

GitHub Actions: read from artifact and assign to variable?

Workflow artifact "tag.txt", created in the first job, has only this inside "1.0.0".

In the second job, the artifact is downloaded and unpacked, and I would like to read the "1.0.0" line of text from the "tag.txt" file and assign it to an env variable to use it for several actions inside the second job.

If that's possible, how is it done ? Is there much difference between powershell and bash implementations of this process ?

Upvotes: 1

Views: 1790

Answers (1)

Aleksandar Ivanov
Aleksandar Ivanov

Reputation: 317

Yes, it is perfectly possible and yes there is quite a bit of difference between Bash and Powershell in doing this task.

BASH variant:

  # write something in a file and create it
  - run: echo "value" >> new.txt

  # read from file and assign to outputs object
  - name: Read and assign
    id: output_test
    run: |
      input_file="new.txt"
      while read line
      do
        file_text=$line
      done < "$input_file"
      echo ::set-output name=new_value::$file_text

  # test to see if done correctly - should echo "value"
  - run: echo ${​​​​{​​​​​​​​​​​ steps.output_test.outputs.new_value }​​​​​​​​​​​}​​​​​​​​​​​​​​​​​​

Powershell variant:

  # write something in a file and create it
  - run: '"value" | Out-File -FilePath "new.txt"'

  # read from file and assign to outputs object
  - name: Read and assign
    id: output_test
    run: |
      $file_text = Get-Content "new.txt"
      echo "::set-output name=new_value::$file_text"

  # test to see if done correctly - should echo "value"
  - run: echo ${​​​​​​​​​{​​​​​​​​​ steps.output_test.outputs.new_value }​​​​​​​​​}​​​​​​​​​

*echo in Powershell is an alias for the Write-Output command

Upvotes: 3

Related Questions