name: Build Site

on:
  push:
    paths-ignore:
    - '_site/**'

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
      with:
        # required for personal access token to be used below
        persist-credentials: false
    - uses: actions/setup-java@v1
      with:
        java-version: '8'
    - run: sudo apt-get install -y patchutils
    - name: Install babashka
      run: |
         curl -s https://raw.githubusercontent.com/borkdude/babashka/master/install -o install-bb
         chmod +x install-bb
         sudo ./install-bb
         rm install-bb
    - name: Install bootleg
      run: |
         curl -LO https://github.com/retrogradeorbit/bootleg/releases/download/v0.1.9/bootleg-0.1.9-linux-amd64.tgz
         tar xvf bootleg-0.1.9-linux-amd64.tgz
         sudo mv bootleg /usr/local/bin

    - name: Fill in blanks
      run: make update-frontmatter
    - name: Commit updated content files
      run: |
        git config --local user.email "action@github.com"
        git config --local user.name "GitHub Action"
        ./stage-if-changed.sh content/
        git diff --staged --exit-code || git commit -m "Update frontmatter"

    - run: npm ci

    - name: Build site
      run: make site

    - name: Commit updated _site files
      run: |
        git config --local user.email "action@github.com"
        git config --local user.name "GitHub Action"
        ./stage-if-changed.sh _site/
        git diff --staged --exit-code || git commit -m "Update compiled _site"

    - name: Push changes
      uses: ad-m/github-push-action@master
      with:
        # Any activity created using the GITHUB_TOKEN provided by the GitHub Action environment does
        # not trigger any workflows, so we use a personal access token
        # https://github.community/t5/GitHub-Actions/Push-from-Action-does-not-trigger-subsequent-action/m-p/37872
        github_token: ${{ secrets.GITHUB_TOKEN }}
        branch: ${{ github.ref }}
