name: "Build and deploy to Netlify"
on:
  push:
    branches:
      - master
      - drafts/*
      - content/*
  workflow_run:
    workflows:
      - "Update webring"
    types:
      - "completed"
env:
  NETLIFY_AUTH_TOKEN: ${{ secrets.NETLIFY_ACCESS_TOKEN }}
  NETLIFY_SITE_ID: ${{ secrets.NETLIFY_SITE_ID }}
jobs:
  build:
    runs-on: ubuntu-latest
    permissions:
      id-token: "write"
      contents: "read"
    steps:
      - name: Fetch/cache the fetched URIs
        uses: actions/cache@v3
        with:
          key: open-uri-content-cache
          path: |
            /tmp/open-uri-cache-foodogsquared-website

      - uses: actions/checkout@v3
        with:
          fetch-depth: 0

      - uses: DeterminateSystems/nix-installer-action@v7

      - name: Fetch all 'content/' branches
        run: |
          git fetch origin +refs/heads/content/*:refs/heads/content/*

      - if: github.ref_name == 'master'
        name: Deploy site to Netlify
        run: |
          nix develop -c rake build[production]
          nix develop -c netlify deploy --prod --dir ./public

      - if: startsWith('drafts/', github.ref_name)
        name: Deploy draft versions of the site to Netlify
        run: |
          nix develop -c rake build[branch-deploy]
          nix develop -c netlify deploy --context branch-deploy --dir ./public
