Search code examples
githubgithub-actions

How can I use matrix as the reusable workflow file name?


I have a github action workflow file which is used to triggered other reusable workflows.

Below is the code. I'd like to map the reusable workflow files with the matrix name. But github gives me an error error parsing called workflow ".github/workflows/main.yml" -> "./.github/workflows/${{ matrix.app-name }}.yml" : failed to fetch workflow: workflow was not found..

Is it possible to use matrix to call workflow files?

jobs:
  run-pipelines:
    strategy:
      matrix:
        app-name: [proj1, proj2]
    uses: ./.github/workflows/${{ matrix.app-name }}.yml

Solution

  • This is not possible. Please take a look on this reply on github issue

    Do you have a finite amount of possible environments? If no, you can skip reading this.

    I would classify this as a workaround, like already mentioned expressions are forbidden in uses. Create a proxy workflow octocat/pipelines/.github/workflows/customer-deploy.yaml@main, which references each of the possible environments. You only need to update it if you modify inputs and outputs.

    on: workflow_call
    # TODO add some inputs if you really need them, you example inputs could be removed without loss of information.
    jobs:
      development:
        if: github.ref_name == 'development'
        uses: "octocat/pipelines/.github/workflows/customer-deploy.yaml@development"
        with:
          environment_name: ${{ github.ref_name }}
          repository_name: ${{ github.repository }}
          repository_ref_name: ${{ github.ref_name }}
          repository_commit_hash: ${{ github.sha }}
        secrets: inherit
      staging:
        if: github.ref_name == 'staging'
        uses: "octocat/pipelines/.github/workflows/customer-deploy.yaml@staging"
        with:
          environment_name: ${{ github.ref_name }}
          repository_name: ${{ github.repository }}
          repository_ref_name: ${{ github.ref_name }}
          repository_commit_hash: ${{ github.sha }}
        secrets: inherit
      production:
        if: github.ref_name == 'production'
        uses: "octocat/pipelines/.github/workflows/customer-deploy.yaml@production"
        with:
          environment_name: ${{ github.ref_name }}
          repository_name: ${{ github.repository }}
          repository_ref_name: ${{ github.ref_name }}
          repository_commit_hash: ${{ github.sha }}
        secrets: inherit
    

    and then consume it like

    name: customer-deploy
    
    on:
      push:
        branches:
          - development
          - staging
          - production
    
    jobs:
      deploy:
        uses: "octocat/pipelines/.github/workflows/customer-deploy.yaml@main"
        secrets: inherit