kind: pipeline
type: docker
name: build-and-push-panel

steps:
  - name: build node app
    image: cypress/included:14.5.4
    commands:
      - |
        set -e
        SHORT_SHA=$(echo "$DRONE_COMMIT_SHA" | cut -c1-8)
        IMG_TAG="${DRONE_BUILD_NUMBER}-${DRONE_BRANCH}"
        echo -n "$IMG_TAG,$SHORT_SHA" > .tags
        echo "Image tags -> $IMG_TAG , $SHORT_SHA"
      - corepack enable
      - corepack prepare pnpm@10.18.0 --activate
      - mkdir -p ./artifacts/node
      - rm -rf .git dist
      - pnpm install --frozen-lockfile --reporter=append-only
      - pnpm run build
      - cp -R .next/standalone/. ./artifacts/node/
      - ls -ltrha ./artifacts/node

  - name: push panel
    image: plugins/docker
    settings:
      registry: git.opplens.se
      repo: git.opplens.se/mazyar/tm-panel
      username:
        from_secret: registry_username
      password:
        from_secret: registry_password

  - name: deploy-uat
    image: appleboy/drone-ssh
    settings:
      host: 10.0.0.3
      username: mazyar
      port: 22
      key:
        from_secret: ssh_deploy_key
      envs:
        - DRONE_BUILD_NUMBER
        - DRONE_BRANCH
      script:
        - set -e
        - cd /data/tm-uat
        - NEW_TAG="${DRONE_BUILD_NUMBER}-${DRONE_BRANCH}"
        - echo "Deploying panel tag $NEW_TAG"
        - sed -i "s/^PANEL_IMG_TAG=.*/PANEL_IMG_TAG=$NEW_TAG/" .env
        - docker compose pull panel
        - docker compose up -d panel
        - docker image prune -f
    when:
      branch:
        - dev

trigger:
  branch:
    - main
    - uat
    - dev
  event:
    - push
