kind: pipeline
type: docker
name: default

workspace:
    path: /drone/nami

steps:
    - name: submodules
      image: alpine/git
      commands:
          - git submodule update --init --recursive

    - name: composer_dev
      image: composer:2.7.9
      commands:
          - composer install --ignore-platform-reqs --dev

    - name: mysql_healthcheck
      image: mysql:oracle
      commands:
          - while ! mysqladmin ping -h db -u db -pdb --silent; do sleep 1; done

    - name: ocdb_healthcheck
      image: mysql:oracle
      commands:
          - while ! mysqladmin ping -h ownclouddb -u owncloud -powncloud --silent; do sleep 1; done

    - name: oc_healthcheck
      image: zoomyboy/adrema-base:latest
      commands:
          - while ! curl  --silent 'http://owncloudserver:8080/ocs/v1.php/cloud/capabilities?format=json' -u admin:admin | grep '"status":"ok"'; do sleep 1; done

    - name: node
      image: node:20.15.0-slim
      commands:
          - npm ci && cd packages/adrema-form && npm ci && npm run build && rm -R node_modules && cd ../../ && npm run img && npm run prod && rm -R node_modules

    - name: tests
      image: zoomyboy/adrema-base:latest
      commands:
          - touch .env
          - php artisan migrate
          - php artisan test
          - rm -f .env
          - vendor/bin/phpstan analyse
      environment:
          APP_NAME: Scoutrobot
          APP_KEY:
              from_secret: app_key
          APP_ENV: local
          APP_DEBUG: true
          APP_URL: http://scoutrobot.test
          LOG_CHANNEL: stack
          DB_CONNECTION: mysql
          DB_HOST: db
          REDIS_HOST: redis
          DB_PORT: 3306
          DB_DATABASE: db
          DB_USERNAME: db
          DB_PASSWORD: db
          BROADCAST_DRIVER: log
          CACHE_DRIVER: file
          QUEUE_CONNECTION: sync
          SESSION_DRIVER: file
          SESSION_LIFETIME: 120
          MAIL_FROM_NAME: '${APP_NAME}'
          PDFLATEX_BIN: /usr/bin/pdflatex
          XELATEX_BIN: /usr/bin/xelatex
          SCOUT_DRIVER: database
          MEILI_MASTER_KEY: abc
          TEST_OWNCLOUD_DOMAIN: http://owncloudserver:8080
          TEST_NEXTCLOUD_DOMAIN: http://nextcloudserver:80

    - name: docker_app_push
      image: plugins/docker
      settings:
          dockerfile: ./.docker/php.Dockerfile
          repo: zoomyboy/adrema-app
          username: zoomyboy
          password:
              from_secret: docker_hub_token
          tags:
              - latest
              - ${DRONE_TAG}
          pull_image: true
      when:
          event: tag

    - name: docker_webservice_push
      image: plugins/docker
      settings:
          dockerfile: ./.docker/nginx.Dockerfile
          repo: zoomyboy/adrema-webservice
          username: zoomyboy
          password:
              from_secret: docker_hub_token
          tags:
              - latest
              - ${DRONE_TAG}
          pull_image: true
      when:
          event: tag

    - name: deploy
      image: zoomyboy/adrema-base:latest
      environment:
          SSH_KEY:
              from_secret: deploy_private_key
      commands:
          - apt-get update > /dev/null
          - apt-get install -y openssh-client
          - mkdir $HOME/.ssh
          - echo "UserKnownHostsFile=/dev/null" >> $HOME/.ssh/config
          - echo "StrictHostKeyChecking=no" >> $HOME/.ssh/config
          - echo "$SSH_KEY" > $HOME/.ssh/id_rsa
          - chmod 600 $HOME/.ssh/id_rsa
          - ./vendor/bin/envoy run deploy
      when:
          event: tag

    - name: github push
      image: alpine/git
      environment:
          SSH_KEY:
              from_secret: github_private_key
      commands:
          - mkdir $HOME/.ssh
          - git config core.sshCommand "ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no"
          - echo "$SSH_KEY" > $HOME/.ssh/id_rsa
          - chmod 600 $HOME/.ssh/id_rsa
          - git remote add gh git@github.com:zoomyboy/adrema.git
          - git push -f gh HEAD:master
      when:
          branch: master
          event: push

    - name: composer_no_dev
      image: composer:2.7.9
      commands:
          - composer install --ignore-platform-reqs --no-dev

    - name: compress
      image: php:8.1.6
      commands:
          - apt-get update -yqq
          - apt-get install -yqq zip tar
          - cd .. && tar -cvzf nami.tar.gz ./nami && mv nami.tar.gz nami/ && cd nami
      when:
          event: tag

    - name: gitea_release
      image: plugins/gitea-release
      settings:
          api_key:
              from_secret: gitea_api_key
          base_url: https://git.zoomyboy.de
          files: ./nami.tar.gz
      when:
          event: tag

services:
    - name: db
      image: mariadb:10.6.5
      environment:
          MARIADB_DATABASE: db
          MARIADB_USER: db
          MARIADB_PASSWORD: db
          MARIADB_ALLOW_EMPTY_ROOT_PASSWORD: yes
    - name: redis
      image: redis
    - name: meilisearch
      image: getmeili/meilisearch:v1.6
      commands:
          - meilisearch --master-key="abc"
    - name: ownclouddb
      image: mariadb:10.11
      environment:
          MYSQL_ROOT_PASSWORD: owncloud
          MYSQL_USER: owncloud
          MYSQL_PASSWORD: owncloud
          MYSQL_DATABASE: owncloud
          MARIADB_AUTO_UPGRADE: 1
    - name: owncloudserver
      image: owncloud/server:10.10.0
      environment:
          OWNCLOUD_DOMAIN: http://owncloudserver:8080
          OWNCLOUD_TRUSTED_DOMAINS: owncloudserver
          OWNCLOUD_DB_TYPE: mysql
          OWNCLOUD_DB_NAME: owncloud
          OWNCLOUD_DB_USERNAME: owncloud
          OWNCLOUD_DB_PASSWORD: owncloud
          OWNCLOUD_DB_HOST: ownclouddb
          OWNCLOUD_ADMIN_USERNAME: admin
          OWNCLOUD_ADMIN_PASSWORD: admin
          OWNCLOUD_MYSQL_UTF8MB4: true
          OWNCLOUD_REDIS_ENABLED: false
          OWNCLOUD_REDIS_HOST: false
    - name: nextclouddb
      image: mariadb:10.11
      environment:
          MYSQL_ROOT_PASSWORD: nextcloud
          MYSQL_USER: nextcloud
          MYSQL_PASSWORD: nextcloud
          MYSQL_DATABASE: nextcloud
          MARIADB_AUTO_UPGRADE: 1
    - name: nextcloudserver
      image: nextcloud
      environment:
          MYSQL_PASSWORD: nextcloud
          MYSQL_DATABASE: nextcloud
          MYSQL_USER: nextcloud
          MYSQL_HOST: nextclouddb
          NEXTCLOUD_ADMIN_USER: admin
          NEXTCLOUD_ADMIN_PASSWORD: admin
          NEXTCLOUD_TRUSTED_DOMAINS: nextcloudserver

trigger:
    event:
        - push
        - tag