name: CI

on: [push, pull_request]

jobs:
  dotenv_lint:
    name: Dotenv Lint
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v2
      - name: Run dotenv-linter
        uses: dotenv-linter/action-dotenv-linter@v2
        with:
          github_token: ${{ secrets.github_token }}
          dotenv_linter_flags: --skip UnorderedKey

  markdown_lint:
    name: Markdown Lint
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v2
      - name: Run markdownlint
        uses: docker://avtodev/markdown-lint:v1
        with:
          args: './README.md'

  phpcs:
    name: PHPCS
    runs-on: ubuntu-latest
    container:
      image: baijunyao/php:8.1-local
      volumes:
        - /__w/laravel-bjyblog/laravel-bjyblog:/app
    steps:
      - name: Checkout
        uses: actions/checkout@v2
      - name: Get Composer Cache Directory
        id: composer-cache
        run: |
          echo "::set-output name=dir::$(composer config cache-files-dir)"
      - uses: actions/cache@v1
        with:
          path: ${{ steps.composer-cache.outputs.dir }}
          key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
          restore-keys: |
            ${{ runner.os }}-composer-
      - name: Initial
        run: |
          cp .env.actions .env
          composer install
      - name: PHP CS Fixer
        run: |
          ./vendor/bin/php-cs-fixer fix --verbose --show-progress=dots --dry-run --diff

  phpstan:
    name: PHPStan
    runs-on: ubuntu-latest
    container:
      image: baijunyao/php:8.1-local
      volumes:
        - /__w/laravel-bjyblog/laravel-bjyblog:/app
    steps:
      - name: Checkout
        uses: actions/checkout@v2
      - name: Get Composer Cache Directory
        id: composer-cache
        run: |
          echo "::set-output name=dir::$(composer config cache-files-dir)"
      - uses: actions/cache@v1
        with:
          path: ${{ steps.composer-cache.outputs.dir }}
          key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
          restore-keys: |
            ${{ runner.os }}-composer-
      - name: Initial
        run: |
          cp .env.actions .env
          composer install
      - name: PHPStan
        run: |
          ./vendor/bin/phpstan analyse --ansi

  helm_lint:
    name: Helm Lint
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v2
      - name: Set up Helm
        uses: azure/setup-helm@v1
      - name: Run Helm lint
        run: |
          cd deploy/helm
          helm lint

  ansible_lint:
    name: Ansible Lint
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v2
      - name: Run Ansible lint
        run: |
          pip3 install ansible
          pip3 install ansible-lint
          GITHUB_ACTIONS=true; ansible-lint -p deploy/ansible

  php_8_0_mysql_5_6:
    name: PHP8.0+MySQL5.6
    runs-on: ubuntu-latest
    services:
      mysql:
        image: mysql:5.6
        env:
          MYSQL_ROOT_PASSWORD: laravel_bjyblog
          MYSQL_DATABASE: laravel_bjyblog
    container:
      image: baijunyao/php:8.0-local
      volumes:
        - /__w/laravel-bjyblog/laravel-bjyblog:/app
    steps:
      - name: Checkout
        uses: actions/checkout@v2
      - name: Get Composer Cache Directory
        id: composer-cache
        run: |
          echo "::set-output name=dir::$(composer config cache-files-dir)"
      - uses: actions/cache@v1
        with:
          path: ${{ steps.composer-cache.outputs.dir }}
          key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
          restore-keys: |
            ${{ runner.os }}-composer-
      - name: Initial
        run: |
          cp .env.actions .env
          composer install
          php artisan key:generate
          php artisan passport:keys
          php artisan migrate
          php artisan db:seed
          php artisan storage:link --relative
      - name: Test
        run: |
          composer test

  php_8_0_mysql_5_7:
    name: PHP8.0+MySQL5.7
    runs-on: ubuntu-latest
    services:
      mysql:
        image: mysql:5.7
        env:
          MYSQL_ROOT_PASSWORD: laravel_bjyblog
          MYSQL_DATABASE: laravel_bjyblog
    container:
      image: baijunyao/php:8.0-local
      volumes:
        - /__w/laravel-bjyblog/laravel-bjyblog:/app
    steps:
      - name: Checkout
        uses: actions/checkout@v2
      - name: Get Composer Cache Directory
        id: composer-cache
        run: |
          echo "::set-output name=dir::$(composer config cache-files-dir)"
      - uses: actions/cache@v1
        with:
          path: ${{ steps.composer-cache.outputs.dir }}
          key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
          restore-keys: |
            ${{ runner.os }}-composer-
      - name: Initial
        run: |
          cp .env.actions .env
          composer install
          php artisan key:generate
          php artisan passport:keys
          php artisan migrate
          php artisan db:seed
          php artisan storage:link --relative
      - name: Test
        run: |
          composer test

  php_8_0_mysql_8_0:
    name: PHP8.0+MySQL8.0
    runs-on: ubuntu-latest
    services:
      mysql:
        image: baijunyao/mysql:8.0
        env:
          MYSQL_ROOT_PASSWORD: laravel_bjyblog
          MYSQL_DATABASE: laravel_bjyblog
    container:
      image: baijunyao/php:8.0-local
      volumes:
        - /__w/laravel-bjyblog/laravel-bjyblog:/app
    steps:
      - name: Checkout
        uses: actions/checkout@v2
      - name: Get Composer Cache Directory
        id: composer-cache
        run: |
          echo "::set-output name=dir::$(composer config cache-files-dir)"
      - uses: actions/cache@v1
        with:
          path: ${{ steps.composer-cache.outputs.dir }}
          key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
          restore-keys: |
            ${{ runner.os }}-composer-
      - name: Initial
        run: |
          cp .env.actions .env
          composer install
          php artisan key:generate
          php artisan passport:keys
          php artisan migrate
          php artisan db:seed
          php artisan storage:link --relative
      - name: Test
        run: |
          composer test:coverage-xml
      - name: Upload coverage to Codecov
        uses: codecov/codecov-action@v1
        with:
          files: storage/framework/testing/coverage.xml

  php_8_1_mysql_5_6:
    name: PHP8.1+MySQL5.6
    runs-on: ubuntu-latest
    services:
      mysql:
        image: mysql:5.6
        env:
          MYSQL_ROOT_PASSWORD: laravel_bjyblog
          MYSQL_DATABASE: laravel_bjyblog
    container:
      image: baijunyao/php:8.1-local
      volumes:
        - /__w/laravel-bjyblog/laravel-bjyblog:/app
    steps:
      - name: Checkout
        uses: actions/checkout@v2
      - name: Get Composer Cache Directory
        id: composer-cache
        run: |
          echo "::set-output name=dir::$(composer config cache-files-dir)"
      - uses: actions/cache@v1
        with:
          path: ${{ steps.composer-cache.outputs.dir }}
          key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
          restore-keys: |
            ${{ runner.os }}-composer-
      - name: Initial
        run: |
          cp .env.actions .env
          composer install
          php artisan key:generate
          php artisan passport:keys
          php artisan migrate
          php artisan db:seed
          php artisan storage:link --relative
      - name: Test
        run: |
          composer test

  php_8_1_mysql_5_7:
    name: PHP8.1+MySQL5.7
    runs-on: ubuntu-latest
    services:
      mysql:
        image: mysql:5.7
        env:
          MYSQL_ROOT_PASSWORD: laravel_bjyblog
          MYSQL_DATABASE: laravel_bjyblog
    container:
      image: baijunyao/php:8.1-local
      volumes:
        - /__w/laravel-bjyblog/laravel-bjyblog:/app
    steps:
      - name: Checkout
        uses: actions/checkout@v2
      - name: Get Composer Cache Directory
        id: composer-cache
        run: |
          echo "::set-output name=dir::$(composer config cache-files-dir)"
      - uses: actions/cache@v1
        with:
          path: ${{ steps.composer-cache.outputs.dir }}
          key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
          restore-keys: |
            ${{ runner.os }}-composer-
      - name: Initial
        run: |
          cp .env.actions .env
          composer install
          php artisan key:generate
          php artisan passport:keys
          php artisan migrate
          php artisan db:seed
          php artisan storage:link --relative
      - name: Test
        run: |
          composer test

  php_8_1_mysql_8_0:
    name: PHP8.1+MySQL8.0
    runs-on: ubuntu-latest
    services:
      mysql:
        image: baijunyao/mysql:8.0
        env:
          MYSQL_ROOT_PASSWORD: laravel_bjyblog
          MYSQL_DATABASE: laravel_bjyblog
    container:
      image: baijunyao/php:8.1-local
      volumes:
        - /__w/laravel-bjyblog/laravel-bjyblog:/app
    steps:
      - name: Checkout
        uses: actions/checkout@v2
      - name: Get Composer Cache Directory
        id: composer-cache
        run: |
          echo "::set-output name=dir::$(composer config cache-files-dir)"
      - uses: actions/cache@v1
        with:
          path: ${{ steps.composer-cache.outputs.dir }}
          key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
          restore-keys: |
            ${{ runner.os }}-composer-
      - name: Initial
        run: |
          cp .env.actions .env
          composer install
          php artisan key:generate
          php artisan passport:keys
          php artisan migrate
          php artisan db:seed
          php artisan storage:link --relative
      - name: Test
        run: |
          composer test:coverage-xml
      - name: Upload coverage to Codecov
        uses: codecov/codecov-action@v1
        with:
          files: storage/framework/testing/coverage.xml

  notify:
    name: Slack Notify
    runs-on: ubuntu-latest
    needs: [dotenv_lint, markdown_lint, helm_lint, ansible_lint, phpcs, phpstan, php_8_0_mysql_5_6, php_8_0_mysql_5_7, php_8_0_mysql_8_0, php_8_1_mysql_5_6, php_8_1_mysql_5_7, php_8_1_mysql_8_0]
    if: always()
    steps:
      - name: Slack Notify
        uses: baijunyao/action-slack-notify@v3.0.0
        if: ${{ github.event_name == 'push' }}
        with:
          slack_channel_id: CEWJP77BP
          slack_bot_token: ${{ secrets.SLACK_BOT_TOKEN }}
          github_context: ${{ toJson(github) }}
