name: Docker Build and Push

on:
  push:
    tags:
      - 'v*'

jobs:
  test:
    name: Run Tests
    runs-on: ubuntu-latest

    steps:
      - name: Checkout repository
        uses: actions/checkout@v6

      - name: Set up Go
        uses: actions/setup-go@v6
        with:
          go-version: 'stable'
          cache: false

      - name: Install dependencies
        run: go mod download

      - name: Run go vet
        run: go vet ./...

      - name: Run tests
        run: go test -v -p 4 ./...


  build-and-push:
    name: Build and Push Docker Image
    runs-on: ubuntu-latest
    needs: test

    steps:
      - name: Checkout repository
        uses: actions/checkout@v6

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v4

      - name: Log in to Docker Registry
        uses: docker/login-action@v4
        with:
          registry: git.fossy.my.id
          username: ${{ secrets.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_PASSWORD }}

      - name: Extract version and determine release type
        id: version
        run: |
          VERSION=${GITHUB_REF#refs/tags/v}
          echo "VERSION=$VERSION" >> $GITHUB_OUTPUT
          echo "BUILD_DATE=$(date -u +'%Y-%m-%dT%H:%M:%SZ')" >> $GITHUB_OUTPUT
          echo "COMMIT=${{ github.sha }}" >> $GITHUB_OUTPUT
          
          if echo "$VERSION" | grep -qE '^[0-9]+\.[0-9]+\.[0-9]+(-[a-zA-Z0-9.]+)?$'; then
            MAJOR=$(echo "$VERSION" | cut -d. -f1)
            MINOR=$(echo "$VERSION" | cut -d. -f2)
            PATCH=$(echo "$VERSION" | cut -d. -f3 | cut -d- -f1)
          
            echo "MAJOR=$MAJOR" >> $GITHUB_OUTPUT
            echo "MINOR=$MINOR" >> $GITHUB_OUTPUT
            echo "PATCH=$PATCH" >> $GITHUB_OUTPUT
          
            if echo "$VERSION" | grep -q '-'; then
              PRERELEASE_TAG=$(echo "$VERSION" | cut -d- -f2 | cut -d. -f1)
              echo "IS_PRERELEASE=true" >> $GITHUB_OUTPUT
              echo "PRERELEASE_TAG=$PRERELEASE_TAG" >> $GITHUB_OUTPUT
            else
              echo "IS_PRERELEASE=false" >> $GITHUB_OUTPUT
            fi
          else
            echo "Invalid version format: $VERSION"
            exit 1
          fi

      - name: Build and push Docker image (release)
        uses: docker/build-push-action@v7
        with:
          context: .
          push: true
          tags: |
            git.fossy.my.id/${{ secrets.DOCKER_USERNAME }}/tunnel-please:v${{ steps.version.outputs.VERSION }}
            git.fossy.my.id/${{ secrets.DOCKER_USERNAME }}/tunnel-please:release
            git.fossy.my.id/${{ secrets.DOCKER_USERNAME }}/tunnel-please:v${{ steps.version.outputs.MAJOR }}.${{ steps.version.outputs.MINOR }}
            git.fossy.my.id/${{ secrets.DOCKER_USERNAME }}/tunnel-please:v${{ steps.version.outputs.MAJOR }}
            git.fossy.my.id/${{ secrets.DOCKER_USERNAME }}/tunnel-please:latest
          platforms: linux/amd64,linux/arm64
          build-args: |
            VERSION=${{ steps.version.outputs.VERSION }}
            BUILD_DATE=${{ steps.version.outputs.BUILD_DATE }}
            COMMIT=${{ steps.version.outputs.COMMIT }}
        if: steps.version.outputs.IS_PRERELEASE == 'false'

      - name: Build and push Docker image (pre-release)
        uses: docker/build-push-action@v7
        with:
          context: .
          push: true
          tags: |
            git.fossy.my.id/${{ secrets.DOCKER_USERNAME }}/tunnel-please:v${{ steps.version.outputs.VERSION }}
            git.fossy.my.id/${{ secrets.DOCKER_USERNAME }}/tunnel-please:staging
          platforms: linux/amd64,linux/arm64
          build-args: |
            VERSION=${{ steps.version.outputs.VERSION }}
            BUILD_DATE=${{ steps.version.outputs.BUILD_DATE }}
            COMMIT=${{ steps.version.outputs.COMMIT }}
        if: steps.version.outputs.IS_PRERELEASE == 'true'