name: Build and Push Docker Image on: push: branches: [ main, master ] tags: [ 'v*', 'release-*' ] workflow_dispatch: {} env: DOCKERFILE: Dockerfile jobs: docker: runs-on: docker steps: - name: Checkout uses: actions/checkout@v4 - name: Set up Docker Buildx uses: docker/setup-buildx-action@v3 - name: Define Registry Variables id: vars run: | # Derive registry/namespace/image from Gitea context, normalize to lowercase OWNER="${{ gitea.repository_owner }}" REPO="${{ gitea.repository_name }}" OWNER=$(echo "$OWNER" | tr '[:upper:]' '[:lower:]') REPO=$(echo "$REPO" | tr '[:upper:]' '[:lower:]') # Allow overriding image name via repository variable IMAGE_NAME; default to repo name IMAGE_NAME="${{ vars.IMAGE_NAME }}" if [ -z "$IMAGE_NAME" ]; then IMAGE_NAME="$REPO"; fi IMAGE_NAME=$(echo "$IMAGE_NAME" | tr '[:upper:]' '[:lower:]') # Gitea exposes server URL like https://gitea.example.com # Prefer explicit var GITEA_REGISTRY; else, use the same host as Gitea server if [ -n "${{ vars.GITEA_REGISTRY }}" ]; then REGISTRY="${{ vars.GITEA_REGISTRY }}" else REGISTRY=$(echo "${{ gitea.server_url }}" | sed -E 's#^https?://##; s#/$##') fi echo "registry=$REGISTRY" >> "$GITHUB_OUTPUT" echo "owner=$OWNER" >> "$GITHUB_OUTPUT" echo "image=$IMAGE_NAME" >> "$GITHUB_OUTPUT" - name: Log in to Gitea Registry uses: docker/login-action@v3 with: registry: ${{ steps.vars.outputs.registry }} username: ${{ gitea.actor }} password: ${{ secrets.GITEA_TOKEN }} - name: Extract metadata (tags, labels) id: meta uses: docker/metadata-action@v5 with: images: | ${{ steps.vars.outputs.registry }}/${{ steps.vars.outputs.owner }}/${{ steps.vars.outputs.image }} tags: | type=raw,value=latest,enable=${{ gitea.ref_type == 'branch' && (gitea.ref == 'refs/heads/main' || gitea.ref == 'refs/heads/master') }} type=ref,event=branch type=ref,event=tag type=sha,format=short - name: Build and push uses: docker/build-push-action@v6 with: context: . file: ${{ env.DOCKERFILE }} platforms: linux/amd64 push: true tags: ${{ steps.meta.outputs.tags }} labels: ${{ steps.meta.outputs.labels }} cache-from: type=registry,ref=${{ steps.vars.outputs.registry }}/${{ steps.vars.outputs.owner }}/${{ steps.vars.outputs.image }}:buildcache cache-to: type=registry,ref=${{ steps.vars.outputs.registry }}/${{ steps.vars.outputs.owner }}/${{ steps.vars.outputs.image }}:buildcache,mode=max