name: Build Dim Bin and Dockerize

on:
  push:
    branches: [ 'master', 'docker-build' ]
    tags: 'v*' 
  workflow_dispatch:

env:
  CARGO_TERM_COLOR: always
  
jobs:
  create-ui:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v3        
      - name: Build UI 
        uses: actions/setup-node@v3
        with:
            node-version: '18'
            cache: 'yarn'
      - run: npm install -g yarn
      - run: yarn -d --cwd ui/ install
        env:
            NODE_OPTIONS: --openssl-legacy-provider
      - run: yarn -d --cwd ui/ build
        env:
            NODE_OPTIONS: --openssl-legacy-provider
      - name: upload ui artifacts
        uses: actions/upload-artifact@v3
        with:
          name: ui
          path: ui/build

  build-armhf:
    needs: [create-ui]
    runs-on: ubuntu-latest
    container: 
      image: rust:1.63
      env:
        CARGO_TARGET_ARMV7_UNKNOWN_LINUX_GNUEABIHF_LINKER: arm-linux-gnueabihf-gcc
        OPENSSL_INCLUDE_DIR: "/usr/include/openssl/"
        OPENSSL_LIB_DIR: "/usr/lib/arm-linux-gnueabihf/"
        CARGO_TERM_COLOR: always
    steps:
      - name: add armhf architecture
        run: dpkg --add-architecture armhf
      - name: install runtime
        run: apt update && apt install -y pkg-config gcc-arm-linux-gnueabihf g++-arm-linux-gnueabihf libc6-armhf-cross libc6-dev-armhf-cross tar ca-certificates
      - name: Checkout repository
        uses: actions/checkout@v2
      - name: Download webui
        uses: actions/download-artifact@v3
        with:
          name: ui
          path: ui/build
      - uses: actions/cache@v3
        with:
          path: |
            .cargo/bin
            .cargo/registry/index
            .cargo/registry/cache
            .cargo/git/db
            target
          key: dim-bin-armhf-${{ hashFiles('**/Cargo.lock') }}
          restore-keys: |
            dim-bin-armhf-
      - name: add armhf target
        run: rustup target add armv7-unknown-linux-gnueabihf
      - name: smoke test
        run: rustc --version
      - name: compile armhf
        run: cargo build --target=armv7-unknown-linux-gnueabihf --release
      - name: check path
        run: ls -al target/
      - name: upload armhf artifacts
        uses: actions/upload-artifact@v3
        with:
          name: armhf-bin
          path: target/armv7-unknown-linux-gnueabihf/release/dim


  build-aarch64:
    needs: [create-ui]
    runs-on: ubuntu-latest
    container: 
      image: rust:1.63
      env:
        CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER: aarch64-linux-gnu-gcc
        OPENSSL_INCLUDE_DIR: "/usr/include/openssl/"
        OPENSSL_LIB_DIR: "/usr/lib/aarch64-linux-gnu/"
        CARGO_TERM_COLOR: always
    steps:
      - name: add arm64 architecture
        run: dpkg --add-architecture arm64
      - name: install runtime
        run: apt update && apt install -y gcc-aarch64-linux-gnu g++-aarch64-linux-gnu libc6-arm64-cross libc6-dev-arm64-cross libssl-dev:arm64
      - name: Checkout repository
        uses: actions/checkout@v2
      - name: Download webui
        uses: actions/download-artifact@v3
        with:
          name: ui
          path: ui/build
      - uses: actions/cache@v3
        with:
          path: |
            .cargo/bin
            .cargo/registry/index
            .cargo/registry/cache
            .cargo/git/db
            target
          key: dim-bin-aarch64-${{ hashFiles('**/Cargo.lock') }}
          restore-keys: |
            dim-bin-aarch64-
      - name: add arm64 target
        run: rustup target add aarch64-unknown-linux-gnu
      - name: smoke test
        run: rustc --version            
      - name: compile aarch64
        run: cargo build --target=aarch64-unknown-linux-gnu --release
      - name: check path
        run: ls -al target/
      - name: upload aarch64 artifacts
        uses: actions/upload-artifact@v3
        with:
          name: aarch64-bin
          path: target/aarch64-unknown-linux-gnu/release/dim
    
  build-amd64:
    needs: [create-ui]
    runs-on: ubuntu-latest
    container: 
      image: rust:1.63
      env:
        CARGO_TERM_COLOR: always
    steps:
      - name: install runtime
        run: apt update && apt install -y gcc-x86-64-linux-gnu g++-x86-64-linux-gnu libc6-dev libssl-dev libva-dev libva-drm2 libva2
      - name: Checkout repository
        uses: actions/checkout@v2
      - name: Download webui
        uses: actions/download-artifact@v3
        with:
          name: ui
          path: ui/build
      - uses: actions/cache@v3
        with:
          path: |
            .cargo/bin
            .cargo/registry/index
            .cargo/registry/cache
            .cargo/git/db
            target
          key: dim-bin-amd64-${{ hashFiles('**/Cargo.lock') }}
          restore-keys: |
            dim-bin-amd64-
      - name: add x86_64 target
        run: rustup target add x86_64-unknown-linux-gnu
      - name: smoke test
        run: rustc --version
      - name: compile amd64
        run: cargo build --features vaapi --target=x86_64-unknown-linux-gnu --release
      - name: check path
        run: ls -al target/
      - name: upload amd64 artifacts
        uses: actions/upload-artifact@v3
        with:
          name: amd64-bin
          path: target/x86_64-unknown-linux-gnu/release/dim

  build-docker-image:
    needs: [build-armhf,build-aarch64,build-amd64]
    name: Build Docker image
    runs-on: ubuntu-latest
    permissions:
      contents: read
      packages: write
    steps:
      - name: fetch repo
        uses: actions/checkout@v3
      - name: Download armhf dim artifacts
        uses: actions/download-artifact@v3
        with:
          name: armhf-bin
          path: bin/armhf-bin
      - name: Download armhf dim artifacts
        uses: actions/download-artifact@v3
        with:
          name: aarch64-bin
          path: bin/aarch64-bin
      - name: Download amd64 dim artifacts
        uses: actions/download-artifact@v3
        with:
          name: amd64-bin
          path: bin/amd64-bin

      - name: check path
        run: ls -al
      - name: check bin path
        run: ls -al bin/

      - name: setup qemu
        uses: docker/setup-qemu-action@v2
      - uses: docker/setup-buildx-action@v2
      - uses: docker/metadata-action@v4
        id: meta
        with:
          images: ghcr.io/${{ github.repository }}
          tags: |
            type=ref,event=branch
            type=ref,event=pr
            type=semver,pattern={{version}}
            type=semver,pattern={{major}}.{{minor}}
      - uses: docker/login-action@v2
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}
      - uses: docker/build-push-action@v4
        with:
          #push: true
          push: ${{ github.event_name != 'pull_request' }}
          context: .
          platforms: linux/amd64,linux/arm64,linux/arm/v7
          file: ./.github/workflows/Dockerfile.ci
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
          cache-to: type=gha,mode=max
          cache-from: type=gha

