I have a monorepo in GitHub containing multiple services.
I want to build them concurrently (using GitHub Actions) for 2 conditions:
- Tag - build the images using the tag name (
service-a:v1.0.0) - Branch main - build the images using the tag
latestwith the cache.
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:
- The workflows are duplicated because of the
tagandbranch mainrunning policy - The execution of the composite action has a lot of boilerplate
What is the best way to reduce these duplications and make reuse of the build action? (in parallel)