Update .gitea/workflows/build-and-push.yml
Some checks failed
Build and Push Container / build-and-push (push) Has been cancelled

This commit is contained in:
wayfinderak 2026-03-23 05:41:43 +00:00
parent cf6b7eae81
commit 0f0d91f994

View File

@ -9,56 +9,139 @@ on:
- v* - v*
workflow_dispatch: workflow_dispatch:
jobs:
build-and-push:
runs-on: ubuntu-latest
env: env:
REGISTRY: gitea.wayfinderak.com REGISTRY: gitea.wayfinderak.com
IMAGE_NAME: wayfinderak/goff IMAGE_NAME: gitea.wayfinderak.com/wayfinderak/goff
jobs:
docker:
runs-on: ubuntu-latest
steps: steps:
- name: Checkout - name: Checkout
uses: actions/checkout@v4 uses: actions/checkout@v4
- name: Set up QEMU - name: Install Go and Docker CLI if needed
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Log in to Gitea registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ secrets.REGISTRY_USERNAME }}
password: ${{ secrets.REGISTRY_PASSWORD }}
- name: Prepare image tags
id: prep
shell: bash shell: bash
run: | run: |
short_sha="${GITHUB_SHA::7}" set -euo pipefail
tags="${REGISTRY}/${IMAGE_NAME}:sha-${short_sha}"
if [[ "${GITHUB_REF_TYPE}" == "branch" && ("${GITHUB_REF_NAME}" == "master" || "${GITHUB_REF_NAME}" == "main") ]]; then need_apt=0
tags+=$'\n'"${REGISTRY}/${IMAGE_NAME}:latest" need_apk=0
if ! command -v go >/dev/null 2>&1 || ! command -v docker >/dev/null 2>&1; then
if command -v apt-get >/dev/null 2>&1; then
need_apt=1
elif command -v apk >/dev/null 2>&1; then
need_apk=1
else
echo "No supported package manager found" >&2
exit 1
fi
fi fi
if [[ "${GITHUB_REF_TYPE}" == "tag" ]]; then if [ "$need_apt" -eq 1 ]; then
version="${GITHUB_REF_NAME#v}" apt-get update
tags+=$'\n'"${REGISTRY}/${IMAGE_NAME}:${version}" if ! command -v go >/dev/null 2>&1; then
apt-get install -y golang-go
fi
if ! command -v docker >/dev/null 2>&1; then
apt-get install -y docker.io curl dnsutils iputils-ping
fi
fi fi
{ if [ "$need_apk" -eq 1 ]; then
echo 'tags<<EOF' if ! command -v go >/dev/null 2>&1; then
echo "$tags" apk add --no-cache go
echo 'EOF' fi
} >> "$GITHUB_OUTPUT" if ! command -v docker >/dev/null 2>&1; then
apk add --no-cache docker-cli curl bind-tools iputils
fi
fi
- name: Build and push image go version
uses: docker/build-push-action@v6 docker version
with:
context: . - name: Registry connectivity diagnostics
push: true shell: bash
platforms: linux/amd64,linux/arm64 run: |
tags: ${{ steps.prep.outputs.tags }} set -x
uname -a || true
cat /etc/os-release || true
env | sort || true
getent hosts "$REGISTRY" || true
nslookup "$REGISTRY" || true
ping -c 1 "$REGISTRY" || true
curl -I --max-time 20 "https://$REGISTRY/v2/" || true
curl -v --max-time 20 "https://$REGISTRY/v2/" -o /dev/null || true
docker info || true
- name: Run tests
run: go test ./...
- name: Log in to Gitea Container Registry
shell: bash
run: |
set -euo pipefail
for attempt in 1 2 3; do
echo "docker login attempt $attempt"
if echo "${{ secrets.REGISTRY_PASSWORD }}" | docker login "$REGISTRY" -u "${{ secrets.REGISTRY_USERNAME }}" --password-stdin; then
exit 0
fi
sleep 5
done
echo "docker login failed after 3 attempts" >&2
exit 1
- name: Determine image tags
id: meta
shell: bash
env:
REF: ${{ gitea.ref }}
REF_NAME: ${{ gitea.ref_name }}
SHA: ${{ gitea.sha }}
run: |
set -euo pipefail
tags=()
if [[ "$REF" == refs/tags/* ]]; then
version="$REF_NAME"
tags+=("$IMAGE_NAME:$version")
tags+=("$IMAGE_NAME:latest")
else
branch="$REF_NAME"
branch_safe="$(echo "$branch" | tr '/_' '--')"
sha_short="$(echo "$SHA" | cut -c1-7)"
tags+=("$IMAGE_NAME:$branch_safe")
tags+=("$IMAGE_NAME:$branch_safe-$sha_short")
if [[ "$branch" == "main" ]]; then
tags+=("$IMAGE_NAME:latest")
fi
fi
printf 'tags<<EOF\n' >> "$GITHUB_OUTPUT"
printf '%s\n' "${tags[@]}" >> "$GITHUB_OUTPUT"
printf 'EOF\n' >> "$GITHUB_OUTPUT"
- name: Build image
shell: bash
run: |
set -euo pipefail
mapfile -t tags <<'EOF'
${{ steps.meta.outputs.tags }}
EOF
build_args=()
for tag in "${tags[@]}"; do
build_args+=(--tag "$tag")
done
docker build "${build_args[@]}" .
- name: Push image
shell: bash
run: |
set -euo pipefail
while IFS= read -r tag; do
[ -n "$tag" ] || continue
docker push "$tag"
done <<'EOF'
${{ steps.meta.outputs.tags }}
EOF