githubgithub-actions

GitHub Actions: How to call a reusable workflow as a step?


I have a main GitHub Actions workflow that is triggered via the workflow_dispatch event, and a reusable workflow that is triggered via the workflow_call event.

Is it possible to run a reusable workflow via a step in your main workflow and not as a job?

Main workflow:

name: main-workflow
on:
  workflow_dispatch:
    inputs:
      message:
       type: string
       description: 'A message to pass to reusable workflow'

jobs:
  use-reusable-workflow:
    runs-on: ubuntu-latest
    steps:
      - name: Call reusable workflow
        uses: my-org/reusable-workflow@main
        with:
          workflow: reusable-workflow.yml
          inputs: |
            message=${{ github.event.inputs.message }}
          secrets: |
            A_SECRET: ${{ secrets.A_SECRET }}

When running the main workflow I am getting the following error:

Error: Can't find 'action.yml', 'action.yaml' or 'Dockerfile' under


Solution

  • According to the GitHub official documentation:

    You call a reusable workflow by using the uses keyword. Unlike when you are using actions within a workflow, you call reusable workflows directly within a job, and not from within job steps.

    Therefore, you can't call a reusable workflow from a job step.


    A workaround in your case could be using a local action, which basically allows you to use an action in the same repository as the workflow.

    Example of a local action call:

    jobs:
      my_first_job:
        steps:
          - name: Check out repository
            uses: actions/checkout@v3
          - name: Use local my-action
            uses: ./.github/actions/my-action
    

    Note that you need to inform the path to the directory that contains the action in your workflow's repository. Therefore, to access the action file, you must check out your repository before using the action with the actions/checkout.

    This local action could be a composite action, similar to what a reusable workflow could achieve using actions, scripts or shell commands. I recommend this article to understand the difference between composite actions and reusable workflows.