如何在主工作流中获取可重用工作流的输出变量。

huangapple go评论57阅读模式
英文:

How to get reusable workflow output variables in main workflow

问题

My main workflow mainjob1调用一个可重用的工作流reusable_workflow_job,然后继续执行它的mainjob2

工作流级别的变量XML_PATH传递给并被可重用的工作流使用,该工作流通过调用Powershell脚本getXMLkey-value.ps1生成一个变量$returnvalue,即app_name=myapp1,然后使用echo "$returnvalue" >> $env:GITHUB_OUTPUT将其设置为GITHUB_OUTPUT

在可重用的工作流中,成功打印了App Name in reusable workflow: myapp1,如下所示:

App Name in reusable workflow: myapp1

然后,流程返回到主工作流mainjob2,在这里我希望现在打印相同的app_name,即myapp1,但是在这里不会打印任何内容:

echo "App name in mainjob2: ${{ needs.reusuable_workflow_job.outputs.app_name }}"

如何在主工作流的mainjob2中打印app_namemyapp1

英文:

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 }}"

答案1

得分: 1

根据官方文档,您需要将 outputs 配置添加到您的 Read XML 可重用工作流程中的 Extract-keys 作业,以及在 workflow_call 级别:

on:
  workflow_call:
    # 将工作流程的输出映射到作业的输出
    outputs:
      app_name:
        description: "应用程序名称描述"
        value: ${{ jobs.Extract-keys.outputs.app_name }}
      
jobs:
  Extract-keys:
    runs-on: "mylocal_windows"
    outputs:  # 在这里添加
      app_name: ${{ steps.run-scriptsx.outputs.returnvalue }}
    steps:
       
       [ ... ]

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

否则,您将无法在 mainjob2 中按预期访问输出值:

jobs:
  [ ... ] 
  mainjob2:
    needs: 
      - reusuable_workflow_job
    runs-on: "mylocal_windows"
    steps:
      - name: 返回主工作流程
        run: |
                    echo "主job2中的应用程序名称: ${{ needs.reusable_workflow_job.outputs.app_name }}"

我在这里创建了一个示例:

注意:这个其他线程 类似,并可能提供一些额外的见解。

英文:

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)

huangapple
  • 本文由 发表于 2023年5月23日 00:42:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76308317.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定