# Home Assistant Operating System build workflow

name: Development build

on:
  workflow_dispatch:

jobs:
  version:

    name: Generate version
    runs-on: [ "ubuntu-20.04" ]
    outputs:
      version_main: ${{ steps.version_main.outputs.version_main }}
      version_dev: ${{ steps.version_dev.outputs.version_dev }}
    steps:
      - name: Generate Development build version
        shell: bash
        id: version_dev
        run: |
          version_dev="dev$(date --utc +'%Y%m%d')"
          echo "Development version \"${version_dev}\""
          echo "::set-output name=version_dev::${version_dev}"
      - uses: actions/checkout@v2
      - name: Get Major/Minor version
        id: version_main
        run: |
          major=$(cat ${GITHUB_WORKSPACE}/buildroot-external/meta | grep VERSION_MAJOR | cut -d'=' -f2)
          build=$(cat ${GITHUB_WORKSPACE}/buildroot-external/meta | grep VERSION_BUILD | cut -d'=' -f2)
          echo "::set-output name=version_main::${major}.${build}"

  build:
    name: Release build for ${{ matrix.board.name }}
    needs: version
    strategy:
      fail-fast: false
      matrix:
        board:
          - {"name": "ova", "output": "ova", "runner": "x86-64-runner"}
          - {"name": "generic_x86_64", "output": "generic-x86-64", "runner": "x86-64-runner"}
          - {"name": "odroid_c2", "output": "odroid-c2", "runner": "aarch64-runner"}
          - {"name": "odroid_c4", "output": "odroid-c4", "runner": "aarch64-runner"}
          - {"name": "odroid_n2", "output": "odroid-n2", "runner": "aarch64-runner"}
          - {"name": "odroid_xu4", "output": "odroid-xu4" , "runner": "aarch64-runner"}
          - {"name": "rpi", "output": "rpi", "runner": "arm-runner"}
          - {"name": "rpi0_w", "output": "rpi0-w", "runner": "arm-runner"}
          - {"name": "rpi2", "output": "rpi2", "runner": "arm-runner"}
          - {"name": "rpi3", "output": "rpi3", "runner": "arm-runner"}
          - {"name": "rpi3_64", "output": "rpi3-64", "runner": "aarch64-runner"}
          - {"name": "rpi4", "output": "rpi4", "runner": "arm-runner"}
          - {"name": "rpi4_64", "output": "rpi4-64", "runner": "aarch64-runner"}
          - {"name": "tinker", "output": "tinker", "runner": "arm-runner"}
    runs-on: ${{ matrix.board.runner }}

    steps:
      # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
      - uses: actions/checkout@v2

      - name: Build container
        run: docker build -t haos-builder .

      - name: 'Add release PKI certs'
        env:
          RAUC_CERTIFICATE: ${{ secrets.RAUC_CERTIFICATE }}
          RAUC_PRIVATE_KEY: ${{ secrets.RAUC_PRIVATE_KEY }}
        run: |
          echo -e "-----BEGIN CERTIFICATE-----\n${RAUC_CERTIFICATE}\n-----END CERTIFICATE-----" > cert.pem
          echo -e "-----BEGIN PRIVATE KEY-----\n${RAUC_PRIVATE_KEY}\n-----END PRIVATE KEY-----" > key.pem

      - name: Build
        run: |
          BUILDER_UID="$(id -u)"
          BUILDER_GID="$(id -g)"
          docker run --rm --privileged -v "${GITHUB_WORKSPACE}:/build" \
            -e BUILDER_UID="${BUILDER_UID}" -e BUILDER_GID="${BUILDER_GID}" \
            -v "${{ matrix.board.runner }}-build-cache:/cache" \
            haos-builder make BUILDDIR=/build VERSION_DEV=${{ needs.version.outputs.version_dev }} ${{ matrix.board.name }}

      - name: Upload images
        uses: appleboy/scp-action@master
        with:
          host: ${{ secrets.DEV_HOST }}
          username: ${{ secrets.DEV_USERNAME }}
          port: ${{ secrets.DEV_PORT }}
          key: ${{ secrets.DEV_SCP_KEY }}
          source: "release/*"
          target: ${{ secrets.DEV_TARGET_PATH }}/${{ needs.version.outputs.version_main }}.${{ needs.version.outputs.version_dev }}/
          strip_components: 1

  bump_version:
    name: Bump dev version to ${{ needs.version.outputs.version_main }}.${{ needs.version.outputs.version_dev }}
    needs: [ build, version ]
    runs-on: [ "ubuntu-20.04" ]

    steps:
    - name: Initialize git
      uses: home-assistant/actions/helpers/git-init@master
      with:
        name: ${{ secrets.GIT_NAME }}
        email: ${{ secrets.GIT_EMAIL }}
        token: ${{ secrets.GIT_TOKEN }}

    - name: Bump Home Assistant OS dev version
      uses: home-assistant/actions/helpers/version-push@master
      with:
        key: "hassos[]"
        key-description: "Home Assistant OS"
        version: ${{ needs.version.outputs.version_main }}.${{ needs.version.outputs.version_dev }}
        channel: "dev"
