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.2.7
      commands:
          - composer install --ignore-platform-reqs --dev

    - name: mysql_healthcheck
      image: mariadb/server:10.3
      commands:
          - while ! mysqladmin ping -h db -u db -pdb --silent; do sleep 1; done

    - name: node
      image: node:17.9.0-slim
      commands:
          - npm ci && npm run img && npm run prod && rm -R node_modules

    - name: phpunit_tests
      image: php:8.1.6
      commands:
          - apt-get update && apt-get install -y libcurl3-dev apt-utils zlib1g-dev libpng-dev libicu-dev libonig-dev rsync texlive-base texlive-xetex texlive-lang-german texlive-fonts-extra
          - docker-php-ext-install pdo_mysql curl gd intl mbstring
          - pecl install redis && docker-php-ext-enable redis
          - cp .docker/fonts/* /usr/share/fonts/
          - php artisan migrate
          - php artisan test
          - rm -f .env
      environment:
          APP_NAME: Scoutrobot
          APP_KEY:
              from_secret: app_key
          APP_ENV: local
          APP_DEBUG: true
          APP_URL: http://scoutrobot.test
          APP_MODE: stamm
          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

    - name: phpstan_tests
      image: php:8.1.6
      commands:
          - apt-get update && apt-get install -y libcurl3-dev apt-utils zlib1g-dev libpng-dev libicu-dev libonig-dev rsync
          - pecl install redis && docker-php-ext-enable redis
          - vendor/bin/phpstan analyse --memory-limit=2G

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

    - name: compress
      image: php:7.3
      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

    - name: deploy silva
      image: drillster/drone-rsync
      settings:
          hosts: ['zoomyboy.de']
          user: stammsilva
          source: ./
          target: ~/nami
          exclude: ['.git']
          key:
              from_secret: deploy_private_key
          script:
              - cd ~/nami
              - /usr/bin/php8.1 artisan migrate --force
      when:
          branch: master
          event: push

    - name: deploy lennep
      image: drillster/drone-rsync
      settings:
          hosts: ['zoomyboy.de']
          user: dpsg-lennep
          source: ./
          target: ~/nami
          exclude: ['.git']
          key:
              from_secret: deploy_private_key
          script:
              - cd ~/nami
              - /usr/bin/php8.1 artisan migrate --force
      when:
          branch: master
          event: push

    - name: deploy dpsgkoeln
      image: drillster/drone-rsync
      settings:
          hosts: ['dpsg-koeln.de']
          user: dpsg-koeln
          source: ./
          target: ~/adrema
          exclude: ['.git']
          key:
              from_secret: deploy_private_key
          script:
              - cd ~/adrema
              - /usr/bin/php8.1 artisan migrate --force
              - sudo systemctl restart adrema-horizon
      when:
          branch: master
          event: push

    - 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"
          - 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

services:
    - name: db
      image: mariadb/server:10.3
      environment:
          MARIADB_DATABASE: db
          MARIADB_USER: db
          MARIADB_PASSWORD: db
          MARIADB_ALLOW_EMPTY_PASSWORD: yes
    - name: redis
      image: redis

trigger:
    event:
        - push
        - tag
