Skip to content

Commit

Permalink
Extract build-quesma-docker-image to a reusable GitHub Actions work…
Browse files Browse the repository at this point in the history
…flow (#862)

While working on an upcoming PR, I noticed that
`build-quesma-docker-image` step in GitHub Actions workflow was
duplicated in 3 places... and my PR would add a 4th copy!

Therefore extract the `build-quesma-docker-image` step into a "reusable
workflow" (`workflow_call` trigger) which can be called from other
workflows, avoiding duplication. All the behaviors of this step stay the
same - for now the "build+push" step and "build+export" step are
separate to make sure the behavior stays the same.

Additionally `docker-image-latest.yml` is renamed to
`nightly-docker-build-and-push.yml` in order to avoid confusion with the
new `build-quesma-docker-image.yml` file.

---------

Signed-off-by: Piotr Grabowski <[email protected]>
  • Loading branch information
avelanarius authored Oct 10, 2024
1 parent 69258d8 commit 0c220f9
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 135 deletions.
90 changes: 90 additions & 0 deletions .github/workflows/build-quesma-docker-image.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# Reusable workflow that builds Quesma Docker image and either:
# - pushes it to Docker Hub
# - or exports it to .tar artifact

name: Docker image build
on:
workflow_call: # Can be reused from another workflow
inputs:
VERSION:
description: 'Version number to tag the image with (optional)'
default: nightly
required: false
type: string
PUSH:
description: 'Whether to push the image to the registry (optional)' # if not, we will export it to .tar artifact
default: false
required: false
type: boolean

jobs:
build-quesma-docker-image:
strategy:
matrix:
module: [ "quesma" ]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Set up Go
uses: actions/setup-go@v5
with:
cache-dependency-path: ${{ matrix.module }}/go.sum
go-version: '1.22'

- name: Log in to Docker Hub
uses: docker/login-action@v3
if: ${{ inputs.PUSH }}
with:
username: ${{ secrets.DOCKER_USER }}
password: ${{ secrets.DOCKER_PAT }}

- name: Set the build date
run: echo QUESMA_BUILD_DATE=$(git --no-pager log -1 --date=format:'%Y-%m-%d' --format="%ad") >> $GITHUB_ENV

# If we don't push the image, we build it only for the host architecture and export it to .tar file
# This step is similar to "Build and push", but avoids building arm64 image, which takes
# a lot of time on GitHub Actions.
- name: Build and export
uses: docker/build-push-action@v6
if: ${{ !inputs.PUSH }}
with:
context: ${{ matrix.module }}/.
tags: |
quesma/quesma:${{ inputs.VERSION }}
quesma/quesma:nightly
push: false
outputs: type=docker,dest=/tmp/image.tar
cache-from: type=gha
cache-to: type=gha,mode=max
env:
DOCKER_BUILD_SUMMARY: false

# If we push the image, we build it for both amd64 and arm64 and don't export it
- name: Build and push
uses: docker/build-push-action@v6
if: ${{ inputs.PUSH }}
with:
context: ${{ matrix.module }}/.
tags: |
quesma/quesma:${{ inputs.VERSION }}
quesma/quesma:nightly
push: true
build-args: |
QUESMA_BUILD_SHA=${{ github.sha }}
QUESMA_VERSION=${{ inputs.VERSION }}
QUESMA_BUILD_DATE=${{ env.QUESMA_BUILD_DATE }}
platforms: linux/amd64,linux/arm64
env:
DOCKER_BUILD_SUMMARY: false

- name: Upload artifact
uses: actions/upload-artifact@v4
if: ${{ !inputs.PUSH }}
with:
name: ${{ matrix.module }}
path: /tmp/image.tar
retention-days: 1
65 changes: 0 additions & 65 deletions .github/workflows/docker-image-latest.yml

This file was deleted.

37 changes: 1 addition & 36 deletions .github/workflows/e2e-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,43 +10,8 @@ on: # @TODO should we run it on PRs?
description: 'Commit hash to run the tests'
required: true
jobs:

build-quesma-docker-image:
strategy:
matrix:
module: [ "quesma" ]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with: ## @TODO REMOVE
ref: ${{ github.event.inputs.GIT_REF }}

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Set up Go
uses: actions/setup-go@v5
with:
cache-dependency-path: ${{ matrix.module }}/go.sum
go-version: '1.22'

- name: Build and export
uses: docker/build-push-action@v6
with:
context: ${{ matrix.module }}/.
tags: ${{ matrix.module }}:latest
outputs: type=docker,dest=/tmp/image.tar
cache-from: type=gha
cache-to: type=gha,mode=max
env:
DOCKER_BUILD_SUMMARY: false

- name: Upload artifact
uses: actions/upload-artifact@v4
with:
name: ${{ matrix.module }}
path: /tmp/image.tar
retention-days: 1
uses: ./.github/workflows/build-quesma-docker-image.yml

e2e-test-run:
runs-on: ubuntu-latest
Expand Down
28 changes: 28 additions & 0 deletions .github/workflows/nightly-docker-build-and-push.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Docker image build (nightly)
# Pipeline which builds `nightly` Docker images for Quesma
# pushes them to Docker Hub as `quesma/quesma:nightly` (and `quesma/quesma:<VERSION>`, if VERSION is specified explicitly)
#
# This workflow is triggered by a push/merge to the `main` branch
# quesma/quesma:nightly image is a pointer to latest released version.
#
# This workflow is not reusable, use build-quesma-docker-image.yml as a reusable workflow.
on:
push:
branches: [ "main" ]
workflow_dispatch: # Handy for testing
inputs:
VERSION:
description: 'Version number to tag the image with (optional)'
default: nightly
required: true
PUSH:
description: 'Whether to push the image to the registry'
default: false
required: true
jobs:
build-quesma-docker-image:
uses: ./.github/workflows/build-quesma-docker-image.yml
with:
VERSION: ${{ inputs.VERSION || 'nightly' }} # when called from the main branch, `github.event.inputs.VERSION` doesn't use default value and is just empty
# Pushes to DockerHub only for `main` branch builds, unless set explicitly in the job input
PUSH: ${{ (github.event_name == 'push' && github.ref == 'refs/heads/main') || inputs.PUSH }}
34 changes: 1 addition & 33 deletions .github/workflows/pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,39 +70,7 @@ jobs:
run: go run gotest.tools/gotestsum@latest --format pkgname-and-test-fails ./...

build-quesma-docker-image:
strategy:
matrix:
module: [ "quesma" ]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Set up Go
uses: actions/setup-go@v5
with:
cache-dependency-path: ${{ matrix.module }}/go.sum
go-version: '1.22'

- name: Build and export
uses: docker/build-push-action@v6
with:
context: ${{ matrix.module }}/.
tags: ${{ matrix.module }}:latest
outputs: type=docker,dest=/tmp/image.tar
cache-from: type=gha
cache-to: type=gha,mode=max
env:
DOCKER_BUILD_SUMMARY: false

- name: Upload artifact
uses: actions/upload-artifact@v4
with:
name: ${{ matrix.module }}
path: /tmp/image.tar
retention-days: 1
uses: ./.github/workflows/build-quesma-docker-image.yml

build-log-generator:
strategy:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ permissions:
contents: write # To be able to create a release via GitHub API

jobs:
build-quesma-docker-image:
build-quesma-docker-image: # based on nightly-docker-build-and-push.yml, but heavily modified
strategy:
matrix:
module: [ "quesma" ]
Expand Down

0 comments on commit 0c220f9

Please sign in to comment.