name: Website checks

on:
  pull_request:
  push:
    branches:
      - main
      - 'v[0-9]+.[0-9]+'
    tags:
      - 'v[0-9]+.[0-9]+.[0-9]+*'

jobs:
  fileschanged:
    name: List files changed for pull request
    runs-on: ubuntu-latest
    steps:
      - name: "Fetch source code"
        uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
      - id: diff 
        run: |
          echo "Comparing current commit to base ref origin/${{github.event.pull_request.base.ref}}"
          git fetch --no-tags --prune --no-recurse-submodules --depth=1 origin ${{github.event.pull_request.base.ref}}
          echo "install=$(git diff --name-only origin/${{github.event.pull_request.base.ref}} | grep 'website/docs/intro/install' | wc -l)" | tee -a "$GITHUB_OUTPUT"
          echo "website=$(git diff --name-only origin/${{github.event.pull_request.base.ref}} | grep 'website/' | wc -l)" | tee -a "$GITHUB_OUTPUT"
    outputs:
      install: ${{ steps.diff.outputs.install }}
      website: ${{ steps.diff.outputs.website }}

  build:
    name: Build
    runs-on: ubuntu-latest
    needs: fileschanged
    if: ${{ needs.fileschanged.outputs.website != 0}}
    steps:
      - name: Checkout
        uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
        with:
          fetch-depth: 0
      - name: Prepare website container
        run: docker compose -f docker-compose.build.yml build
        working-directory: website
      # The segregation between the next 2 steps is because on non-main branches, there are
      # blog posts that point to the 'main' branch documentation that is fully missing from
      # the branches that are used to maintain older versions of OpenTofu.
      # Therefore, we run the full website check on main (and any branch in a PR that points
      # to the main branch) and we run a trimmed down website checks for any runs against
      # a non-main branch (or a PR that points to a non-main branch)
      - name: Build website
        if: github.ref == 'refs/heads/main' || github.base_ref == 'main'
        run: docker compose -f docker-compose.build.yml up --exit-code-from website
        working-directory: website
      - name: Build website with warns
        if: github.ref != 'refs/heads/main' && github.base_ref != 'main'
        run: docker compose -f docker-compose.build-non-main.yml up --exit-code-from website
        working-directory: website

  installation-instructions:
    name: "Test Installation Instructions"
    runs-on: ubuntu-latest
    needs: fileschanged
    if: ${{ needs.fileschanged.outputs.install != 0}}

    steps:
      - name: "Fetch source code"
        uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2

      - name: "Run Installation Instructions Test"
        run: make test-linux-install-instructions
