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

trigger:
  tags:
    include:
      - '*'
pr: none
schedules:
  - cron: "0 1 * * *"
    displayName: "nightly builds"
    branches:
      include:
        - dev
    always: true
variables:
  - name: versionBuilder
    value: '2021.02.0'
  - group: docker
  - group: github
  - group: twine
resources:
  repositories:
    - repository: azure
      type: github
      name: 'home-assistant/ci-azure'
      endpoint: 'home-assistant'

stages:

- stage: 'Validate'
  jobs:
  - template: templates/azp-job-version.yaml@azure
    parameters:
      ignoreDev: true
  - job: 'Permission'
    pool:
      vmImage: 'ubuntu-latest'
    steps:
    - script: |
        sudo apt-get install -y --no-install-recommends \
          jq curl

        release="$(Build.SourceBranchName)"
        created_by="$(curl -s https://api.github.com/repos/home-assistant/core/releases/tags/${release} | jq --raw-output '.author.login')"

        if [[ "${created_by}" =~ ^(balloob|pvizeli|fabaff|robbiet480|bramkragten|frenck)$ ]]; then
          exit 0
        fi

        echo "${created_by} is not allowed to create an release!"
        exit 1
      displayName: 'Check rights'
      condition: and(succeeded(), startsWith(variables['Build.SourceBranch'], 'refs/tags'))

