name: Build

on:
  schedule:
    - cron: "0 9 * * *"
  pull_request:
    types: [opened, synchronize, reopened]
  push:
    branches:
      - master
      - "*"
      - "!gh-pages"

jobs:
  build_and_test:
    strategy:
      fail-fast: false
      matrix:
        version:
          - stable

    name: ${{ matrix.version }} - x86_64-unknown-linux-gnu
    runs-on: ubuntu-latest

    services:
      postgres:
        image: postgres
        env:
          POSTGRES_PASSWORD: password
          POSTGRES_USER: postgres
          POSTGRES_DB: postgres
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
        ports:
          - 5432:5432

      mcaptcha-redis:
        image: mcaptcha/cache
        ports:
          - 6379:6379
      mcaptcha-smtp:
        image: maildev/maildev
        env:
          MAILDEV_WEB_PORT: "1080"
          MAILDEV_INCOMING_USER: "admin"
          MAILDEV_INCOMING_PASS: "password"
        ports:
          - 1080:1080
          - 10025:1025

      maria:
        image: mariadb:10
        env:
          MARIADB_USER: "maria"
          MARIADB_PASSWORD: "password"
          MARIADB_ROOT_PASSWORD: "password"
          MARIADB_DATABASE: "maria"
        options: >-
          --health-cmd="mysqladmin ping"
          --health-interval=10s
          --health-timeout=5s
          --health-retries=10
        ports:
          - 3306:3306

    steps:
      - uses: actions/checkout@v4


      - name: configure GPG key
        if: (github.ref == 'refs/heads/master' || github.event_name == 'push') && github.repository == 'mCaptcha/mCaptcha'
        run: echo -n "$RELEASE_BOT_GPG_SIGNING_KEY" | gpg --batch --import --pinentry-mode loopback
        env:
          RELEASE_BOT_GPG_SIGNING_KEY: ${{ secrets.RELEASE_BOT_GPG_SIGNING_KEY }}

      - name: load env
        run: |
          source .env_sample \
            && echo "POSTGRES_DATABASE_URL=$POSTGRES_DATABASE_URL" >> $GITHUB_ENV \
            && echo "MARIA_DATABASE_URL=$MARIA_DATABASE_URL" >> $GITHUB_ENV

      - uses: actions/setup-node@v2
        with:
          node-version: "20.0.0"

      - uses: actions-rust-lang/setup-rust-toolchain@v1

      - name: install nightwatch dep
        run: sudo apt-get install xvfb

      - name: Run migrations
        run: make migrate
        env:
          POSTGRES_DATABASE_URL: "${{ env.POSTGRES_DATABASE_URL }}"
          MARIA_DATABASE_URL: "${{ env.MARIA_DATABASE_URL }}"

      - name: build
        run: make
        env:
          POSTGRES_DATABASE_URL: "${{ env.POSTGRES_DATABASE_URL }}"
          MARIA_DATABASE_URL: "${{ env.MARIA_DATABASE_URL }}"

            #      - name: build frontend
            #        run: make frontend
            #
      - name: lint frontend
        run: yarn lint

      - name: run tests
        run: make test
        env:
          POSTGRES_DATABASE_URL: "${{ env.POSTGRES_DATABASE_URL }}"
          MARIA_DATABASE_URL: "${{ env.MARIA_DATABASE_URL }}"

      - name: run integration tests
        run: make test.integration

      - name: Login to DockerHub
        if: github.ref == 'refs/heads/master' && github.event_name == 'push' && github.repository == 'mCaptcha/mCaptcha'
        uses: docker/login-action@v1
        with:
          username: mcaptcha
          password: ${{ secrets.DOCKERHUB_TOKEN }}

      - name: publish docker images
        if: (github.ref == 'refs/heads/master' || github.event_name == 'push') && github.repository == 'mCaptcha/mCaptcha'
        run: make docker-publish

      - name: publish bins
        if: (github.ref == 'refs/heads/master' || github.event_name == 'push') && github.repository == 'mCaptcha/mCaptcha'
        run: ./scripts/publish.sh publish master latest $DUMBSERVE_PASSWORD
        env:
          DUMBSERVE_PASSWORD: ${{ secrets.DUMBSERVE_PASSWORD }}
          GPG_PASSWORD: ${{ secrets.GPG_PASSWORD }}

      - name: generate documentation
        if: matrix.version == 'stable' && (github.repository == 'mCaptcha/mCaptcha')
        run: make doc
        env:
          POSTGRES_DATABASE_URL: "${{ env.POSTGRES_DATABASE_URL }}"
          MARIA_DATABASE_URL: "${{ env.MARIA_DATABASE_URL }}"
          GIT_HASH: 8e77345f1597e40c2e266cb4e6dee74888918a61 # dummy value
          COMPILED_DATE: "2021-07-21"

      - name: Deploy to GitHub Pages
        if: matrix.version == 'stable' && (github.repository == 'mCaptcha/mCaptcha')
        uses: JamesIves/github-pages-deploy-action@3.7.1
        with:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          BRANCH: gh-pages
          FOLDER: target/doc
