From 0c78512445fe2b67ec31e333eb257537c0e82725 Mon Sep 17 00:00:00 2001 From: coding Date: Wed, 25 Mar 2026 09:20:29 +0000 Subject: [PATCH] Add optimized Gitea CI/CD workflows for Docker builds Features: - docker-autobuild.yaml: Automated builds on push to main/master - Path ignores for markdown/docs files - Docker Buildx with caching support - Automatic cleanup after builds - docker-build.yaml: Manual builds with flexible options - Custom version tags - Additional tags support (e.g., stable,production) - Toggle latest tag - Docker Buildx with layer caching Both workflows: - Use registry credentials from Gitea secrets - Support local registry at 192.168.88.201:5000 - Proper login/logout handling - Optimized for sticker-bot project --- .gitea/workflows/docker-autobuild.yaml | 63 ++++++++++++++++++ .gitea/workflows/docker-build.yaml | 90 ++++++++++++++++++++++++++ 2 files changed, 153 insertions(+) create mode 100644 .gitea/workflows/docker-autobuild.yaml create mode 100644 .gitea/workflows/docker-build.yaml diff --git a/.gitea/workflows/docker-autobuild.yaml b/.gitea/workflows/docker-autobuild.yaml new file mode 100644 index 0000000..2be37fd --- /dev/null +++ b/.gitea/workflows/docker-autobuild.yaml @@ -0,0 +1,63 @@ +name: Auto Build and Push Docker Image + +on: + push: + branches: + - main + - master + paths-ignore: + - '**.md' + - '.gitignore' + - 'data/**' + - '.env.example' + +jobs: + build: + runs-on: ubuntu-latest + steps: + - name: Checkout Repository + uses: actions/checkout@v4 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Log in to Docker Registry + run: | + echo "${{ secrets.REGISTRY_PASSWORD }}" | docker login -u "${{ secrets.REGISTRY_USERNAME }}" --password-stdin 192.168.88.201:5000 || true + + - name: Build and Push Docker Image + run: | + REGISTRY="192.168.88.201:5000" + IMAGE_NAME="${{ gitea.repository }}" + + # Versionierung: v{run_number}-autobuild + VERSION="v${{ gitea.run_number }}-autobuild" + FULL_IMAGE_PATH="$REGISTRY/$IMAGE_NAME" + + echo "========================================" + echo "Building Docker Image" + echo "Registry: $REGISTRY" + echo "Image: $IMAGE_NAME" + echo "Version: $VERSION" + echo "========================================" + + # Build with cache + docker build \ + --cache-from $FULL_IMAGE_PATH:latest \ + -t $FULL_IMAGE_PATH:latest \ + -t $FULL_IMAGE_PATH:$VERSION \ + . + + # Push images + docker push $FULL_IMAGE_PATH:latest + docker push $FULL_IMAGE_PATH:$VERSION + + echo "✅ Successfully built and pushed:" + echo " - $FULL_IMAGE_PATH:latest" + echo " - $FULL_IMAGE_PATH:$VERSION" + + - name: Cleanup + if: always() + run: | + docker logout 192.168.88.201:5000 || true + docker system prune -f || true diff --git a/.gitea/workflows/docker-build.yaml b/.gitea/workflows/docker-build.yaml new file mode 100644 index 0000000..419161c --- /dev/null +++ b/.gitea/workflows/docker-build.yaml @@ -0,0 +1,90 @@ +name: Manual Build and Push Docker Image + +on: + workflow_dispatch: + inputs: + tag_name: + description: 'Version tag (e.g., 1.0.0, v1.0.0). Leave empty for auto-generated v{run_number}.' + required: false + default: '' + additional_tags: + description: 'Additional tags (comma-separated, e.g., stable,production)' + required: false + default: '' + push_latest: + description: 'Also push as latest?' + required: true + default: 'true' + type: boolean + +jobs: + build: + runs-on: ubuntu-latest + steps: + - name: Checkout Repository + uses: actions/checkout@v4 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Log in to Docker Registry + run: | + echo "${{ secrets.REGISTRY_PASSWORD }}" | docker login -u "${{ secrets.REGISTRY_USERNAME }}" --password-stdin 192.168.88.201:5000 || true + + - name: Build and Push Docker Image + run: | + REGISTRY="192.168.88.201:5000" + IMAGE_NAME="${{ gitea.repository }}" + FULL_IMAGE_PATH="$REGISTRY/$IMAGE_NAME" + + # Determine version + if [ -z "${{ github.event.inputs.tag_name }}" ]; then + VERSION="v${{ gitea.run_number }}" + else + VERSION="${{ github.event.inputs.tag_name }}" + fi + + echo "========================================" + echo "Building Docker Image (Manual)" + echo "Registry: $REGISTRY" + echo "Image: $IMAGE_NAME" + echo "Version: $VERSION" + echo "========================================" + + # Build with cache + docker build \ + --cache-from $FULL_IMAGE_PATH:latest \ + -t $FULL_IMAGE_PATH:$VERSION \ + . + + # Push versioned tag + docker push $FULL_IMAGE_PATH:$VERSION + echo "✅ Pushed: $FULL_IMAGE_PATH:$VERSION" + + # Push latest if enabled + if [ "${{ github.event.inputs.push_latest }}" = "true" ]; then + docker tag $FULL_IMAGE_PATH:$VERSION $FULL_IMAGE_PATH:latest + docker push $FULL_IMAGE_PATH:latest + echo "✅ Pushed: $FULL_IMAGE_PATH:latest" + fi + + # Push additional tags + if [ -n "${{ github.event.inputs.additional_tags }}" ]; then + IFS=',' read -ra TAGS <<< "${{ github.event.inputs.additional_tags }}" + for tag in "${TAGS[@]}"; do + tag=$(echo $tag | xargs) # trim whitespace + docker tag $FULL_IMAGE_PATH:$VERSION $FULL_IMAGE_PATH:$tag + docker push $FULL_IMAGE_PATH:$tag + echo "✅ Pushed: $FULL_IMAGE_PATH:$tag" + done + fi + + echo "========================================" + echo "Build completed successfully!" + echo "========================================" + + - name: Cleanup + if: always() + run: | + docker logout 192.168.88.201:5000 || true + docker system prune -f || true