Skip to content

Publish autodarts docker image #593

Publish autodarts docker image

Publish autodarts docker image #593

# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.
# GitHub recommends pinning actions to a commit SHA.
# To get a newer version, you will need to update the SHA.
# You can also reference a tag or branch, but the action may change without warning.
name: Publish autodarts docker image
#trigger when a new version tag is set
on:
push:
branches:
- main
paths:
- Dockerfile
schedule:
- cron: "0 10 * * *"
workflow_dispatch:
inputs:
force_rebuild:
description: Force a rebuild and push even when no new autodarts version is available
required: false
type: boolean
default: false
permissions:
contents: write
env:
AUTODARTS_IMAGE_NAME: michvllni/autodarts
jobs:
get_target_version:
name: Get latest release from autodarts
runs-on: ubuntu-latest
outputs:
version: ${{ steps.autodarts-latest-release.outputs.version }}
create_release: ${{ steps.autodarts-latest-release.outputs.create_release }}
steps:
- name: Get latest release from autodarts
id: autodarts-latest-release
run: |
latestVersion=$(curl -s https://get.autodarts.io/detection/latest/linux/arm64/RELEASES.json | jq -r '.currentVersion')
echo "Latest version from autodarts: $latestVersion"
#get current version from docker hub michvllni/autodarts-docker
currentVersion=$(curl -s https://hub.docker.com/v2/repositories/michvllni/autodarts/tags/ | jq -r '.results[].name' | grep -E '^[0-9]+\.[0-9]+\.[0-9]+$' | sort -V | tail -n 1)
echo "Current version from docker hub: v$currentVersion"
if [ "$latestVersion" != "v$currentVersion" ]; then
echo "New version available"
echo "version=$latestVersion" >> $GITHUB_OUTPUT
echo "create_release=true" >> $GITHUB_OUTPUT
elif [ "${{ github.event_name }}" = "workflow_dispatch" ] && [ "${{ github.event.inputs.force_rebuild }}" = "true" ]; then
echo "Manual force rebuild requested"
echo "version=$latestVersion" >> $GITHUB_OUTPUT
echo "create_release=false" >> $GITHUB_OUTPUT
elif [ "${{ github.event_name }}" = "push" ]; then
echo "Dockerfile changed, rebuilding latest image"
echo "version=$latestVersion" >> $GITHUB_OUTPUT
echo "create_release=false" >> $GITHUB_OUTPUT
else
echo "No new version available"
echo "create_release=false" >> $GITHUB_OUTPUT
fi
shell: bash
push_to_registry:
name: Build and Push Docker image to Github Container Registry
runs-on: ubuntu-latest
needs: get_target_version
if: needs.get_target_version.outputs.version != null && needs.get_target_version.outputs.version != ''
steps:
- name: Check out the repo
uses: actions/checkout@v6
# https://github.com/docker/setup-qemu-action
- name: Set up QEMU
uses: docker/setup-qemu-action@v4
# https://github.com/docker/setup-buildx-action
- name: Set up Docker Buildx
id: buildx
uses: docker/setup-buildx-action@v4
- name: Login to Docker Hub
if: github.event_name != 'pull_request'
uses: docker/login-action@v4
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Get Alpine version from Dockerfile
id: alpine_version
run: |
mapfile -t alpineVersions < <(grep -E '^FROM( --platform=\$\{[^}]+\})? alpine:' Dockerfile | sed -E 's/^FROM( --platform=\$\{[^}]+\})? alpine:([^[:space:]]+).*/\2/' | sort -u)
if [ ${#alpineVersions[@]} -eq 0 ]; then
echo "Could not determine Alpine version from Dockerfile"
exit 1
fi
echo "version=${alpineVersions[0]}" >> $GITHUB_OUTPUT
- name: Set Docker Image Metadata
id: set_image_metadata
uses: docker/metadata-action@v6
with:
# list of Docker images to use as base name for tags
images: |
docker.io/${{ env.AUTODARTS_IMAGE_NAME }}
# Docker tags based on the following events/attributes
tags: |
type=schedule
type=ref,event=branch
type=ref,event=pr
type=semver,pattern={{version}},value=${{ needs.get_target_version.outputs.version }}
type=semver,pattern={{major}}.{{minor}},value=${{ needs.get_target_version.outputs.version }}
type=semver,pattern={{major}},value=${{ needs.get_target_version.outputs.version }}
type=semver,pattern={{version}}-${{ steps.alpine_version.outputs.version }},value=${{ needs.get_target_version.outputs.version }}
type=sha
latest
- name: Build and push
uses: docker/build-push-action@v7
with:
context: .
provenance: false
platforms: linux/amd64,linux/arm64
build-args: VERSION=${{ needs.get_target_version.outputs.version }}
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.set_image_metadata.outputs.tags }}
labels: ${{ steps.set_image_metadata.outputs.labels }}
create_release:
name: Create GitHub Release
runs-on: ubuntu-latest
needs:
- get_target_version
- push_to_registry
if: needs.get_target_version.outputs.version != '' && needs.get_target_version.outputs.create_release == 'true' && github.event_name != 'pull_request'
steps:
- name: Create GitHub tag and release
uses: softprops/action-gh-release@v3
with:
tag_name: ${{ needs.get_target_version.outputs.version }}
name: ${{ needs.get_target_version.outputs.version }}
generate_release_notes: true