name: Nightly Canary Release

on:
  schedule:
    - cron: '0 6 * * *' # daily at 6am UTC
  workflow_dispatch:

concurrency:
  group: nightly-publish
  cancel-in-progress: false

jobs:
  check:
    name: Check for new commits
    runs-on: ubuntu-latest
    permissions:
      contents: read
    outputs:
      has_changes: ${{ steps.check.outputs.has_changes }}
    steps:
      - uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4.3.1
        with:
          fetch-depth: 0

      - name: Check for recent commits
        id: check
        run: |
          # 25h window absorbs cron-vs-commit timing skew at the boundary.
          RECENT=$(git log --since="25 hours ago" --oneline | head -1)
          if [ -n "$RECENT" ]; then
            echo "has_changes=true" >> "$GITHUB_OUTPUT"
          else
            echo "has_changes=false" >> "$GITHUB_OUTPUT"
          fi

  build:
    name: Build nightly packages
    needs: check
    if: needs.check.outputs.has_changes == 'true' || github.event_name == 'workflow_dispatch'
    runs-on: ubuntu-latest
    permissions:
      contents: read
    steps:
      - uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4.3.1

      - name: Install uv
        uses: astral-sh/setup-uv@d0cc045d04ccac9d8b7881df0226f9e82c39688e # v6
        with:
          version: "0.11.3"
          python-version: "3.12"
          enable-cache: false

      - name: Stamp nightly versions
        run: |
          DATE=$(date +%Y%m%d)

          # All workspace packages share the same base version and are released together.
          BASE=$(python -c "
          import re
          print(re.search(r'__version__\s*=\s*\"(.*?)\"', open('lib/crewai/src/crewai/__init__.py').read()).group(1))
          ")
          NIGHTLY="${BASE}.dev${DATE}"
          echo "Nightly version: ${NIGHTLY}"

          for init_file in \
            lib/crewai/src/crewai/__init__.py \
            lib/crewai-core/src/crewai_core/__init__.py \
            lib/crewai-tools/src/crewai_tools/__init__.py \
            lib/crewai-files/src/crewai_files/__init__.py \
            lib/cli/src/crewai_cli/__init__.py; do
            sed -i "s/__version__ = .*/__version__ = \"${NIGHTLY}\"/" "$init_file"
            echo "Stamped $init_file -> $NIGHTLY"
          done

          # Update all cross-package dependency pins to the nightly version.
          sed -i "s/\"crewai==[^\"]*\"/\"crewai==${NIGHTLY}\"/" lib/crewai-tools/pyproject.toml
          sed -i "s/\"crewai-core==[^\"]*\"/\"crewai-core==${NIGHTLY}\"/" lib/crewai/pyproject.toml
          sed -i "s/\"crewai-cli==[^\"]*\"/\"crewai-cli==${NIGHTLY}\"/" lib/crewai/pyproject.toml
          sed -i "s/\"crewai-tools==[^\"]*\"/\"crewai-tools==${NIGHTLY}\"/" lib/crewai/pyproject.toml
          sed -i "s/\"crewai-files==[^\"]*\"/\"crewai-files==${NIGHTLY}\"/" lib/crewai/pyproject.toml
          sed -i "s/\"crewai-core==[^\"]*\"/\"crewai-core==${NIGHTLY}\"/" lib/cli/pyproject.toml
          echo "Updated cross-package dependency pins to ${NIGHTLY}"

      - name: Build packages
        run: |
          uv build --all-packages
          rm dist/.gitignore

      - name: Upload artifacts
        uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2
        with:
          name: dist
          path: dist/

  publish:
    name: Publish nightly to PyPI
    needs: build
    runs-on: ubuntu-latest
    environment:
      name: pypi
    permissions:
      id-token: write
      contents: read
    steps:
      - name: Install uv
        uses: astral-sh/setup-uv@d0cc045d04ccac9d8b7881df0226f9e82c39688e # v6
        with:
          version: "0.11.3"
          python-version: "3.12"
          enable-cache: false

      - name: Download artifacts
        uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4.3.0
        with:
          name: dist
          path: dist

      - name: Publish to PyPI
        env:
          UV_PUBLISH_TOKEN: ${{ secrets.PYPI_API_TOKEN }}
        run: |
          failed=0
          for package in dist/*; do
            if [[ "$package" == *"crewai_devtools"* ]]; then
              echo "Skipping private package: $package"
              continue
            fi
            echo "Publishing $package"
            # --check-url skips files already on PyPI so manual re-runs on the same day are idempotent.
            if ! uv publish --check-url https://pypi.org/simple/ "$package"; then
              echo "Failed to publish $package"
              failed=1
            fi
          done
          if [ $failed -eq 1 ]; then
            echo "Some packages failed to publish"
            exit 1
          fi