Ashar
Ashar

Reputation: 3065

How to pass dynamically populated job level environment variables between jobs - Github Actions

It is known how to pass a known variable from one job to another as shown in the answer here:

How to pass job level environment variable between jobs - Github Actions

But, my case is different. In my case the variable echo "var1=good" >> $env:GITHUB_ENV is not known. Instead, a list of key=value is constructed dynamically from a variable and appended to GITHUB_OUTPUT and GITHUB_ENV as shown in the example below:

name: Main Workflow

on:
  push:
    branches:
      - main

jobs:
  mainjob1:
    runs-on: ubuntu-latest
    outputs:
      ---> don't know how to list dynamic key-values from the ForEach loop
    steps:

#      - name: Set Single known Variable
#        id: split
#        run: |
#          echo "var1=good" >> $GITHUB_OUTPUT

      - name: Set Dynamic Variable list using ForEach loop
        run: |
          "${{ needs.READ_VARS_FROM_XML.outputs.key_value_x }}" -split '[\r\n]+' | ForEach-Object {
              $trimvalue = $_.Trim()
              echo "$trimvalue" >>  $env:GITHUB_OUTPUT
              echo "$trimvalue" >>  $env:GITHUB_ENV
            }
        id: split

      - name: Print sample single key-value from the dynamic variable list
        run: |
          echo "PORT NUMBER: ${{ env.port }} or ${{ steps.split.output.port }}" 

  mainjob2:
    needs: mainjob1
    runs-on: ubuntu-latest

    steps:

      - name: Access Variable
        run: |
          ---> Dont know how to print dynamic populated key-values here
#          echo "var1=${{ needs.mainjob1.outputs.var1 }}"

How do I access this dynamic key=value list in another job as the outputs: at job level is not known?

Upvotes: 0

Views: 792

Answers (0)

Related Questions