steps:
  # Set up Docker Buildx
  - name: "gcr.io/cloud-builders/docker"
    entrypoint: "bash"
    args:
      - "-c"
      - |
        docker buildx create --name mybuilder --use
        docker buildx inspect --bootstrap

  # Build and push image with branch name tag
  - name: "gcr.io/cloud-builders/docker"
    id: "Build and Push (Branch)"
    entrypoint: "bash"
    args:
      - "-c"
      - |
        docker buildx build \
          --platform linux/amd64 \
          -t us-docker.pkg.dev/$PROJECT_ID/nebulous/server:$BRANCH_NAME \
          --cache-from type=registry,ref=us-docker.pkg.dev/$PROJECT_ID/nebulous/server:buildcache \
          --cache-to type=registry,ref=us-docker.pkg.dev/$PROJECT_ID/nebulous/server:buildcache,mode=max \
          --push \
          .

        # Only tag as latest if on main branch
        if [ "$BRANCH_NAME" == "main" ]; then
          docker buildx build \
            --platform linux/amd64 \
            -t us-docker.pkg.dev/$PROJECT_ID/nebulous/server:latest \
            --cache-from type=registry,ref=us-docker.pkg.dev/$PROJECT_ID/nebulous/server:buildcache \
            --push \
            .
        fi
        
  # Conditionally push a tag if triggered by a tag event
  - name: "gcr.io/cloud-builders/docker"
    id: "Build and Push (Tag)"
    entrypoint: "bash"
    args:
      - "-c"
      - |
        if [ -n "$TAG_NAME" ]; then
          echo "Detected tag: $TAG_NAME. Building with tag."
          docker buildx build \
            --platform linux/amd64 \
            -t us-docker.pkg.dev/$PROJECT_ID/nebulous/server:$TAG_NAME \
            --cache-from type=registry,ref=us-docker.pkg.dev/$PROJECT_ID/nebulous/server:buildcache \
            --cache-to type=registry,ref=us-docker.pkg.dev/$PROJECT_ID/nebulous/server:buildcache,mode=max \
            --push \
            .
        else
          echo "No TAG_NAME detected. Skipping tag push step."
        fi

timeout: "3600s"

options:
  machineType: "N1_HIGHCPU_8"
  env:
    - DOCKER_BUILDKIT=1