0

My main workflow mainjob1 calls a reusable workflow reusable_workflow_job and then proceeds to execute its mainjob2

Workflow level variable XML_PATH is passed to and used by reusable workflow which generates a variable $returnvalue" i.e. app_name=myapp1 by calling a powershell getXMLkey-value.ps1 and is set to GITHUB_OUTPUT using echo "$returnvalue" >> $env:GITHUB_OUTPUT.

Inside the reusable workflow the App Name in reusable workflow: ${{ steps.run-scriptsx.outputs.app_name }}" is printed successfully as App Name in reusable workflow: myapp1

The flow then returns to main workflow mainjob2 where I wish to now print the same app_name i.e myapp1 but it does not print anything here: echo "App name in mainjob2: ${{ needs.reusuable_workflow_job.outputs.app_name }}"

How can I print app_name as myapp1 in main workflow's mainjob2?


Below are my workflow files:

Main workflow file:

    env:
      XML_PATH: "\\\\remotehost7\\$D\\Logs\\test.xml"
    on:
      push: main
    jobs:
      mainjob1:
        runs-on: "mylocal_windows"
        outputs:
          userxmlpath: ${{ steps.step1.outputs.userxmlpath }}
        steps:
          - name: Print variable
            id: step1
            run: |
              echo "user XML path: ${{ env.XML_PATH }}"
              echo "userxmlpath=${{ env.XML_PATH }}">> $env:GITHUB_OUTPUT
    
      reusuable_workflow_job:
        needs:
          - mainjob1
        uses: knowyrtech/betech/.github/workflows/getvalue.yml@mm365
        with:
          xml_Path: "${{ needs.mainjob1.outputs.userxmlpath }}"
        secrets: inherit
    
      mainjob2:
        needs: 
          - reusuable_workflow_job
        runs-on: "mylocal_windows"
        steps:
          - name: Back to main workflow
            run: |
              echo "App name in mainjob2: ${{ needs.reusuable_workflow_job.outputs.app_name }}"

Reusable workflow:

    name: Read XML
    on:
      workflow_call:
        inputs:
          xml_Path:
            required: true
            default: '\\remotehost5\$D\Logs\test.xml'
    jobs:
      Extract-keys:
        runs-on: "mylocal_windows"
        steps:
          - name: Checkout reusable repo
            uses: actions/checkout@v3
            with:
              repository: knowyrtech/betech
              ref: mm365
              path: "${{ github.workspace }}/betech"
              token: ${{ secrets.my_automated_token }}
    
          - name: Call powershell
            id: run-scriptsx
            run: | 
              set-location "${{ github.workspace}}\betech\scripts"
              $returnvalue=& "./getXMLkey-value.ps1"
              echo "$returnvalue"
              echo "$returnvalue" >> $env:GITHUB_OUTPUT
             
          - name: Inside reusable workflow
            run: |
              echo "App Name in reusable workflow: ${{ steps.run-scriptsx.outputs.app_name }}"
GuiFalourd
  • 15,523
  • 8
  • 44
  • 71
Ashar
  • 2,942
  • 10
  • 58
  • 122
  • @Azeem i get error `You cant use outputs with uses` when job level outputs for app_name under `reusuable_workflow_job` – Ashar May 22 '23 at 23:31

1 Answers1

1

According to the official documentation, you need to add the outputs configuration to your Read XML reusable workflow, in the Extract-keys job, as well as at the workflow_call level:

on:
  workflow_call:
    # Map the workflow outputs to job outputs
    outputs:
      app_name:
        description: "app name description"
        value: ${{ jobs.Extract-keys.outputs.app_name }}  

jobs:
  Extract-keys:
    runs-on: "mylocal_windows"
    outputs:  # HERE
      app_name: ${{ steps.run-scriptsx.outputs.returnvalue }}
    steps:
       
       [ ... ]

       - name: Call powershell
         id: run-scriptsx
         run: | 
           set-location "${{ github.workspace}}\betech\scripts"
           $returnvalue=& "./getXMLkey-value.ps1"
           echo "$returnvalue"
           echo "$returnvalue" >> $env:GITHUB_OUTPUT

Otherwise, you won't be able to access the output value as expected in your mainjob2:

jobs:
  [ ... ] 
  mainjob2:
    needs: 
      - reusuable_workflow_job
    runs-on: "mylocal_windows"
    steps:
      - name: Back to main workflow
        run: |
          echo "App name in mainjob2: ${{ needs.reusable_workflow_job.outputs.app_name }}"

I've made an example here:

Observation: This other thread is similar and might give some insights as well)

GuiFalourd
  • 15,523
  • 8
  • 44
  • 71
  • are you sure output is `app_name: ${{ steps.run-scriptsx.outputs.returnvalue }}` and not `${{ steps.run-scriptsx.outputs.app_name }}` ? I tried `app_name: {{ steps.run-scriptsx.outputs.app_name }}` but `echo "App name in mainjob2: ${{ needs.reusable_workflow_job.outputs.app_name }}"` still does not print anything. – Ashar May 22 '23 at 22:12
  • the solution does not work. `mainjob2` needs `reusuable_workflow_job` whereas `outputs: ` suggested by you has to be deployed to ` Extract-keys:` Thats the reason `${{ needs.reusable_workflow_job.outputs.app_name }}` does not print anything. – Ashar May 22 '23 at 22:52
  • There is contradictions in the answers by @GuiFalourd and @Azeem with regards to job level `outputs` for `app_name` be under `reusuable_workflow_job` or `Extract-keys`. Also, the solution does not work and does not print anything for `echo "App name in mainjob2: ${{ needs.reusable_workflow_job.outputs.app_name }}"`. I updated the original post with job workflow GUI – Ashar May 22 '23 at 23:30
  • @Ashar: I just reviewed [Using outputs from a reusable workflow](https://docs.github.com/en/actions/using-workflows/reusing-workflows#using-outputs-from-a-reusable-workflow) and seems like the `outputs` in the caller workflow is not needed (supported) so this answer seems to doing the right thing as per the example in docs. Now, the issue is with your `returnvalue` where you're returning `app_name=" and not `` only. Looks like that is the issue here. You may try returning only the value from the called workflow. – Azeem May 23 '23 at 05:17
  • @Azeem i setup a sample demo on public repo here and it does not work. runner "moh_win" is a self-hosted runner. https://github.com/knowyrtech/getvarfromreusuable/tree/main/.github/workflows – Ashar May 23 '23 at 07:13
  • @Azeem would you be able to check my public repo or do u want me to post afresh with the same i created? – Ashar May 23 '23 at 07:19
  • @Azeem I got the fix. The proposed answer is incomplete. There should to be 2 outputs: one above the job inline workflows input and second under the job as in the answer. Only then the calling workflow gets the value – Ashar May 23 '23 at 08:49
  • @Ashar: Good to hear that. Could you please share the link to both of these? Better yet, post an answer to this highlighting these two points. That'll be helpful for others. Thanks! – Azeem May 23 '23 at 09:26
  • @Azeem sure will do it tomorrow. – Ashar May 23 '23 at 09:49
  • I had indeed forgotten the `workflow_call` **outputs** configurations in the reusable workflow. I've also added a workflow example to the answer. – GuiFalourd May 23 '23 at 11:19