# Home Assistant Operating System release build workflow

name: Release build

on:
  release:
    types: [published]

jobs:
  validate_release:
    name: Validate release
    runs-on: [ "ubuntu-20.04" ]
    outputs:
      version: ${{ steps.version_check.outputs.version }}
      version_dev: ${{ steps.version_check.outputs.version_dev }}
    steps:
      - uses: actions/checkout@v2
      - name: Validate version
        id: version_check
        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)
          tag_major=$(echo "${{ github.event.release.tag_name }}" | cut -d '.' -f 1)
          tag_build=$(echo "${{ github.event.release.tag_name }}" | cut -d '.' -f 2)
          tag_dev=$(echo "${{ github.event.release.tag_name }}" | cut -d '.' -f 3)
          if [ "${major}.${build}" != "${tag_major}.${tag_build}" ]; then
            echo "Version number in Buildroot metadata is does not match tag (${major}.${build} vs ${{ github.event.release.tag_name }})."
            exit 1
          fi
          if [ "" != "${tag_dev}" ]; then
            echo "Note: Release build with custom dev part: ${tag_dev}."
          fi
          echo "::set-output name=version::${major}.${build}.${tag_dev}"
          echo "::set-output name=version_dev::${tag_dev}"

  build:
    name: Release build for ${{ matrix.board.name }}
    needs: validate_release
    strategy:
      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.validate_release.outputs.version_dev }} ${{ matrix.board.name }}

      - name: Upload disk image
        if: ${{ matrix.board.name != 'ova' }}
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ github.event.release.upload_url }}
          asset_path: ${{ github.workspace }}/release/haos_${{ matrix.board.output }}-${{ needs.validate_release.outputs.version }}.img.xz
          asset_name: haos_${{ matrix.board.output }}-${{ needs.validate_release.outputs.version }}.img.xz
          asset_content_type: application/x-xz

      - name: Upload rauc update
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ github.event.release.upload_url }}
          asset_path: ${{ github.workspace }}/release/haos_${{ matrix.board.output }}-${{ needs.validate_release.outputs.version }}.raucb
          asset_name: haos_${{ matrix.board.output }}-${{ needs.validate_release.outputs.version }}.raucb
          asset_content_type: application/octet-stream

      - name: Upload ova image
        if: ${{ matrix.board.name == 'ova' }}
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ github.event.release.upload_url }}
          asset_path: ${{ github.workspace }}/release/haos_${{ matrix.board.output }}-${{ needs.validate_release.outputs.version }}.ova
          asset_name: haos_${{ matrix.board.output }}-${{ needs.validate_release.outputs.version }}.ova
          asset_content_type: application/x-tar

      - name: Upload qcow2 image
        if: ${{ matrix.board.name == 'ova' }}
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ github.event.release.upload_url }}
          asset_path: ${{ github.workspace }}/release/haos_${{ matrix.board.output }}-${{ needs.validate_release.outputs.version }}.qcow2.xz
          asset_name: haos_${{ matrix.board.output }}-${{ needs.validate_release.outputs.version }}.qcow2.xz
          asset_content_type: application/x-xz

      - name: Upload vdi image
        if: ${{ matrix.board.name == 'ova' }}
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ github.event.release.upload_url }}
          asset_path: ${{ github.workspace }}/release/haos_${{ matrix.board.output }}-${{ needs.validate_release.outputs.version }}.vdi.zip
          asset_name: haos_${{ matrix.board.output }}-${{ needs.validate_release.outputs.version }}.vdi.zip
          asset_content_type: application/zip

      - name: Upload vhdx image
        if: ${{ matrix.board.name == 'ova' }}
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ github.event.release.upload_url }}
          asset_path: ${{ github.workspace }}/release/haos_${{ matrix.board.output }}-${{ needs.validate_release.outputs.version }}.vhdx.zip
          asset_name: haos_${{ matrix.board.output }}-${{ needs.validate_release.outputs.version }}.vhdx.zip
          asset_content_type: application/zip

      - name: Upload vmdk image
        if: ${{ matrix.board.name == 'ova' }}
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ github.event.release.upload_url }}
          asset_path: ${{ github.workspace }}/release/haos_${{ matrix.board.output }}-${{ needs.validate_release.outputs.version }}.vmdk.zip
          asset_name: haos_${{ matrix.board.output }}-${{ needs.validate_release.outputs.version }}.vmdk.zip
          asset_content_type: application/zip

  bump_version:
    name: Bump dev version to ${{ needs.validate_release.outputs.version }}
    needs: [ build, validate_release ]
    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 beta version
      uses: home-assistant/actions/helpers/version-push@master
      with:
        key: "hassos[]"
        key-description: "Home Assistant OS"
        version: ${{ needs.validate_release.outputs.version }}
        channel: "beta"
