# kind: pipeline
# type: docker
# name: frontend-deployment

steps:
    - name: build
      image: node:22
      commands:
          - npm ci
          - npm run build

    - name: ssh-copy-to-host
      image: appleboy/drone-scp
      settings:
          host:
              from_secret: ssh_host
          username:
              from_secret: ssh_username
          key:
              from_secret: ssh_key
          port: 2332
          target: /home/mnisyif/docker-containers/mnisyif/frontend
          source:
              - dist/
              - Dockerfile
              - nginx.conf
              - version.txt

    - name: webserver-docker-deploy
      image: appleboy/drone-ssh
      settings:
          host:
              from_secret: ssh_host
          username:
              from_secret: ssh_username
          key:
              from_secret: ssh_key
          port: 2332
          script:
              - cd /home/mnisyif/docker-containers/mnisyif/frontend
              - VERSION=$(cat version.txt)
              - docker build -t frontend:$VERSION .
              - docker stop frontend || true
              - docker rm frontend || true
              - docker run -d --name frontend -p 5173:80 frontend:$VERSION
              - docker image prune -f
              - docker system prune -f --volumes
              - >
                  for img in $(docker images frontend --format "{{.Tag}}" | grep -v $VERSION); do
                    docker rmi frontend:$img;
                  done

trigger:
    event:
        - push
    branch:
        - master
