name: tools
on:
  push:
  pull_request:

env:
  BASE_IMAGE: openpilot-base
  CL_BASE_IMAGE: openpilot-base-cl
  DOCKER_REGISTRY: ghcr.io/commaai
  DOCKER_LOGIN: docker login ghcr.io -u ${{ github.actor }} -p ${{ secrets.GITHUB_TOKEN }}

  BUILD: |
      docker pull $(grep -iohP '(?<=^from)\s+\S+' Dockerfile.openpilot_base) || true
      docker pull $DOCKER_REGISTRY/$BASE_IMAGE:latest || true
      docker build --cache-from $DOCKER_REGISTRY/$BASE_IMAGE:latest -t $DOCKER_REGISTRY/$BASE_IMAGE:latest -t $BASE_IMAGE:latest -f Dockerfile.openpilot_base .
  BUILD_CL: |
      docker pull $(grep -iohP '(?<=^from)\s+\S+' Dockerfile.openpilot_base_cl) || true
      docker pull $DOCKER_REGISTRY/$BASE_IMAGE:latest || true
      docker build --cache-from $DOCKER_REGISTRY/$CL_BASE_IMAGE:latest -t $DOCKER_REGISTRY/$CL_BASE_IMAGE:latest -t $CL_BASE_IMAGE:latest -f Dockerfile.openpilot_base_cl .
  RUN: docker run --shm-size 1G -v $PWD:/tmp/openpilot -e PYTHONPATH=/tmp/openpilot -e GITHUB_ACTION -e GITHUB_REF -e GITHUB_HEAD_REF -e GITHUB_SHA -e \
       GITHUB_REPOSITORY -e GITHUB_RUN_ID -v /tmp/comma_download_cache:/tmp/comma_download_cache $BASE_IMAGE /bin/sh -c

jobs:
  build_latest_ubuntu:
    name: build latest ubuntu
    runs-on: ubuntu-20.04
    timeout-minutes: 60
    steps:
    - uses: actions/checkout@v3
      with:
        submodules: true
    - name: Cache pyenv
      id: ubuntu-latest-pyenv
      uses: actions/cache@v3
      with:
        path: |
          ~/.pyenv
          ~/.local/share/virtualenvs/
        key: ubuntu-latest-python-${{ hashFiles('tools/ubuntu_setup.sh') }}-
    - name: Cache scons
      id: ubuntu-latest-scons
      uses: actions/cache@v3
      with:
        path: /tmp/scons_cache
        key: ubuntu-latest-scons-${{ hashFiles('.github/workflows/tools_test.yaml') }}-
        restore-keys: |
          ubuntu-latest-scons-${{ hashFiles('.github/workflows/tools_test.yaml') }}-
          ubuntu-latest-scons-

    - name: tools/ubuntu_setup.sh
      run: |
        source tools/openpilot_env.sh
        tools/ubuntu_setup.sh
    - name: Build openpilot
      run: |
        source tools/openpilot_env.sh
        pipenv run scons -j$(nproc) --extras --test
    - name: Cleanup scons cache
      run: |
        source tools/openpilot_env.sh
        rm -rf /tmp/scons_cache/*
        pipenv run scons -j$(nproc) --extras --test --cache-populate

  plotjuggler:
    name: plotjuggler
    runs-on: ubuntu-20.04
    timeout-minutes: 30
    steps:
    - uses: actions/checkout@v3
      with:
        submodules: true
    - name: Build Docker image
      run: eval "$BUILD"
    - name: Unit test
      run: |
        ${{ env.RUN }} "scons -j$(nproc) --directory=/tmp/openpilot/cereal && \
                        apt-get update && \
                        apt-get install -y libdw-dev libqt5svg5-dev libqt5x11extras5-dev && \
                        cd /tmp/openpilot/tools/plotjuggler && \
                        ./test_plotjuggler.py"

  simulator:
    name: simulator
    runs-on: ubuntu-20.04
    timeout-minutes: 50
    env:
      IMAGE_NAME: openpilot-sim
    if: github.repository == 'commaai/openpilot'
    steps:
    - uses: actions/checkout@v3
      with:
        submodules: true
    - name: Pull LFS
      run: git lfs pull
    - name: Build base image
      run: eval "$BUILD"
    - name: Build base cl image
      run: eval "$BUILD_CL"
    - name: Pull latest simulator image
      run: docker pull $DOCKER_REGISTRY/$IMAGE_NAME:latest || true
    - name: Build simulator image
      run: docker build --cache-from $DOCKER_REGISTRY/$IMAGE_NAME:latest -t $DOCKER_REGISTRY/$IMAGE_NAME:latest -f tools/sim/Dockerfile.sim .
    - name: Push to container registry
      if: github.ref == 'refs/heads/master' && github.repository == 'commaai/openpilot'
      run: |
        $DOCKER_LOGIN
        docker push $DOCKER_REGISTRY/$IMAGE_NAME:latest
