name: Release

on:
  release:
    types:
      - published

env:
  PYTHON_VERSION: "3.13"
  NODE_OPTIONS: --max_old_space_size=6144

# Set default workflow permissions
# All scopes not mentioned here are set to no access
# https://docs.github.com/en/actions/security-guides/automatic-token-authentication#permissions-for-the-github_token
permissions:
  actions: none

jobs:
  release:
    name: Release
    runs-on: ubuntu-latest
    environment: pypi
    permissions:
      contents: write # Required to upload release assets
      id-token: write # For "Trusted Publisher" to PyPi
    if: github.repository_owner == 'home-assistant'
    steps:
      - name: Checkout the repository
        uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2

      - name: Set up Python ${{ env.PYTHON_VERSION }}
        uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v6.2.0
        with:
          python-version: ${{ env.PYTHON_VERSION }}

      - name: Verify version
        uses: home-assistant/actions/helpers/verify-version@master

      - name: Setup Node
        uses: actions/setup-node@6044e13b5dc448c55e2357c09f80417699197238 # v6.2.0
        with:
          node-version-file: ".nvmrc"
          cache: yarn

      - name: Install dependencies
        run: yarn install

      - name: Download Translations
        run: ./script/translations_download
        env:
          LOKALISE_TOKEN: ${{ secrets.LOKALISE_TOKEN }}

      - name: Build and release package
        run: |
          python3 -m pip install build
          export SKIP_FETCH_NIGHTLY_TRANSLATIONS=1
          script/release

      - name: Publish to PyPI
        uses: pypa/gh-action-pypi-publish@ed0c53931b1dc9bd32cbe73a98c7f6766f8a527e # v1.13.0
        with:
          skip-existing: true

      - name: Upload release assets
        uses: softprops/action-gh-release@a06a81a03ee405af7f2048a818ed3f03bbf83c7b # v2.5.0
        with:
          files: |
            dist/*.whl
            dist/*.tar.gz

  wheels-init:
    name: Init wheels build
    needs: release
    runs-on: ubuntu-latest
    steps:
      - name: Generate requirements.txt
        run: |
          # Sleep to give pypi time to populate the new version across mirrors
          sleep 240
          version=$(echo "${{ github.ref }}" | awk -F"/" '{print $NF}' )
          echo "home-assistant-frontend==$version" > ./requirements.txt

      # home-assistant/wheels doesn't support SHA pinning
      - name: Build wheels
        uses: home-assistant/wheels@2025.12.0
        with:
          abi: cp313
          tag: musllinux_1_2
          arch: amd64
          wheels-key: ${{ secrets.WHEELS_KEY }}
          requirements: "requirements.txt"

  release-landing-page:
    name: Release landing-page frontend
    if: github.event.release.prerelease == false
    runs-on: ubuntu-latest
    permissions:
      contents: write # Required to upload release assets
    steps:
      - name: Checkout the repository
        uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
      - name: Setup Node
        uses: actions/setup-node@6044e13b5dc448c55e2357c09f80417699197238 # v6.2.0
        with:
          node-version-file: ".nvmrc"
          cache: yarn
      - name: Install dependencies
        run: yarn install
      - name: Download Translations
        run: ./script/translations_download
        env:
          LOKALISE_TOKEN: ${{ secrets.LOKALISE_TOKEN }}
      - name: Build landing-page
        run: landing-page/script/build_landing_page
      - name: Tar folder
        run: tar -czf landing-page/home_assistant_frontend_landingpage-${{ github.event.release.tag_name }}.tar.gz -C landing-page/dist .
      - name: Upload release asset
        uses: softprops/action-gh-release@a06a81a03ee405af7f2048a818ed3f03bbf83c7b # v2.5.0
        with:
          files: landing-page/home_assistant_frontend_landingpage-${{ github.event.release.tag_name }}.tar.gz
