Automation

Using GitHub Actions Reusable Workflows with Matrix Strategies

Learn how to implement reusable workflows and matrix strategies in GitHub Actions efficiently.

4 min read

Learn how to streamline your CI/CD pipelines by leveraging GitHub Actions reusable workflows with matrix strategies. Reusable workflows simplify managing shared configurations, while matrix strategies allow multi-environment testing with a single configuration setup.

Prerequisites for GitHub Actions Reusable Workflows

To get started with reusable workflows in GitHub Actions, ensure you meet the following requirements:

prerequisites

  • A GitHub repository ready for workflow development.
  • Basic knowledge of GitHub Actions workflow syntax.
  • Access to the repository where the reusable workflow will be created or stored.

Setting Up a Reusable Workflow

Follow these steps to create a reusable workflow in your GitHub repository:

steps

  1. Create a Workflow File
    Navigate to the Actions tab in your repository and create a new workflow. Save it under .github/workflows with an appropriate file name like docker-build.yml.

  2. Define the Workflow Metadata
    Begin the YAML file with a name for the workflow and the trigger configuration:

    yaml
    name: Build and Publish Docker Image
    on:
      workflow_call:
        inputs:
          image_name:
            required: true
            type: string
          tag:
            required: false
            type: string
        secrets:
          docker_username:
            required: true
          docker_password:
            required: true
  3. Add Workflow Jobs and Steps
    Write the jobs and their steps. You can define multiple jobs as necessary:

    yaml
    jobs:
      build:
        runs-on: ubuntu-latest
        steps:
          - name: Checkout code
            uses: actions/checkout@v3
          
          - name: Log in to DockerHub
            run: echo "${{ secrets.docker_password }}" | docker login -u "${{ secrets.docker_username }}" --password-stdin
          
          - name: Build Docker image
            run: docker build -t ${{ inputs.image_name }}:${{ inputs.tag || 'latest' }} .
      publish:
        runs-on: ubuntu-latest
        steps:
          - name: Publish Docker image
            run: docker push ${{ inputs.image_name }}:${{ inputs.tag || 'latest' }}
  4. Commit and Push
    Save the file, commit your changes, and push them to your repository.


Using the Reusable Workflow in Another Repository

Here's how to incorporate a reusable workflow from one GitHub repository into another:

steps

  1. Modify the Caller Workflow
    In the repository that will use the reusable workflow, edit or create a new workflow file (e.g., .github/workflows/trigger.yml).

  2. Add a Job to Reference the Reusable Workflow
    Use the uses keyword to specify the reusable workflow's repository and file path:

    yaml
    jobs:
      docker:
        uses: username/reusable-workflow-repo/.github/workflows/docker-build.yml@main
        with:
          image_name: 'my-image'
          tag: 'v1.0.0'
        secrets:
          docker_username: ${{ secrets.DOCKER_USERNAME }}
          docker_password: ${{ secrets.DOCKER_PASSWORD }}
  3. Commit and Push Changes
    Save your changes, commit, and push the updated workflow file.

  4. Verify Execution in GitHub Actions
    Go to the Actions tab of the repository and check if the workflow runs as expected.


Limitations of Reusable Workflows

Reusable workflows in GitHub Actions have some limitations to consider:



FAQ

How do I use matrix strategies in a reusable workflow?

You can define matrix configurations within the reusable workflow job using the matrix keyword. Pass matrix combinations to the caller workflow by including with inputs that iterate over the matrix.

Can reusable workflows use private repositories?

Reusable workflows can only be accessed from private repositories if both the reusable workflow and calling workflows reside in the same repository. Otherwise, they must be stored in public or accessible internal repositories.

Are environment variables shared between workflows?

No, environment variables set in the caller workflow are not automatically passed to the reusable workflow. Explicitly pass them using the with or secrets configuration.


Official reference: GitHub Actions documentation.