Github Actions by Example
  • GitHub Actions by Example
  • chap1-intro
    • Section 1 - Introducing GitHub Actions
    • Section 2 - Terminology and Core Concepts
    • Section 3 - Building a workflow
  • chap2-deployment-workflow
    • Section 1 - Workflow
    • Section 2 - The services
    • Section 3 - The test workflow job
    • Section 4 - The build and push workflow job
    • Section 5 - The deploy workflow job
  • Chap3-reusable-workflows
    • Section 1 - What is a reusable workflow
    • Section 2 - The test reusable workflow
    • Section 3 - The build and push reusable workflow
    • Section 4 - The deploy reusable workflow
    • Section 5 - Releasing the reusable workflow
  • Chap4-custom-actions
    • Section 1 - What is a custom action
    • Section 2 - Composite Custom Actions
    • Section 3 - Javascript Custom Actions
Powered by GitBook
On this page
  • Creating the reusable workflow job
  • Setting up the reusable workflow files
  • Refactoring the build and push workflow
  1. Chap3-reusable-workflows

Section 3 - The build and push reusable workflow

PreviousSection 2 - The test reusable workflowNextSection 4 - The deploy reusable workflow

Last updated 8 months ago

Similarly to the reusable test workflow, the build and push workflow will closely resemble the one we created in Chapter 2, Section 4. The main difference is that this new workflow job will be reusable, allowing it to be called from other repositories.

Creating the reusable workflow job

We’ll start by referencing the logic used in Chapter 2, Section 4 (The build and test job) and refactor it to part of a reusable workflow.

Setting up the reusable workflow files

First, we will rename the reusable-test.yaml file located in the .github/workflows/ directory of the repository to reusable-test-build.yaml.

Refactoring the build and push workflow

Next, we’ll refactor the build and push workflow by copying it from one of our service repositories and placing it below the test job in our newly renamed reusable workflow file. For this example, we'll use the from the test-build-and-deploy workflow in the user-management-service repository and modify it to be reusable.

To make the build and push job reusable, we’ll follow a similar process as with the test job. Since it’s now part of a reusable workflow file, we only need to introduce the necessary inputs and utilize them within the job.

We’ll add the following inputs, providing default values for each:

  • image-owner: the repository owner (the ORG or username)

    • defaults to: ${{ github.event.repository.owner.login }}

  • image-name: the image name

    • defaults to: ${{ github.event.repository.name }}

  • build-tags: the build tags

    • defaults to:

            type=sha,prefix=,format=long
            type=ref,event=branch
            type=ref,event=pr
  • build-push: whether to push the image after the build

    • defaults to:

      ${{ github.event_name != 'pull_request' }}

We’ll now place the build and push job into the reusable workflow, integrating the inputs we just defined:

name: test-build
...
on:
  workflow_call:
    inputs:
...

      image-owner:
        required: false
        type: string
        description: "The repository owner (the ORG or username)"
        default: ${{ github.event.repository.owner.login }}
      image-name:
        required: false
        type: string
        description: "The image name"
        default: ${{ github.event.repository.name }}
      build-tags:
        required: false
        type: string
        description: "The build tags"
        default: |
          type=sha,prefix=,format=long
          type=ref,event=branch
          type=ref,event=pr
      build-push:
        required: false
        type: boolean
        description: "Whether to push the image after build"
        default: ${{ github.event_name != 'pull_request' }}
jobs:
  test:
  ...
  build:
    runs-on: ubuntu-latest
    needs: test
    steps:
      - name: checkout
        uses: actions/checkout@v4
      - name: Docker meta
        id: meta
        uses: docker/metadata-action@v5
        with:
          images: ghcr.io/${{ inputs.image-owner }}/${{ inputs.image-name }}
          tags: ${{ inputs.build-tags }}
      - name: Set up QEMU
        uses: docker/setup-qemu-action@v3
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3
      - name: Login to Github Container Registry
        uses: docker/login-action@v3
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}
      - name: Build and push
        uses: docker/build-push-action@v6
        with:
          push: ${{ inputs.build-push }}
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}

github-actions-by-example-reusable-workflows
build and push job