name: ARM builds

on:
  push:
    branches:
      - arm-build
  schedule:
    - cron:  '0 4 * * 2' # Tuesday at 4:00 UTC
  workflow_dispatch:

jobs:
  Build:
    if: github.repository == 'CESNET/UltraGrid' || github.event.schedule == null
    runs-on: ubuntu-22.04 # TODO: see the commit message why 22.04 is explicit
    env:
      GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
    strategy:
      matrix:
        arch: [armhf, arm64]
        include:
          - arch: armhf
            repo: http://mirrordirector.raspbian.org/raspbian/
            keyring: /etc/apt/trusted.gpg
            qemu_bin_arch: arm
            deb_release: bullseye
          - arch: arm64
            repo: https://deb.debian.org/debian
            keyring: /usr/share/keyrings/debian-archive-keyring.gpg
            qemu_bin_arch: aarch64
            deb_release: bullseye

    steps:
    - uses: actions/checkout@v4
    - name: Init environment variables
      run: . .github/scripts/environment.sh
    - name: Prepare
      run: |
        sudo apt update
        sudo apt install binfmt-support curl qemu-user-static

    - name: Fetch SDKs ETags
      id: etags
      run: |
        $GITHUB_WORKSPACE/.github/scripts/get-etag.sh ndi\
         https://downloads.ndi.tv/SDK/NDI_SDK_Linux/\
        Install_NDI_SDK_v6_Linux.tar.gz >> $GITHUB_OUTPUT
    - name: Run actions/cache for NDI
      id: cache-ndi
      uses: actions/cache@main
      with:
        path: /var/tmp/Install_NDI_SDK_Linux.tar.gz
        key: cache-ndi-${{ runner.os }}-${{ steps.etags.outputs.ndi }}
    - name: Download NDI
      if: steps.cache-ndi.outputs.cache-hit != 'true'
      run: "curl -Lf https://downloads.ndi.tv/SDK/NDI_SDK_Linux/\
       Install_NDI_SDK_v6_Linux.tar.gz -o /var/tmp/Install_NDI_SDK_Linux.tar.gz"

    - name: Run actions/cache for ARM Chroot
      id: cache-bootstrap
      uses: actions/cache@main
      with:
        path: '~/chroot.tar'
        key: cache-bootstrap-${{ env.ImageOS }}-${{ matrix.arch }}-${{
          hashFiles('.github/scripts/Linux/arm/bootstrap.sh',
          '.github/scripts/Linux/install_others.sh') }}
    - name: Debootstrap
      if: steps.cache-bootstrap.outputs.cache-hit != 'true'
      run: |
        sudo apt install debootstrap debian-archive-keyring
        sudo rm -rf ~/chroot
        sudo mkdir -p ~/chroot/var/tmp
        sudo mv /var/tmp/*NDI* ~/chroot/var/tmp
        wget http://archive.raspbian.org/raspbian.public.key -O - | sudo apt-key add -q
        sudo debootstrap --keyring=${{ matrix.keyring }} --arch ${{ matrix.arch }} ${{ matrix.deb_release }} ~/chroot ${{ matrix.repo }}
        sudo cp -r $GITHUB_WORKSPACE/.github ~/chroot/
        sudo cp /usr/bin/qemu-${{ matrix.qemu_bin_arch }}-static ~/chroot/usr/bin/
        sudo -EH chroot ~/chroot /.github/scripts/Linux/arm/bootstrap.sh ${{ matrix.arch }}
        cd ~ && sudo tar cf chroot.tar chroot # Create archive for caching. Needs to be created as root, therefore created manually.
    - name: Extract Cached Chroot
      if: steps.cache-bootstrap.outputs.cache-hit == 'true'
      run: |
        cd ~
        sudo tar xf chroot.tar
    - name: Build
      run: |
        sudo cp -ar $GITHUB_WORKSPACE ~/chroot/UltraGrid
        sudo -EH chroot ~/chroot /bin/sh -ec "cd UltraGrid; .github/scripts/Linux/arm/build.sh"
    - name: Upload Release Asset
      id: upload-release-asset
      if: github.repository == 'CESNET/UltraGrid'
      run: |
        sudo apt install jq
        sudo .github/scripts/replace-asset.sh GITHUB_REPOSITORY=$GITHUB_REPOSITORY GITHUB_TOKEN=${{ secrets.GITHUB_TOKEN }} continuous ~/chroot/UltraGrid/UltraGrid-latest-${{ matrix.arch }}.AppImage application/x-appimage Linux%20${{ matrix.arch }}%20build
    - name: Upload Build
      if: steps.upload-release-asset.conclusion == 'skipped'
      uses: actions/upload-artifact@main
      with:
        name: UltraGrid CI ${{ matrix.arch }} build
        path: '~/chroot/UltraGrid/UltraGrid-latest-${{ matrix.arch }}.AppImage'

# vi: set expandtab sw=2:
