name: Build and Push Docker Image on: push: branches: - main tags: - '*' workflow_dispatch: jobs: build-and-push: runs-on: ubuntu-latest steps: - name: Install dependencies shell: sh run: | apk add --no-cache git buildah fuse-overlayfs - name: Checkout code shell: sh run: | git clone https://gitea.leeworks.dev/${{ gitea.repository }}.git . git checkout ${{ gitea.sha }} - name: Determine image tags id: tags shell: sh run: | REGISTRY="gitea.leeworks.dev" REPO_OWNER="${{ gitea.repository_owner }}" REPO_NAME="${{ gitea.repository }}" # Extract repository name without owner REPO_NAME_ONLY=$(echo "$REPO_NAME" | cut -d'/' -f2) # Convert to lowercase for Docker registry compatibility REPO_OWNER_LOWER=$(echo "$REPO_OWNER" | tr '[:upper:]' '[:lower:]') REPO_NAME_LOWER=$(echo "$REPO_NAME_ONLY" | tr '[:upper:]' '[:lower:]') # Base image path IMAGE_BASE="${REGISTRY}/${REPO_OWNER_LOWER}/${REPO_NAME_LOWER}" # Determine tag based on ref case "${{ gitea.ref }}" in refs/tags/*) # Tag push - use the tag name TAG_NAME="${{ gitea.ref_name }}" echo "IMAGE_TAG=${IMAGE_BASE}:${TAG_NAME}" >> $GITHUB_OUTPUT echo "PUSH_LATEST=true" >> $GITHUB_OUTPUT ;; refs/heads/main) # Main branch - use commit SHA (shortened to 7 chars) and latest SHORT_SHA=$(echo "${{ gitea.sha }}" | cut -c1-7) echo "IMAGE_TAG=${IMAGE_BASE}:${SHORT_SHA}" >> $GITHUB_OUTPUT echo "PUSH_LATEST=true" >> $GITHUB_OUTPUT ;; *) # Other branches - use branch name BRANCH_TAG=$(echo "${{ gitea.ref_name }}" | sed 's/\//-/g') echo "IMAGE_TAG=${IMAGE_BASE}:${BRANCH_TAG}" >> $GITHUB_OUTPUT echo "PUSH_LATEST=false" >> $GITHUB_OUTPUT ;; esac echo "IMAGE_LATEST=${IMAGE_BASE}:latest" >> $GITHUB_OUTPUT - name: Login to registry shell: sh run: | echo "${{ secrets.PERSONAL_TOKEN }}" | buildah login --username "${{ gitea.actor }}" --password-stdin gitea.leeworks.dev - name: Build and push with buildah shell: sh run: | echo "Building image with buildah..." buildah bud -t ${{ steps.tags.outputs.IMAGE_TAG }} . echo "Pushing image..." buildah push ${{ steps.tags.outputs.IMAGE_TAG }} if [ "${{ steps.tags.outputs.PUSH_LATEST }}" = "true" ]; then echo "Tagging and pushing latest..." buildah tag ${{ steps.tags.outputs.IMAGE_TAG }} ${{ steps.tags.outputs.IMAGE_LATEST }} buildah push ${{ steps.tags.outputs.IMAGE_LATEST }} fi echo "Build and push completed successfully!" echo "Image available at ${{ steps.tags.outputs.IMAGE_TAG }}"