Build and push the Docker image when a release is published #2

Merged
iamdoubz merged 1 commits from release-image-workflow into main 2026-11-05 23:05:36 -06:00
+42
View File
@@ -0,0 +1,42 @@
# Builds the Docker image when a release is published and pushes it to this Gitea's container registry.
# Release v1.2.3 -> :1.2.3 :1.2 :1 :latest
# Pre-release v1.3.0 -> :1.3.0 :dev (:1 and :1.3 only move on stable releases)
# Setup: add a repository secret REGISTRY_TOKEN, a personal access token with the write:package scope.
# The built-in GITEA_TOKEN can't push packages.
name: Release image
on:
release:
types: [published]
jobs:
image:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Build and push
shell: bash
env:
TAG: ${{ gitea.event.release.tag_name }}
PRERELEASE: ${{ gitea.event.release.prerelease }}
SERVER: ${{ gitea.server_url }}
REPO: ${{ gitea.repository }}
OWNER: ${{ gitea.repository_owner }}
REGISTRY_TOKEN: ${{ secrets.REGISTRY_TOKEN }}
run: |
set -euo pipefail
version="${TAG#v}"
[[ "$version" =~ ^(([0-9]+)\.[0-9]+)\.[0-9]+ ]] || { echo "::error::Release tag '$TAG' isn't vX.Y.Z"; exit 1; }
if [[ "$PRERELEASE" == true ]]; then
tags=("$version" dev) # :X and :X.Y stay on the last stable release
else
tags=("$version" "${BASH_REMATCH[1]}" "${BASH_REMATCH[2]}" latest)
fi
registry="${SERVER#*://}"
image="$registry/${REPO,,}" # registry paths must be lowercase
echo "$REGISTRY_TOKEN" | docker login "$registry" -u "$OWNER" --password-stdin
docker build --label "org.opencontainers.image.version=$version" "${tags[@]/#/--tag=$image:}" .
for t in "${tags[@]}"; do docker push "$image:$t"; done