# https://dev.azure.com/home-assistant

trigger:
  batch: true
  tags:
    include:
    - '*'
    exclude:
    - untagged*
pr: none

variables:
  - name: versionGHR
    value: 'v0.13.0'
  - group: github
  - group: hassos

jobs:

- job: 'VersionValidate'
  condition: startsWith(variables['Build.SourceBranch'], 'refs/tags')
  pool:
    vmImage: 'ubuntu-latest'
  steps:
  - script: |
      . buildroot-external/meta

      if [ "${VERSION_MAJOR}.${VERSION_BUILD}" != "$(Build.SourceBranchName)" ]; then
        exit 1
      fi
    displayName: 'Check version of branch/tag'


- job: 'Release'
  dependsOn:
  - 'VersionValidate'
  timeoutInMinutes: 240
  pool:
    name: 'Buildroot'
  strategy:
    maxParallel: 1
    matrix:
      OpenVirtualAppliance:
        board: 'ova'
      IntelNuc:
        board: 'intel_nuc'
      OdroidC2:
        board: 'odroid_c2'
      OdroidN2:
        board: 'odroid_n2'
      OdroidXU4:
        board: 'odroid_xu4'
      RaspberryPi:
        board: 'rpi'
      RaspberryPi0-W:
        board: 'rpi0_w'
      RaspberryPi2:
        board: 'rpi2'
      RaspberryPi3:
        board: 'rpi3'
      RaspberryPi3-64:
        board: 'rpi3_64'
      RaspberryPi4:
        board: 'rpi4'
      RaspberryPi4-64:
        board: 'rpi4_64'
      AsusTinker:
        board: 'tinker'
  workspace:
    clean: 'all'
  steps:
  - script: |
      echo -e "-----BEGIN CERTIFICATE-----\n$(releaseCertificate)\n-----END CERTIFICATE-----" > cert.pem
      echo -e "-----BEGIN PRIVATE KEY-----\n$(releaseKey)\n-----END PRIVATE KEY-----" > key.pem
    displayName: 'Add release PKI certs'
  - script: |
      sudo docker build -t hassos:azure .
    displayName: 'Build container for HassOS'
  - script: |
      curl -SsL https://github.com/tcnksm/ghr/releases/download/$(versionGHR)/ghr_$(versionGHR)_linux_amd64.tar.gz | tar xzf -
      cp ghr_$(versionGHR)_linux_amd64/ghr .
      rm -rf ghr_$(versionGHR)_linux_amd64
    displayName: 'Install GHR'
  - script: |
      BUILDER_UID="$(id -u)"
      BUILDER_GID="$(id -g)"

      sudo docker run --rm --privileged -v "$(pwd):/build" \
        -e BUILDER_UID="${BUILDER_UID}" -e BUILDER_GID="${BUILDER_GID}" \
        -v "/mnt/build-cache:/cache" \
        hassos:azure make $(board)
    displayName: 'Build $(board)'
  - script: |
      ./ghr -t $(githubToken) $(Build.SourceBranchName) release/
    displayName: 'Upload $(board)'
