githubcontinuous-integrationgithub-actionscicdbuilding-github-actions

Github reusable workflows: use parameterized "uses"


I am trying to build my reusable workflow in such a way anyone in my team can hook up with it, pass the variable that it needs, and the workflow decides what to do according to input received. The problem I am having comes when within such workflow, I want to call another one by using a received input. To go into details, my workflow receives as input:

inputs:
  service:
    description: 'The name of the service'
    required: false
    type: string

within one of its job I am trying to do the following:

  call-next-workflow:
    name: Run Test 
    uses: "./.github/workflows/${{inputs.service}}.yml"

unfortunately github complains saying:

"./.github/workflows/${{inputs.service}}.yml" : failed to fetch workflow: workflow was not found.

Is there a way to use my parameter into the uses part, needed to call the next workflow?


Solution

  • The problem is that the uses path must be static and cannot be dynamic.

    As a workaround, you could create a proxy workflow, which references each of the possible services.

    on: workflow_call
    inputs:
      service:
        required: true
        type: string
    
    jobs:
      service_a:
        if: inputs.service == 'service A'
        uses: ".github/workflows/service_a.yaml@main"
      service_b:
        if: inputs.service == 'service B'
        uses: ".github/workflows/service_b.yaml@main"
      service_c:
        if: inputs.service == 'service C'
        uses: ".github/workflows/service_c.yaml@main"
    

    I appreciate that this does not have the same level of flexibility as a dynamic uses path.