Skip to content

Publish Docker image #91

Publish Docker image

Publish Docker image #91

Workflow file for this run

name: Publish Docker image
on:
workflow_dispatch:
inputs:
tag:
type: string
description: Set tag for docker image if workflow not started from main
default: ""
required: false
push:
branches:
- main
jobs:
release-please:
if: github.event_name != 'workflow_dispatch'
runs-on: ubuntu-latest
steps:
- uses: googleapis/release-please-action@v4
id: release
- uses: actions/checkout@v4
- name: tag major and minor versions
if: steps.release.outputs.release_created
run: |
git config user.name github-actions[bot]
git config user.email 41898282+github-actions[bot]@users.noreply.github.com
git tag -d v${{ steps.release.outputs.major }} || true
git tag -d v${{ steps.release.outputs.major }}.${{ steps.release.outputs.minor }} || true
git push origin :v${{ steps.release.outputs.major }} || true
git push origin :v${{ steps.release.outputs.major }}.${{ steps.release.outputs.minor }} || true
git tag -a v${{ steps.release.outputs.major }} -m "Release v${{ steps.release.outputs.major }}"
git tag -a v${{ steps.release.outputs.major }}.${{ steps.release.outputs.minor }} -m "Release v${{ steps.release.outputs.major }}.${{ steps.release.outputs.minor }}"
git push origin v${{ steps.release.outputs.major }}
git push origin v${{ steps.release.outputs.major }}.${{ steps.release.outputs.minor }}
- name: display pr output
if: steps.release.outputs.prs_created == 'true'
run: |-
echo 'pr output: ${{ steps.release.outputs.pr }}'
echo 'prs output: ${{ steps.release.outputs.prs }}'
- name: install yq
if: steps.release.outputs.prs_created == 'true'
uses: chrisdickinson/setup-yq@v1.0.1
with:
yq-version: v4.44.2
- name: checkout release-please branch
if: steps.release.outputs.prs_created == 'true'
uses: actions/checkout@v4
with:
ref: ${{ fromJson(steps.release.outputs.pr).headBranchName }}
- name: update addon version
if: steps.release.outputs.prs_created == 'true'
run: |-
version=$(yq '.version' ./pubspec.yaml)
yq -i ".version |= \"v${version}\"" addon/config.json
git add .
if [[ ! -z "$(git status --porcelain)" ]]; then
git config user.name github-actions[bot]
git config user.email 41898282+github-actions[bot]@users.noreply.github.com
git commit -m "Update addon version"
git push
fi
outputs:
release_created: ${{ steps.release.outputs.release_created }}
tag_name: ${{ steps.release.outputs.tag_name }}
push_to_registry:
name: Push Docker image to Docker Hub
needs:
- release-please
if: ${{ ! cancelled() && needs.release-please.outputs.release_created || github.event_name == 'workflow_dispatch' }}
runs-on: ubuntu-latest
steps:
- name: Check out the repo
uses: actions/checkout@v4
with:
fetch-tags: true
fetch-depth: 200
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Log in to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUBUSERNAME }}
password: ${{ secrets.DOCKERHUBACCESSTOKEN }}
- name: Get nearest tag
id: get-nearest-tag
if: ${{ github.ref != format('refs/heads/{0}', 'main') }}
shell: bash
run: |-
TAGS=$(git tag --list)
NEAREST_TAG=$(git describe --tags --abbrev=0)
SPECIFIC_TAG=$(git tag --points-at $(git rev-list -n 1 $NEAREST_TAG) | grep -E 'v[0-9]+\.[0-9]+\.[0-9]+')
if [[ ! -z "$SPECIFIC_TAG" ]]; then
echo "Nearest tag is: $SPECIFIC_TAG"
echo "Using tag ${SPECIFIC_TAG}-edge"
echo "nearest_tag=${SPECIFIC_TAG}-edge" >> $GITHUB_OUTPUT
else
echo "Nearest tag not found, using 'edge' tag"
echo "nearest_tag=edge" >> $GITHUB_OUTPUT
fi
- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ secrets.DOCKERHUBUSERNAME }}/leaf2mqtt
tags: |
type=raw,value=${{ needs.release-please.outputs.tag_name }},enable=${{ github.ref == format('refs/heads/{0}', 'main') }}
type=raw,value=latest,enable=${{ github.ref == format('refs/heads/{0}', 'main') }}
type=raw,value=${{ inputs.tag != '' && inputs.tag || steps.get-nearest-tag.outputs.nearest_tag }},enable=${{ github.ref != format('refs/heads/{0}', 'main') }}
- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
context: .
build-args: |
APP_VERSION=${{ steps.meta.outputs.version }}
push: true
platforms: linux/amd64,linux/arm64,linux/arm/v8
tags: ${{ steps.meta.outputs.tags }}