name: Deploy Website

on:
  push:
    branches:
      - main

jobs:
  build-detekt-docs:
    if: github.repository == 'detekt/detekt'
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Repo
        uses: actions/checkout@v2

      - name: Setup Java
        uses: actions/setup-java@v2
        with:
          java-version: 17
          distribution: 'temurin'

      - name: Build Detekt Documentation
        uses: gradle/gradle-build-action@v2
        with:
          arguments: :detekt-generator:generateDocumentation

      - name: Upload Generated Rules documentation
        uses: actions/upload-artifact@v2
        with:
          name: detekt-generated-docs
          path: docs/pages/documentation/

      - name: Upload KDoc documentation
        uses: actions/upload-artifact@v2
        with:
          name: detekt-kdoc
          path: docs/pages/kdoc/

      - name: Upload generated CLI options documentation
        uses: actions/upload-artifact@v2
        with:
          name: detekt-cli-options-docs
          path: docs/pages/gettingstarted/cli-options.md

  build-website:
    needs: build-detekt-docs
    runs-on: ubuntu-latest
    container: jekyll/builder
    steps:
    - name: Checkout Repo
      uses: actions/checkout@v2

    - uses: actions/cache@v2
      with:
        path: docs/vendor/bundle
        key: cache-gems-${{ hashFiles('**/Gemfile.lock') }}
        restore-keys: cache-gems-

    - name: Unzipping Generated Rules documentation
      uses: actions/download-artifact@v2
      with:
        name: detekt-generated-docs
        path: docs/pages/documentation/

    - name: Unzipping KDoc documentation
      uses: actions/download-artifact@v2
      with:
        name: detekt-kdoc
        path: docs/pages/kdoc/

    - name: Unzipping generated CLI options documentation
      uses: actions/download-artifact@v2
      with:
        name: detekt-cli-options-docs
        path: docs/pages/gettingstarted/

    - name: Display structure of files
      run: ls -R
      working-directory: docs/

    - name: Create Directories
      run: |
        mkdir _site/
        mkdir -p vendor/bundle/
        chmod -R 777 _site/
        chmod -R 777 vendor/
        touch Gemfile.lock
        chmod a+w Gemfile.lock
      working-directory: docs/

    - name: Install Ruby Dependencies
      working-directory: docs/
      run: |
        bundle config path vendor/bundle
        bundle install --jobs 4 --retry 3

    - name: Build the Jekyll Website
      run: bundle exec jekyll build -d _site/
      working-directory: docs/

    - name: Deploy Github Pages
      uses: JamesIves/github-pages-deploy-action@v4
      with:
        branch: gh-pages
        folder: docs/_site/
