version: 0.1
component: build
timeoutInSeconds: 1800
runAs: root
shell: bash

env:
  variables:
    REGISTRY: "icn.ocir.io/idyhsdamac8c/tasteby"
  exportedVariables:
    - IMAGE_TAG
    - BACKEND_IMAGE
    - FRONTEND_IMAGE

steps:
  - type: Command
    name: "Setup buildx for ARM64"
    command: |
      docker run --rm --privileged multiarch/qemu-user-static --reset -p yes
      docker buildx create --name armbuilder --use
      docker buildx inspect --bootstrap

  - type: Command
    name: "Set image tag"
    command: |
      IMAGE_TAG="${OCI_BUILD_RUN_ID:0:8}-$(date +%Y%m%d%H%M)"
      BACKEND_IMAGE="${REGISTRY}/backend:${IMAGE_TAG}"
      FRONTEND_IMAGE="${REGISTRY}/frontend:${IMAGE_TAG}"
      echo "IMAGE_TAG=${IMAGE_TAG}"
      echo "BACKEND_IMAGE=${BACKEND_IMAGE}"
      echo "FRONTEND_IMAGE=${FRONTEND_IMAGE}"

  - type: Command
    name: "Build backend image"
    command: |
      cd backend-java
      docker buildx build --platform linux/arm64 \
        -t "${BACKEND_IMAGE}" \
        -t "${REGISTRY}/backend:latest" \
        --load \
        .

  - type: Command
    name: "Build frontend image"
    command: |
      cd frontend
      docker buildx build --platform linux/arm64 \
        --build-arg NEXT_PUBLIC_GOOGLE_MAPS_API_KEY="${NEXT_PUBLIC_GOOGLE_MAPS_API_KEY}" \
        --build-arg NEXT_PUBLIC_GOOGLE_CLIENT_ID="${NEXT_PUBLIC_GOOGLE_CLIENT_ID}" \
        -t "${FRONTEND_IMAGE}" \
        -t "${REGISTRY}/frontend:latest" \
        --load \
        .

outputArtifacts:
  - name: backend-image
    type: DOCKER_IMAGE
    location: ${BACKEND_IMAGE}
  - name: frontend-image
    type: DOCKER_IMAGE
    location: ${FRONTEND_IMAGE}
