Skip to content

Deploy hass-addon-sunsynk-multi to ghcr.io #3

Deploy hass-addon-sunsynk-multi to ghcr.io

Deploy hass-addon-sunsynk-multi to ghcr.io #3

name: Deploy hass-addon-sunsynk-multi to ghcr.io
on:
release:
types:
- published
workflow_run:
workflows: ["CI"]
branches: [main]
types:
- completed
workflow_dispatch: {}
env:
REGISTRY_IMAGE: ghcr.io/${{ github.repository_owner }}/hass-addon-sunsynk-multi
jobs:
information:
name: Gather add-on information
runs-on: ubuntu-latest
outputs:
architectures: ${{ steps.information.outputs.architectures }}
build: ${{ steps.information.outputs.build }}
description: ${{ steps.information.outputs.description }}
environment: ${{ steps.release.outputs.environment }}
name: ${{ steps.information.outputs.name }}
slug: ${{ steps.information.outputs.slug }}
target: ${{ steps.information.outputs.target }}
version: ${{ steps.release.outputs.version }}
steps:
- name: ⤵️ Check out code from GitHub
uses: actions/checkout@v4
- name: 🚀 Run add-on information
id: information
uses: frenck/[email protected]
with:
path: ./hass-addon-sunsynk-multi
- name: ℹ️ Gather version and environment
id: release
run: |
sha="${{ github.sha }}"
# Default values
environment="edge"
version="${sha:0:7}"
# Check if it's a release event
if [[ "${{ github.event_name }}" == "release" ]]; then
version="${{ github.event.release.tag_name }}" # Lowercase the tag name
version="${version#v}" # Remove 'v' prefix if present
environment="stable"
# Set environment to beta if it's a prerelease
if [[ "${{ github.event.release.prerelease }}" == "true" ]]; then
environment="beta"
fi
fi
# Output the environment and version
echo "environment=${environment}" >> "$GITHUB_OUTPUT"
echo "version=${version}" >> "$GITHUB_OUTPUT"
# Log the outputs
cat "$GITHUB_OUTPUT" >> $GITHUB_STEP_SUMMARY
build:
name: 👷 Build & Deploy ${{ matrix.platform }} ${{ needs.information.outputs.environment }}/${{ needs.information.outputs.version }}
runs-on: ubuntu-latest
needs: information
strategy:
fail-fast: false
matrix:
platform:
- linux/amd64
- linux/arm/v6
- linux/arm/v7
- linux/arm64
steps:
- name: ⤵️ Check out code from GitHub
uses: actions/checkout@v4
- name: Prepare Files for Build
run: |
mkdir -p hass-addon-sunsynk-multi/sunsynk
cp -r src hass-addon-sunsynk-multi/sunsynk/
cp setup.* README.md hass-addon-sunsynk-multi/sunsynk/
- name: Set up the correct base image based on platform
run: |
case "${{ matrix.platform }}" in
linux/amd64) echo "BUILD_FROM=homeassistant/amd64-base-python:3.11-alpine3.18" >> $GITHUB_ENV ;;
linux/arm64) echo "BUILD_FROM=homeassistant/aarch64-base-python:3.11-alpine3.18" >> $GITHUB_ENV ;;
linux/arm/v6) echo "BUILD_FROM=homeassistant/armhf-base-python:3.11-alpine3.18" >> $GITHUB_ENV ;;
linux/arm/v7) echo "BUILD_FROM=homeassistant/armv7-base-python:3.11-alpine3.18" >> $GITHUB_ENV ;;
linux/i386) echo "BUILD_FROM=homeassistant/i386-base-python:3.11-alpine3.18" >> $GITHUB_ENV ;;
esac
- name: Docker meta
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY_IMAGE }}
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Cache Docker layers
uses: actions/cache@v4
with:
path: /tmp/.buildx-cache # Cache path
key: ${{ runner.os }}-buildx-${{ github.sha }} # Unique key for cache
restore-keys: |
${{ runner.os }}-buildx-
- name: Login to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Prepare sanitized platform name
id: sanitize-platform
run: |
SANITIZED_PLATFORM=$(echo ${{ matrix.platform }} | sed 's/^linux\///; s/\///g')
echo "SANITIZED_PLATFORM=${SANITIZED_PLATFORM}" >> $GITHUB_ENV
echo "Sanitized platform name: ${SANITIZED_PLATFORM}"
- name: Build and push by digest
id: build
uses: docker/build-push-action@v6
with:
context: ./hass-addon-sunsynk-multi # Specify the directory containing your Dockerfile
file: ./hass-addon-sunsynk-multi/Dockerfile # Specify the Dockerfile path within the context
platforms: ${{ matrix.platform }}
cache-from: type=local,src=/tmp/.buildx-cache # Use cache for build
cache-to: type=local,dest=/tmp/.buildx-cache,mode=max # Save cache
labels: ${{ steps.meta.outputs.labels }}
outputs: type=image,name=${{ env.REGISTRY_IMAGE }},push-by-digest=true,name-canonical=true,push=true
build-args: |
BUILD_FROM=${{ env.BUILD_FROM }}
- name: Export digest
run: |
mkdir -p /tmp/digests
digest="${{ steps.build.outputs.digest }}"
touch "/tmp/digests/${digest#sha256:}"
- name: Upload digest
uses: actions/upload-artifact@v4
with:
name: digests-${{ env.SANITIZED_PLATFORM }} # Use the sanitized platform name
path: /tmp/digests/*
if-no-files-found: error
retention-days: 1
- name: Save platform to file
id: save-platform
run: |
mkdir -p outputs
echo ${{ env.SANITIZED_PLATFORM }} >> outputs/${{ env.SANITIZED_PLATFORM }}.txt
- name: Artifact platform file for later
uses: actions/upload-artifact@v3
with:
name: outputs
path: outputs/*.txt
merge:
runs-on: ubuntu-latest
needs:
- information
- build
steps:
- name: Download digests
uses: actions/download-artifact@v4
with:
path: /tmp/digests
pattern: digests-*
merge-multiple: true
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Docker meta
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY_IMAGE }}
- name: Login to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Load outputs
uses: actions/download-artifact@v3
with:
name: outputs
path: outputs
- name: Load outputs to variable
id: platforms_raw
run: |
ls outputs
# Replace newlines with commas, then remove the trailing comma
echo "value=$(cat outputs/*.txt | tr '\n' ',' | sed 's/,$//')" >> "$GITHUB_OUTPUT"
- name: Generate tags for multi-platform image
run: |
# Define base tags
TAGS=(
"-t ${{ env.REGISTRY_IMAGE }}:${{ needs.information.outputs.version }}"
"-t ${{ env.REGISTRY_IMAGE }}:${{ needs.information.outputs.environment }}"
)
# Get the list of platforms from the build job output
platforms="${{ steps.platforms_raw.outputs.value }}"
# Split the platforms and add platform-specific tags
IFS=',' read -ra PLATFORM_ARRAY <<< "$platforms"
for PLATFORM in "${PLATFORM_ARRAY[@]}"; do
TAGS+=("-t ${{ env.REGISTRY_IMAGE }}/${PLATFORM}:${{ needs.information.outputs.version }}")
TAGS+=("-t ${{ env.REGISTRY_IMAGE }}/${PLATFORM}:${{ needs.information.outputs.environment }}")
done
# Join tags into a string
TAGS_STRING=$(printf " %s" "${TAGS[@]}")
# Export the tags string to the environment for later use
echo "TAGS_STRING=${TAGS_STRING}" >> $GITHUB_ENV
# Log the generated tags for debugging
echo "Generated tags: ${TAGS_STRING}"
- name: Create manifest list and push
working-directory: /tmp/digests
run: |
docker buildx imagetools create $TAGS_STRING \
$(printf '${{ env.REGISTRY_IMAGE }}@sha256:%s ' *)
- name: Inspect image
run: |
# Inspect the image using the first tag from TAGS_STRING (without the leading '-t ' part)
INSPECT_TAG=$(echo "${TAGS_STRING}" | awk '{print $2}')
# Log the tag being inspected
echo "Inspecting image with tag: $INSPECT_TAG"
# Run the inspection
docker buildx imagetools inspect $INSPECT_TAG
publish_addon:
name: 🆕 Update addon version to ${{ needs.information.outputs.version }}
needs: [information, build, merge]
permissions:
contents: write
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Point edge to the new ref
run: |
sed -i 's/version:.*/version: "${{ needs.information.outputs.version }}"/g' hass-addon-sunsynk-edge/config.yaml
- name: Point multi to the new ref
if: "${{ needs.information.outputs.environment == 'stable'}}"
run: |
sed -i 's/version:.*/version: "${{ needs.information.outputs.version }}"/g' hass-addon-sunsynk-multi/config.yaml
- uses: stefanzweifel/[email protected]
with:
branch: main
commit_user_email: [email protected]
commit_message: Update addon version to ${{ needs.information.outputs.version }} [no ci]