From a2fa425853f27c58be1a7131728ba8b3bf3cfe19 Mon Sep 17 00:00:00 2001 From: Senko-san Date: Sun, 7 Jun 2026 21:11:50 +0300 Subject: [PATCH] feat: build --- .gitea/workflows/docker-publish.yml | 91 +++++++++++++++++++++++++++++ dockerfiles/Dockerfile.dev | 7 +-- dockerfiles/Dockerfile.prod | 2 +- 3 files changed, 93 insertions(+), 7 deletions(-) create mode 100644 .gitea/workflows/docker-publish.yml diff --git a/.gitea/workflows/docker-publish.yml b/.gitea/workflows/docker-publish.yml new file mode 100644 index 0000000..34d6f89 --- /dev/null +++ b/.gitea/workflows/docker-publish.yml @@ -0,0 +1,91 @@ +name: Docker Build & Publish + +on: + push: + branches: [master] + workflow_dispatch: + +env: + # Number of tagged (non-latest) versions to keep per image name. + KEEP_VERSIONS: "5" + +jobs: + build-and-push: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + + # Strip protocol and trailing slash from server_url to get the registry host. + - name: Resolve registry metadata + id: meta + run: | + host=$(echo "${{ gitea.server_url }}" | sed 's|https\?://||; s|/$||') + # Container registry requires lowercase image names. + repo_lc=$(echo "${{ gitea.repository }}" | tr '[:upper:]' '[:lower:]') + echo "host=$host" >> "$GITHUB_OUTPUT" + echo "image=$host/$repo_lc" >> "$GITHUB_OUTPUT" + echo "sha=$(git rev-parse --short HEAD)" >> "$GITHUB_OUTPUT" + + - name: Log in to Gitea registry + uses: docker/login-action@v3 + with: + registry: ${{ steps.meta.outputs.host }} + username: ${{ gitea.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Build and push + uses: docker/build-push-action@v5 + with: + context: . + file: dockerfiles/Dockerfile.prod + push: true + # PUBLIC_API_BASE_URL left as /api/v1 so any reverse proxy works. + build-args: | + PUBLIC_API_BASE_URL=/api/v1 + tags: | + ${{ steps.meta.outputs.image }}:latest + ${{ steps.meta.outputs.image }}:${{ steps.meta.outputs.sha }} + + cleanup: + name: Prune old image versions + needs: build-and-push + runs-on: ubuntu-latest + steps: + - name: Delete versions beyond KEEP_VERSIONS + env: + GITEA_URL: ${{ gitea.server_url }} + OWNER: ${{ gitea.repository_owner }} + IMAGE: ${{ gitea.event.repository.name }} + TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + image=$(echo "$IMAGE" | tr '[:upper:]' '[:lower:]') + + # List all container package versions for this image (page size 50 is + # enough for typical repos; increase if you push very frequently). + response=$(curl -sf \ + -H "Authorization: token $TOKEN" \ + -H "Accept: application/json" \ + "${GITEA_URL}/api/v1/packages/${OWNER}?type=container&limit=50&q=${image}") + + # Keep the KEEP_VERSIONS newest SHA-tagged versions; always preserve 'latest'. + to_delete=$(printf '%s' "$response" \ + | jq -r \ + --arg name "$image" \ + --argjson keep "$KEEP_VERSIONS" \ + '[.[] | select(.name == $name and .version != "latest")] + | sort_by(.created) | reverse + | .[$keep:][].version') + + if [ -z "$to_delete" ]; then + echo "Nothing to prune." + exit 0 + fi + + while IFS= read -r version; do + echo "Deleting ${image}:${version}" + curl -sf -X DELETE \ + -H "Authorization: token $TOKEN" \ + "${GITEA_URL}/api/v1/packages/${OWNER}/container/${image}/${version}" \ + && echo " ok" || echo " failed (may already be gone, continuing)" + done <<< "$to_delete" diff --git a/dockerfiles/Dockerfile.dev b/dockerfiles/Dockerfile.dev index 0bcfc12..aa029a5 100644 --- a/dockerfiles/Dockerfile.dev +++ b/dockerfiles/Dockerfile.dev @@ -4,14 +4,9 @@ # shadows the container install. Build context = mcma-webui/. FROM node:22-slim -# `modern-sk` is a git dependency (git+https://...) — npm needs git to fetch it. -RUN apt-get update \ - && apt-get install -y --no-install-recommends git \ - && rm -rf /var/lib/apt/lists/* - WORKDIR /app -COPY package.json package-lock.json modern-sk-*.tgz ./ +COPY package.json package-lock.json ./ RUN npm ci COPY . . diff --git a/dockerfiles/Dockerfile.prod b/dockerfiles/Dockerfile.prod index 1198b76..e79f572 100644 --- a/dockerfiles/Dockerfile.prod +++ b/dockerfiles/Dockerfile.prod @@ -8,7 +8,7 @@ FROM node:22-slim AS build WORKDIR /app -COPY package.json package-lock.json modern-sk-*.tgz ./ +COPY package.json package-lock.json ./ RUN npm ci COPY . .