itaied
itaied

Reputation: 7107

Github actions - multiple workflows for parallel image build

I have a monorepo in GitHub containing multiple services.
I want to build them concurrently (using GitHub Actions) for 2 conditions:

I couldn't find a way how to create a common workflow and modify the arguments (like a function).
The only solution I could come up with is to create a composite actions like so:

runs:
  using: "composite"
  steps:
    - uses: actions/checkout@v2
      with:
        lfs: true

    - uses: docker/setup-qemu-action@v1

    - uses: docker/setup-buildx-action@v1

    - uses: docker/login-action@v1
      with:
        registry: ${{ secrets.registry }}
        username: ${{ secrets.username }}
        password: ${{ secrets.password }}

    - name: Environment details
      id: github_ref
      run: |
        echo ::set-output name=SOURCE_TAG::${GITHUB_REF#refs/tags/}

    - uses: docker/build-push-action@v2
      with:
        context: ${{ inputs.context }}
        push: true
        file: ${{ inputs.dockerfile }}
        tags: ${{ secrets.registry }}/${{ inputs.image }}:{{ steps.github_ref.outputs.SOURCE_TAG }}
        build-args: ${{ inputs.build-args }}
        cache-from: ${{ inputs.cache-from }}
        cache-to: ${{ inputs.cache-to }}

But now I have a problem where I need to specify all the jobs for the services in 2 workflows, for example:

main branch workflow:

on:
  push:
    branches:
      - main

jobs:
  build-service-a:
    timeout-minutes: 15
    runs-on: ubuntu-latest
    steps:
      - uses: ./.github/actions/build-image
        with:
          dockerfile: service-a/Dockerfile
          context: .
          image: service-a

  build-service-b:
    timeout-minutes: 15
    runs-on: ubuntu-latest
    steps:
      - uses: ./.github/actions/build-image
        with:
          dockerfile: service-b/Dockerfile
          context: .
          image: service-b

tag branch workflow:


on:
  release:
    types:
      - created

jobs:
  build-service-a:
    timeout-minutes: 15
    runs-on: ubuntu-latest
    steps:
      - uses: ./.github/actions/build-image
        with:
          dockerfile: service-a/Dockerfile
          context: .
          image: service-a
          cache-to: ...
          cache-from: ...

  build-service-b:
    timeout-minutes: 15
    runs-on: ubuntu-latest
    steps:
      - uses: ./.github/actions/build-image
        with:
          dockerfile: service-b/Dockerfile
          context: .
          image: service-b
          cache-to: ...
          cache-from: ...

As you can see, I have multiple duplications:

What is the best way to reduce these duplications and make reuse of the build action? (in parallel)

Upvotes: 0

Views: 2803

Answers (1)

Krzysztof Madej
Krzysztof Madej

Reputation: 40543

You will get a slightly better result if you use matrix:

jobs:
  build:
    runs-on: ubuntu-latest
    strategy:
      matrix:
       include:
         - dockerfile: service-a/Dockerfile
           image: "service-a"
           cache-to: ...
           cache-from: ...
         - dockerfile: service-b/Dockerfile
           image: "service-b"
           cache-to: ...
           cache-from: ...
    steps:
      - uses: ./.github/actions/build-image
        with:
          dockerfile: ${{ matrix.dockerfile }}
          context: .
          image: : ${{ matrix.image }}
          cache-to: ${{ matrix.cache-to}}
          cache-from: ${{ matrix.cache-from}}

This will not save you from The workflows are duplicated because of the tag and branch main running policy but reduce bilerplate code inside workflow.

Upvotes: 2

Related Questions