name: Docs and Tutorial
on:
  push:
    branches: [main, 'stable/*']
  pull_request:
    branches: [main, 'stable/*']
concurrency:
  group: ${{ github.repository }}-${{ github.ref }}-${{ github.head_ref }}-${{ github.workflow }}
  cancel-in-progress: true
jobs:
  docs:
    runs-on: ubuntu-latest
    timeout-minutes: 60
    strategy:
      matrix:
        python-version: ['3.9']
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0
      - name: Set up Python ${{ matrix.python-version }}
        uses: actions/setup-python@v5
        with:
          python-version: ${{ matrix.python-version }}
      - name: Pip cache
        uses: actions/cache@v4
        with:
          path: ~/.cache/pip
          key: ${{ runner.os }}-${{ matrix.python-version}}-pip-docs-${{ hashFiles('setup.py','requirements-dev.txt','constraints.txt') }}
          restore-keys: |
            ${{ runner.os }}-${{ matrix.python-version}}-pip-docs-
            ${{ runner.os }}-${{ matrix.python-version}}-pip-
            ${{ runner.os }}-${{ matrix.python-version}}-
      - name: Install Deps
        run: |
          set -e
          pip install -U pip virtualenv wheel
          pip install -U tox
          sudo apt-get update
          sudo apt-get install -y build-essential libopenblas-dev pandoc
        shell: bash
      - name: Run Docs Build
        run: tox -edocs
      - uses: actions/upload-artifact@v4
        with:
          name: html_docs
          path: docs/_build/html
  tutorials:
    runs-on: ubuntu-latest
    needs: [docs]
    strategy:
      matrix:
        python-version: ['3.9']
    steps:
      - uses: actions/checkout@v4
      - name: Set up Python ${{ matrix.python-version }}
        uses: actions/setup-python@v5
        with:
          python-version: ${{ matrix.python-version }}
      - name: Pip cache
        uses: actions/cache@v4
        with:
          path: ~\AppData\Local\pip\Cache
          key: ${{ runner.os }}-${{ matrix.python-version}}-pip-tutorials-${{ hashFiles('setup.py','requirements-dev.txt','constraints.txt') }}
          restore-keys: |
            ${{ runner.os }}-${{ matrix.python-version}}-pip-tutorials-
            ${{ runner.os }}-${{ matrix.python-version}}-pip-
            ${{ runner.os }}-${{ matrix.python-version}}}-
      - name: Setup tutorials job
        run: |
          set -e
          sudo apt install -y graphviz pandoc libblas-dev liblapack-dev
          git clone https://github.com/Qiskit/qiskit-tutorials --depth=1
          python -m pip install --upgrade pip wheel
          pip install -U -r requirements-dev.txt -c constraints.txt
          pip install -c constraints.txt git+https://github.com/Qiskit/qiskit
          pip install -c constraints.txt .
          pip check
        shell: bash
      - name: Run Tutorials
        run: |
          set -e
          cd qiskit-tutorials
          rm -rf tutorials/circuits tutorials/circuits_advanced tutorials/algorithms tutorials/operators
          sphinx-build -b html . _build/html
      - uses: actions/upload-artifact@v4
        with:
          name: tutorials_html
          path: qiskit-tutorials/_build/html