- stage: 'Build'
  jobs:
  - job: 'ReleasePython'
    condition: startsWith(variables['Build.SourceBranch'], 'refs/tags')
    pool:
      vmImage: 'ubuntu-latest'
    steps:
    - task: UsePythonVersion@0
      displayName: 'Use Python 3.8'
      inputs:
        versionSpec: '3.8'
    - script: pip install twine wheel
      displayName: 'Install tools'
    - script: python setup.py sdist bdist_wheel
      displayName: 'Build package'
    - script: |
        export TWINE_USERNAME="$(twineUser)"
        export TWINE_PASSWORD="$(twinePassword)"

        twine upload dist/* --skip-existing
      displayName: 'Upload pypi'
  - job: 'ReleaseDocker'
    timeoutInMinutes: 240
    pool:
      vmImage: 'ubuntu-latest'
    strategy:
      maxParallel: 5
      matrix:
        amd64:
          buildArch: 'amd64'
        i386:
          buildArch: 'i386'
        armhf:
          buildArch: 'armhf'
        armv7:
          buildArch: 'armv7'
        aarch64:
          buildArch: 'aarch64'
    steps:
    - template: templates/azp-step-ha-version.yaml@azure
    - script: |
        docker login -u $(dockerUser) -p $(dockerPassword)
      displayName: 'Docker hub login'
    - script: docker pull homeassistant/amd64-builder:$(versionBuilder)
      displayName: 'Install Builder'
    - script: |
        set -e

        docker run --rm --privileged \
          -v ~/.docker:/root/.docker:rw \
          -v /run/docker.sock:/run/docker.sock:rw \
          -v $(pwd):/data:ro \
          homeassistant/amd64-builder:$(versionBuilder) \
          --generic $(homeassistantRelease) "--$(buildArch)" -t /data \
      displayName: 'Build Release'
  - job: 'ReleaseMachine'
    dependsOn:
    - ReleaseDocker
    timeoutInMinutes: 240
    pool:
      vmImage: 'ubuntu-latest'
    strategy:
      maxParallel: 17
      matrix:
        qemux86-64:
          buildMachine: 'qemux86-64'
        generic-x86-64:
          buildMachine: 'generic-x86-64'
        intel-nuc:
          buildMachine: 'intel-nuc'
        qemux86:
          buildMachine: 'qemux86'
        qemuarm:
          buildMachine: 'qemuarm'
        raspberrypi:
          buildMachine: 'raspberrypi'
        raspberrypi2:
          buildMachine: 'raspberrypi2'
        raspberrypi3:
          buildMachine: 'raspberrypi3'
        raspberrypi4:
          buildMachine: 'raspberrypi4'
        odroid-xu:
          buildMachine: 'odroid-xu'
        tinker:
          buildMachine: 'tinker'
        qemuarm-64:
          buildMachine: 'qemuarm-64'
        raspberrypi3-64:
          buildMachine: 'raspberrypi3-64'
        raspberrypi4-64:
          buildMachine: 'raspberrypi4-64'
        odroid-c2:
          buildMachine: 'odroid-c2'
        odroid-c4:
          buildMachine: 'odroid-c4'
        odroid-n2:
          buildMachine: 'odroid-n2'
    steps:
    - template: templates/azp-step-ha-version.yaml@azure
    - script: |
        docker login -u $(dockerUser) -p $(dockerPassword)
      displayName: 'Docker hub login'
    - script: docker pull homeassistant/amd64-builder:$(versionBuilder)
      displayName: 'Install Builder'
    - script: |
        set -e

        docker run --rm --privileged \
          -v ~/.docker:/root/.docker \
          -v /run/docker.sock:/run/docker.sock:rw \
          -v $(pwd):/data:ro \
          homeassistant/amd64-builder:$(versionBuilder) \
          --homeassistant-machine "$(homeassistantRelease)=$(buildMachine)" \
          -t /data/machine --docker-hub homeassistant
      displayName: 'Build Machine'

- stage: 'Publish'
  jobs:
  - job: 'ReleaseHassio'
    pool:
      vmImage: 'ubuntu-latest'
    steps:
    - template: templates/azp-step-ha-version.yaml@azure
    - script: |
        sudo apt-get install -y --no-install-recommends \
          git jq curl

        git config --global user.name "Pascal Vizeli"
        git config --global user.email "pvizeli@syshack.ch"
        git config --global credential.helper store

        echo "https://$(githubToken):x-oauth-basic@github.com" > $HOME/.git-credentials
      displayName: 'Install requirements'
    - script: |
        set -e

        version="$(homeassistantRelease)"

        git clone https://github.com/home-assistant/version
        cd version

        dev_version="$(jq --raw-output '.homeassistant.default' dev.json)"
        beta_version="$(jq --raw-output '.homeassistant.default' beta.json)"
        stable_version="$(jq --raw-output '.homeassistant.default' stable.json)"

        if [[ "$version" =~ d ]]; then
          sed -i "s|$dev_version|$version|g" dev.json
        elif [[ "$version" =~ b ]]; then
          sed -i "s|$beta_version|$version|g" beta.json
        else
          sed -i "s|$beta_version|$version|g" beta.json
          sed -i "s|$stable_version|$version|g" stable.json
        fi

        git commit -am "Bump Home Assistant $version"
        git push
      displayName: "Update version files"
  - job: 'ReleaseDocker'
    pool:
      vmImage: 'ubuntu-latest'
    steps:
    - template: templates/azp-step-ha-version.yaml@azure
    - script: |
        docker login -u $(dockerUser) -p $(dockerPassword)
      displayName: 'Docker login'
    - script: |
        set -e
        export DOCKER_CLI_EXPERIMENTAL=enabled

        function create_manifest() {
          local tag_l=$1
          local tag_r=$2

          docker manifest create homeassistant/home-assistant:${tag_l} \
            homeassistant/amd64-homeassistant:${tag_r} \
            homeassistant/i386-homeassistant:${tag_r} \
            homeassistant/armhf-homeassistant:${tag_r} \
            homeassistant/armv7-homeassistant:${tag_r} \
            homeassistant/aarch64-homeassistant:${tag_r}

          docker manifest annotate homeassistant/home-assistant:${tag_l} \
            homeassistant/amd64-homeassistant:${tag_r} \
            --os linux --arch amd64

          docker manifest annotate homeassistant/home-assistant:${tag_l} \
            homeassistant/i386-homeassistant:${tag_r} \
            --os linux --arch 386

          docker manifest annotate homeassistant/home-assistant:${tag_l} \
            homeassistant/armhf-homeassistant:${tag_r} \
            --os linux --arch arm --variant=v6

          docker manifest annotate homeassistant/home-assistant:${tag_l} \
            homeassistant/armv7-homeassistant:${tag_r} \
            --os linux --arch arm --variant=v7

          docker manifest annotate homeassistant/home-assistant:${tag_l} \
            homeassistant/aarch64-homeassistant:${tag_r} \
            --os linux --arch arm64 --variant=v8

          docker manifest push --purge homeassistant/home-assistant:${tag_l}
        }

        docker pull homeassistant/amd64-homeassistant:$(homeassistantRelease)
        docker pull homeassistant/i386-homeassistant:$(homeassistantRelease)
        docker pull homeassistant/armhf-homeassistant:$(homeassistantRelease)
        docker pull homeassistant/armv7-homeassistant:$(homeassistantRelease)
        docker pull homeassistant/aarch64-homeassistant:$(homeassistantRelease)

        # Create version tag
        create_manifest "$(homeassistantRelease)" "$(homeassistantRelease)"

        # Create general tags
        if [[ "$(homeassistantRelease)" =~ d ]]; then
          create_manifest "dev" "$(homeassistantRelease)"
        elif [[ "$(homeassistantRelease)" =~ b ]]; then
          create_manifest "beta" "$(homeassistantRelease)"
          create_manifest "rc" "$(homeassistantRelease)"
        else
          create_manifest "stable" "$(homeassistantRelease)"
          create_manifest "latest" "$(homeassistantRelease)"
          create_manifest "beta" "$(homeassistantRelease)"
          create_manifest "rc" "$(homeassistantRelease)"
        fi

      displayName: 'Create Meta-Image'

- stage: 'Addidional'
  jobs:
  - job: 'Updater'
    pool:
      vmImage: 'ubuntu-latest'
    variables:
    - group: gcloud
    steps:
    - template: templates/azp-step-ha-version.yaml@azure
    - script: |
        set -e

        export CLOUDSDK_CORE_DISABLE_PROMPTS=1

        curl -o google-cloud-sdk.tar.gz https://dl.google.com/dl/cloudsdk/release/google-cloud-sdk.tar.gz
        tar -C . -xvf google-cloud-sdk.tar.gz
        rm -f google-cloud-sdk.tar.gz
        ./google-cloud-sdk/install.sh
      displayName: 'Setup gCloud'
      condition: eq(variables['homeassistantReleaseStable'], 'true')
    - script: |
        set -e

        export CLOUDSDK_CORE_DISABLE_PROMPTS=1

        echo "$(gcloudAnalytic)" > gcloud_auth.json
        ./google-cloud-sdk/bin/gcloud auth activate-service-account --key-file gcloud_auth.json
        rm -f gcloud_auth.json
      displayName: 'Auth gCloud'
      condition: eq(variables['homeassistantReleaseStable'], 'true')
    - script: |
        set -e

        export CLOUDSDK_CORE_DISABLE_PROMPTS=1

        ./google-cloud-sdk/bin/gcloud functions deploy Analytics-Receiver \
          --project home-assistant-analytics \
          --update-env-vars VERSION=$(homeassistantRelease) \
          --source gs://analytics-src/function-source.zip
      displayName: 'Push details to updater'
      condition: eq(variables['homeassistantReleaseStable'], 'true')