Files
2026-11-20 18:43:07 +01:00

116 lines
4.7 KiB
YAML

name: Build and publish image
on:
push:
tags: ['*']
concurrency:
group: coredns-web-ui-image
cancel-in-progress: false
jobs:
image:
runs-on: ubuntu-latest
permissions:
contents: read
actions: read
env:
CI_IMAGE: coredns-web-ui:ci-${{ github.run_id }}-${{ github.run_attempt }}
steps:
- uses: actions/checkout@v4
with:
persist-credentials: false
- name: Resolve version
id: version
run: |
set -eu
version=${GITHUB_REF_NAME#v}
printf '%s\n' "$version" | grep -Eq '^[0-9]+\.[0-9]+\.[0-9]+(-[0-9A-Za-z.-]+)?$' || { echo "::error::tag $GITHUB_REF_NAME is not a semantic version" >&2; exit 1; }
echo "version=$version" >> "$GITHUB_OUTPUT"
case $version in *-*) echo latest=false ;; *) echo latest=true ;; esac >> "$GITHUB_OUTPUT"
- name: Wait for CI on main
env:
GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }}
run: |
set -eu
commit=$(git rev-parse HEAD)
url="${GITHUB_API_URL}/repos/${GITHUB_REPOSITORY}/actions/workflows/test.yaml/runs"
deadline=$((SECONDS + 1800))
registration_deadline=$((SECONDS + 120))
while (( SECONDS < deadline )); do
response=$(curl --fail --silent --show-error --max-time 20 --retry 2 \
--header "Authorization: Bearer ${GITEA_TOKEN:?missing Gitea job token}" \
"$url?branch=main&event=push&head_sha=$commit&limit=1")
run=$(jq -c --arg sha "$commit" '
.workflow_runs | map(select(.head_branch == "main" and .event == "push" and .head_sha == $sha)) | first // empty
' <<< "$response")
if [ -z "$run" ]; then
if (( SECONDS >= registration_deadline )); then
echo "No push CI run on main for $commit" >&2
exit 1
fi
else
status=$(jq -r '.status' <<< "$run")
conclusion=$(jq -r '.conclusion // ""' <<< "$run")
run_url=$(jq -r '.html_url' <<< "$run")
echo "Main CI for $commit: $status ${conclusion} ($run_url)"
case "$status:$conclusion" in
completed:success) exit 0 ;;
requested:|pending:|queued:|waiting:|in_progress:) ;;
*) exit 1 ;;
esac
fi
sleep 15
done
echo "Timed out waiting for main CI for $commit" >&2
exit 1
- name: Build image
id: build
env:
VERSION: ${{ steps.version.outputs.version }}
run: |
set -eu
if docker image inspect "$CI_IMAGE" >/dev/null 2>&1; then
echo "::error::local image tag already exists: $CI_IMAGE" >&2
exit 1
fi
commit=$(git rev-parse HEAD)
commit=${commit:0:12}
docker build --build-arg APP_VERSION="$VERSION" --build-arg COMMIT_ID="$commit" -f dockerfile -t "$CI_IMAGE" .
printf 'image_id=%s\n' "$(docker image inspect --format '{{.Id}}' "$CI_IMAGE")" >> "$GITHUB_OUTPUT"
- name: Publish image
env:
GIT_HOST: ${{ vars.GIT_HOST }}
REGISTRY_USER: ${{ vars.REGISTRY_USER }}
GIT_TOKEN: ${{ secrets.GIT_TOKEN }}
VERSION: ${{ steps.version.outputs.version }}
LATEST: ${{ steps.version.outputs.latest }}
run: |
set -eu
test -n "$GIT_HOST" && test -n "$REGISTRY_USER" && test -n "$GIT_TOKEN"
image="${GIT_HOST}/${GITHUB_REPOSITORY}"
printf '%s' "$GIT_TOKEN" | docker login "$GIT_HOST" --username "$REGISTRY_USER" --password-stdin
trap 'docker logout "$GIT_HOST" >/dev/null 2>&1 || true' EXIT
push_tag() (
destination="${image}:$1"
previous=$(docker image inspect --format '{{.Id}}' "$destination" 2>/dev/null || true)
current=$(docker image inspect --format '{{.Id}}' "$CI_IMAGE")
if [ "$previous" != "$current" ]; then
docker tag "$CI_IMAGE" "$destination"
trap 'if [ -n "$previous" ]; then docker tag "$previous" "$destination"; else docker image rm "$destination" >/dev/null; fi' EXIT
fi
docker push "$destination"
)
push_tag "$VERSION"
if [ "$LATEST" = true ]; then
push_tag latest
fi
- name: Remove local images
if: always() && steps.build.outputs.image_id != ''
env:
IMAGE_ID: ${{ steps.build.outputs.image_id }}
run: |
if [ "$(docker image inspect --format '{{.Id}}' "$CI_IMAGE" 2>/dev/null)" = "$IMAGE_ID" ]; then
docker image rm "$CI_IMAGE" >/dev/null
fi