name: 'Divlo'

on:
  push:
    branches: [master, develop]
  pull_request:
    branches: [master, develop]

jobs:
  analyze:
    runs-on: 'ubuntu-latest'

    strategy:
      fail-fast: false
      matrix:
        language: ['javascript']

    steps:
      - uses: 'actions/checkout@v2.4.0'

      - name: 'Initialize CodeQL'
        uses: 'github/codeql-action/init@v1'
        with:
          languages: ${{ matrix.language }}

      - name: 'Perform CodeQL Analysis'
        uses: 'github/codeql-action/analyze@v1'

  build:
    runs-on: 'ubuntu-latest'
    steps:
      - uses: 'actions/checkout@v2.4.0'

      - name: 'Use Node.js'
        uses: 'actions/setup-node@v2.5.1'
        with:
          node-version: '16.x'
          cache: 'npm'

      - name: 'Install'
        run: 'npm install'

      - name: 'Build'
        run: 'npm run build'

  lint:
    runs-on: 'ubuntu-latest'
    steps:
      - uses: 'actions/checkout@v2.4.0'

      - name: 'Use Node.js'
        uses: 'actions/setup-node@v2.5.1'
        with:
          node-version: '16.x'
          cache: 'npm'

      - name: 'Install'
        run: 'npm install'

      - name: 'lint:commit'
        run: 'npm run lint:commit -- --to "${{ github.sha }}"'

      - name: 'lint:editorconfig'
        run: 'npm run lint:editorconfig'

      - name: 'lint:markdown'
        run: 'npm run lint:markdown'

      - name: 'lint:typescript'
        run: 'npm run lint:typescript'

      - name: 'lint:prettier'
        run: 'npm run lint:prettier'

      - name: 'resume:validate'
        run: 'npm run resume:validate'

      - name: 'lint:dotenv'
        uses: 'dotenv-linter/action-dotenv-linter@v2'
        with:
          github_token: ${{ secrets.github_token }}

      - name: 'lint:docker'
        uses: 'hadolint/hadolint-action@v1.6.0'
        with:
          dockerfile: './Dockerfile'

  test-unit:
    runs-on: 'ubuntu-latest'
    steps:
      - uses: 'actions/checkout@v2.4.0'

      - name: 'Use Node.js'
        uses: 'actions/setup-node@v2.5.1'
        with:
          node-version: '16.x'
          cache: 'npm'

      - name: 'Install'
        run: 'npm install'

      - name: 'Unit Test'
        run: 'npm run test:unit'

  test-lighthouse:
    runs-on: 'ubuntu-latest'
    steps:
      - uses: 'actions/checkout@v2.4.0'

      - name: 'Use Node.js'
        uses: 'actions/setup-node@v2.5.1'
        with:
          node-version: '16.x'
          cache: 'npm'

      - name: 'Install'
        run: 'npm install'

      - name: 'Build'
        run: 'npm run build'

      - name: 'html-w3c-validator'
        run: 'npm run test:html-w3c-validator'

      - name: 'Lighthouse'
        run: 'npm run test:lighthouse'
        env:
          LHCI_GITHUB_APP_TOKEN: ${{ secrets.LHCI_GITHUB_APP_TOKEN }}

  test-e2e:
    runs-on: 'ubuntu-latest'
    steps:
      - uses: 'actions/checkout@v2.4.0'

      - name: 'Use Node.js'
        uses: 'actions/setup-node@v2.5.1'
        with:
          node-version: '16.x'
          cache: 'npm'

      - name: 'Install'
        run: 'npm install'

      - name: 'Build'
        run: 'npm run build'

      - name: 'End To End (e2e) Test'
        run: 'npm run test:e2e'

  release:
    if: github.ref == 'refs/heads/master' && github.event_name == 'push'
    needs: [analyze, build, lint, test-unit, test-lighthouse, test-e2e]
    runs-on: 'ubuntu-latest'
    steps:
      - uses: 'actions/checkout@v2.4.0'
        with:
          fetch-depth: 0
          persist-credentials: false

      - name: 'Import GPG key'
        uses: 'crazy-max/ghaction-import-gpg@v4'
        with:
          gpg_private_key: ${{ secrets.GPG_PRIVATE_KEY }}
          git_user_signingkey: true
          git_commit_gpgsign: true

      - name: 'Use Node.js'
        uses: 'actions/setup-node@v2.5.1'
        with:
          node-version: '16.x'
          cache: 'npm'

      - name: 'Install'
        run: 'npm install'

      - name: 'Release'
        run: 'npm run release'
        env:
          GH_TOKEN: ${{ secrets.GH_TOKEN }}
          GIT_COMMITTER_NAME: ${{ secrets.GIT_NAME }}
          GIT_COMMITTER_EMAIL: ${{ secrets.GIT_EMAIL }}

      - name: 'Deploy to Vercel'
        run: 'npm run deploy -- --token="${VERCEL_TOKEN}" --prod'
        env:
          VERCEL_TOKEN: ${{ secrets.VERCEL_TOKEN }}
          VERCEL_PROJECT_ID: ${{ secrets.VERCEL_PROJECT_ID }}
          VERCEL_ORG_ID: ${{ secrets.VERCEL_ORG_ID }}
