Docker Build and Publish #125
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Docker Build and Publish | |
on: | |
push: | |
branches: | |
- 'main' | |
# Manual trigger from the UI | |
workflow_dispatch: | |
inputs: | |
message: | |
description: "Message for build" | |
required: true | |
jobs: | |
docker: | |
runs-on: ubuntu-24.04 | |
steps: | |
- | |
name: Checkout | |
uses: actions/checkout@v4 | |
- | |
name: Check whether there are any commits since this run was last triggered and push them and/or set the output | |
id: should_run | |
run: | | |
git config user.name github-actions | |
git config user.email [email protected] | |
# Defensive: update submodules in the main branch | |
git checkout main | |
git submodule update --init --recursive --remote --rebase --force | |
git add . | |
git commit -am "Update submodules [skip ci]" --no-verify || true | |
git push origin main --no-verify | |
git checkout - | |
git rebase main | |
CHANGES_COUNT=$(git log @{u}.. | wc -l) | |
MANUAL_REBUILD="${{ github.event_name == 'workflow_dispatch' }}" | |
VERSION_BUMP="${{ contains(github.event.inputs.message, '[version bump]') }}" | |
if [ $CHANGES_COUNT -gt 0 ] || [[ $MANUAL_REBUILD == "true" && $VERSION_BUMP == "true" ]]; then | |
# Do the version bump in the 'main' branch ONLY if | |
# there were other changes coming from the 'main' branch (or) | |
# this is a manual trigger with the key-phrase | |
git checkout main | |
TAG_NAME=$(npm version -m "%s [skip ci]" patch) | |
git commit --all --amend --no-edit --no-verify | |
git push origin main --no-verify | |
git tag -f $TAG_NAME | |
git push origin --tags --no-verify | |
echo "TAG_NAME=${TAG_NAME:1}" >> $GITHUB_ENV | |
fi | |
echo "Number of changes: $CHANGES_COUNT" | |
if [ $CHANGES_COUNT -eq 0 ] && [ $MANUAL_REBUILD != "true" ]; then | |
echo "No changes found - terminating the build" | |
echo "::set-output name=should_run::false" | |
else # changes > 0 (or) MANUAL_REBUILD=true | |
echo "Pushing rebased commits" | |
git push origin $(git rev-parse --abbrev-ref HEAD) --no-verify | |
fi | |
- | |
name: Docker meta | |
id: meta | |
if: ${{ (steps.should_run.outputs.should_run != 'false') && (github.event_name != 'workflow_dispatch' || (github.event_name == 'workflow_dispatch' && (contains(github.event.inputs.message, '[main]'))) )}} | |
uses: docker/metadata-action@v5 | |
with: | |
# list of Docker images to use as base name for tags | |
images: | | |
${{ github.repository }} | |
ghcr.io/${{ github.repository }} | |
# generate Docker tags based on the following events/attributes | |
tags: | | |
${{ env.TAG_NAME }} | |
latest | |
- | |
name: Set up QEMU | |
uses: docker/setup-qemu-action@v3 | |
if: ${{ (steps.should_run.outputs.should_run != 'false') && (github.event_name != 'workflow_dispatch' || (github.event_name == 'workflow_dispatch' && (contains(github.event.inputs.message, '[main]'))) )}} | |
- | |
name: Set up Docker Buildx | |
uses: docker/setup-buildx-action@v3 | |
if: ${{ (steps.should_run.outputs.should_run != 'false') && (github.event_name != 'workflow_dispatch' || (github.event_name == 'workflow_dispatch' && (contains(github.event.inputs.message, '[main]'))) )}} | |
- | |
name: Login to GitHub Container Registry | |
uses: docker/login-action@v3 | |
if: ${{ (steps.should_run.outputs.should_run != 'false') && (github.event_name != 'workflow_dispatch' || (github.event_name == 'workflow_dispatch' && (contains(github.event.inputs.message, '[main]'))) )}} | |
with: | |
registry: ghcr.io | |
username: ${{ github.repository_owner }} | |
password: ${{ secrets.GITHUB_TOKEN }} | |
- | |
name: Login to DockerHub | |
uses: docker/login-action@v3 | |
if: ${{ (steps.should_run.outputs.should_run != 'false') && (github.event_name != 'workflow_dispatch' || (github.event_name == 'workflow_dispatch' && (contains(github.event.inputs.message, '[main]'))) )}} | |
with: | |
username: ${{ secrets.DOCKERHUB_USERNAME }} | |
password: ${{ secrets.DOCKERHUB_TOKEN }} | |
- | |
name: Build and push | |
if: ${{ (steps.should_run.outputs.should_run != 'false') && (github.event_name != 'workflow_dispatch' || (github.event_name == 'workflow_dispatch' && (contains(github.event.inputs.message, '[main]'))) )}} | |
uses: docker/build-push-action@v5 | |
with: | |
context: . | |
# If needed, we can add more platforms when requested. | |
platforms: linux/amd64,linux/arm64 | |
# Do not push pull requests | |
push: ${{ github.event_name != 'pull_request' }} | |
tags: ${{ steps.meta.outputs.tags }} | |
labels: ${{ steps.meta.outputs.labels }} |