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
Create a Workflow File
Navigate to theActionstab in your repository and create a new workflow. Save it under.github/workflowswith an appropriate file name likedocker-build.yml.Define the Workflow Metadata
Begin the YAML file with a name for the workflow and the trigger configuration:yamlname: 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: trueAdd Workflow Jobs and Steps
Write the jobs and their steps. You can define multiple jobs as necessary:yamljobs: 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' }}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
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).Add a Job to Reference the Reusable Workflow
Use theuseskeyword to specify the reusable workflow's repository and file path:yamljobs: 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 }}Commit and Push Changes
Save your changes, commit, and push the updated workflow file.Verify Execution in GitHub Actions
Go to theActionstab 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.