name: Publish Release

on:
  workflow_dispatch:
  release:
    types: [published]
  schedule:
    - cron: "0 2 * * *"

permissions:
  contents: read

jobs:
  init:
    name: Initialize build
    runs-on: ubuntu-latest
    outputs:
      tag: ${{ steps.tag.outputs.tag }}
    steps:
      - uses: actions/checkout@v2
      - name: Get tag
        id: tag
        run: |
          if [[ "$GITHUB_EVENT_NAME" = "release" ]]; then
            TAG="${GITHUB_REF#refs/tags/}"
          else
            TAG=$(cat esphome/const.py | sed -n -E "s/^__version__\s+=\s+\"(.+)\"$/\1/p")
            today="$(date --utc '+%Y%m%d')"
            TAG="${TAG}${today}"
          fi
          echo "::set-output name=tag::${TAG}"

  deploy-pypi:
    name: Build and publish to PyPi
    if: github.repository == 'esphome/esphome' && github.event_name == 'release'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Set up Python
        uses: actions/setup-python@v1
        with:
          python-version: '3.x'
      - name: Set up python environment
        run: |
          script/setup
          pip install setuptools wheel twine
      - name: Build
        run: python setup.py sdist bdist_wheel
      - name: Upload
        env:
          TWINE_USERNAME: __token__
          TWINE_PASSWORD: ${{ secrets.PYPI_TOKEN }}
        run: twine upload dist/*

  deploy-docker:
    name: Build and publish docker containers
    if: github.repository == 'esphome/esphome'
    permissions:
      contents: read
      packages: write
    runs-on: ubuntu-latest
    needs: [init]
    strategy:
      matrix:
        arch: [amd64, armv7, aarch64]
        build_type: ["ha-addon", "docker", "lint"]
    steps:
    - uses: actions/checkout@v2
    - name: Set up Python
      uses: actions/setup-python@v2
      with:
        python-version: '3.9'

    - name: Set up Docker Buildx
      uses: docker/setup-buildx-action@v1
    - name: Set up QEMU
      uses: docker/setup-qemu-action@v1

    - name: Log in to docker hub
      uses: docker/login-action@v1
      with:
        username: ${{ secrets.DOCKER_USER }}
        password: ${{ secrets.DOCKER_PASSWORD }}
    - name: Log in to the GitHub container registry
      uses: docker/login-action@v1
      with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

    - name: Build and push
      run: |
        docker/build.py \
          --tag "${{ needs.init.outputs.tag }}" \
          --arch "${{ matrix.arch }}" \
          --build-type "${{ matrix.build_type }}" \
          build \
          --push

  deploy-docker-manifest:
    if: github.repository == 'esphome/esphome'
    permissions:
      contents: read
      packages: write
    runs-on: ubuntu-latest
    needs: [init, deploy-docker]
    strategy:
      matrix:
        build_type: ["ha-addon", "docker", "lint"]
    steps:
    - uses: actions/checkout@v2
    - name: Set up Python
      uses: actions/setup-python@v2
      with:
        python-version: '3.9'
    - name: Enable experimental manifest support
      run: |
        mkdir -p ~/.docker
        echo "{\"experimental\": \"enabled\"}" > ~/.docker/config.json

    - name: Log in to docker hub
      uses: docker/login-action@v1
      with:
        username: ${{ secrets.DOCKER_USER }}
        password: ${{ secrets.DOCKER_PASSWORD }}
    - name: Log in to the GitHub container registry
      uses: docker/login-action@v1
      with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

    - name: Run manifest
      run: |
        docker/build.py \
          --tag "${{ needs.init.outputs.tag }}" \
          --build-type "${{ matrix.build_type }}" \
          manifest

  deploy-ha-addon-repo:
    if: github.repository == 'esphome/esphome' && github.event_name == 'release'
    runs-on: ubuntu-latest
    needs: [deploy-docker]
    steps:
      - env:
          TOKEN: ${{ secrets.DEPLOY_HA_ADDON_REPO_TOKEN }}
        run: |
          TAG="${GITHUB_REF#refs/tags/}"
          curl \
            -u ":$TOKEN" \
            -X POST \
            -H "Accept: application/vnd.github.v3+json" \
            https://api.github.com/repos/esphome/home-assistant-addon/actions/workflows/bump-version.yml/dispatches \
            -d "{\"ref\":\"main\",\"inputs\":{\"version\":\"$TAG\"}}"
