name: docker

on:
  push:
    branches:
      - 'master'
  pull_request:
    branches:
      - '**'

concurrency:
  group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
  cancel-in-progress: ${{ github.event_name == 'pull_request' }}

jobs:
  docker_deps:

    # Disabling ###-deps step from CI because it is too flaky (seg faults, arm64 build timeout...)
    # Only way I was able to build all images is to do it from a ubuntu 20.04 computer with:
    #    $ sudo add-apt-repository ppa:canonical-server/server-backports
    #    $ sudo apt-get update
    #    $ sudo apt-get upgrade qemu-user-static
    #    $ docker run --rm --privileged multiarch/qemu-user-static --reset -p yes -c yes
    # More info: https://github.com/introlab/rtabmap/issues/1454
    # Skipped on pull requests; built and pushed only on push to master.
    if: github.event_name != 'pull_request'

    runs-on: ubuntu-latest
    
    strategy:
      fail-fast: false
      matrix:
        docker_tag: [focal-deps, jammy-deps, noble-deps, noble-kilted-deps, resolute-deps]
        include:
        - docker_tag: focal-deps
          docker_tags: |
            introlab3it/rtabmap:focal-deps
          docker_platforms: |
            linux/amd64
            linux/arm64
          docker_path: 'focal/deps'
        - docker_tag: jammy-deps
          docker_tags: |
            introlab3it/rtabmap:jammy-deps
          docker_platforms: |
            linux/amd64
            linux/arm64
          docker_path: 'jammy/deps'
        - docker_tag: noble-deps
          docker_tags: |
            introlab3it/rtabmap:noble-deps
          docker_platforms: |
            linux/amd64
            linux/arm64
          docker_path: 'noble/deps'
        - docker_tag: noble-kilted-deps
          docker_tags: |
            introlab3it/rtabmap:noble-kilted-deps
          docker_platforms: |
            linux/amd64
            linux/arm64
          docker_path: 'noble-kilted/deps'
        - docker_tag: resolute-deps
          docker_tags: |
            introlab3it/rtabmap:resolute-deps
          docker_platforms: |
            linux/amd64
            linux/arm64
          docker_path: 'resolute/deps'
    
    steps:
      -
        name: Checkout
        uses: actions/checkout@v2
      - 
        name: Set up QEMU
        uses: docker/setup-qemu-action@v3
        with:
          platforms: all
      -
        name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3
      -
        name: Login to DockerHub
        uses: docker/login-action@v3
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}
      -
        name: Build and push
        uses: docker/build-push-action@v6
        with:
          context: .
          push: true
          platforms: ${{ matrix.docker_platforms }}
          file: ./docker/${{ matrix.docker_path }}/Dockerfile
          tags: ${{ matrix.docker_tags }}
          cache-from: type=registry,ref=introlab3it/rtabmap:${{ matrix.docker_tag }}
          cache-to: type=inline
  
  docker:
    needs: docker_deps
    # Run even when docker_deps is skipped (it is, on pull requests).
    if: ${{ !cancelled() && !failure() }}
    runs-on: ubuntu-latest
    
    strategy:
      fail-fast: false
      matrix:
        docker_tag: [bionic, focal, jammy, noble, noble-kilted, resolute, android23, android24, android26, android30]
        include:
        - docker_tag: bionic
          docker_tags: |
            introlab3it/rtabmap:bionic
            introlab3it/rtabmap:18.04
          docker_args: |
            NOT_USED=0
          docker_platforms: |
            linux/amd64
            linux/arm64
          docker_path: 'bionic'
        - docker_tag: focal
          docker_tags: |
            introlab3it/rtabmap:focal
            introlab3it/rtabmap:20.04
            introlab3it/rtabmap:latest
          docker_args: |
            NOT_USED=0
          docker_platforms: |
            linux/amd64
            linux/arm64
          docker_path: 'focal'
        - docker_tag: jammy
          docker_tags: |
            introlab3it/rtabmap:jammy
            introlab3it/rtabmap:22.04
          docker_args: |
            NOT_USED=0
          docker_platforms: |
            linux/amd64
            linux/arm64
          docker_path: 'jammy'
        - docker_tag: noble
          docker_tags: |
            introlab3it/rtabmap:noble
            introlab3it/rtabmap:24.04
          docker_args: |
            NOT_USED=0
          docker_platforms: |
            linux/amd64
            linux/arm64
          docker_path: 'noble'
        - docker_tag: noble-kilted
          docker_tags: |
            introlab3it/rtabmap:noble-kilted
          docker_args: |
            NOT_USED=0
          docker_platforms: |
            linux/amd64
            linux/arm64
          docker_path: 'noble-kilted'
        - docker_tag: resolute
          docker_tags: |
            introlab3it/rtabmap:resolute
            introlab3it/rtabmap:26.04
          docker_args: |
            NOT_USED=0
          docker_platforms: |
            linux/amd64
            linux/arm64
          docker_path: 'resolute'
        - docker_tag: android23
          docker_tags: |
            introlab3it/rtabmap:android23
            introlab3it/rtabmap:tango
          docker_args: |
            API_VERSION=23
          docker_platforms: |
            linux/amd64
          docker_path: 'noble/android/rtabmap_apiXX'
        - docker_tag: android24
          docker_tags: |
            introlab3it/rtabmap:android24
          docker_args: |
            API_VERSION=24
          docker_platforms: |
            linux/amd64
          docker_path: 'noble/android/rtabmap_apiXX'
        - docker_tag: android26
          docker_tags: |
            introlab3it/rtabmap:android26
          docker_args: |
            API_VERSION=26
          docker_platforms: |
            linux/amd64
          docker_path: 'noble/android/rtabmap_apiXX'
        - docker_tag: android30
          docker_tags: |
            introlab3it/rtabmap:android30
          docker_args: |
            API_VERSION=30
          docker_platforms: |
            linux/amd64
          docker_path: 'noble/android/rtabmap_apiXX'
    
    steps:
      -
        name: Checkout
        uses: actions/checkout@v2
      - 
        name: Set up QEMU
        uses: docker/setup-qemu-action@v3
        with:
          platforms: all
      -
        name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3
      -
        name: Login to DockerHub
        # Only needed when pushing; skipped on pull requests (secrets are
        # unavailable for fork PRs and we don't push there anyway).
        if: github.event_name != 'pull_request'
        uses: docker/login-action@v3
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}
      -
        name: Build and push
        uses: docker/build-push-action@v6
        with:
          context: .
          push: ${{ github.event_name != 'pull_request' }}
          platforms: ${{ github.event_name == 'pull_request' && 'linux/amd64' || matrix.docker_platforms }}
          file: ./docker/${{ matrix.docker_path }}/Dockerfile
          build-args: |
            ${{ matrix.docker_args }}
          tags: ${{ matrix.docker_tags }}
          cache-from: type=registry,ref=introlab3it/rtabmap:${{ matrix.docker_tag }}
          cache-to: type=inline

