name: tools

on:
  push:
    branches:
      - master
  pull_request:

concurrency:
  group: ${{ github.event_name == 'push' && github.ref == 'refs/heads/master' && github.run_id || github.head_ref || github.ref }}-${{ github.workflow }}-${{ github.event_name }}
  cancel-in-progress: true

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

  BUILD: selfdrive/test/docker_build.sh base

  RUN: docker run --shm-size 1G -v $GITHUB_WORKSPACE:/tmp/openpilot -w /tmp/openpilot -e FILEREADER_CACHE=1 -e PYTHONPATH=/tmp/openpilot -e NUM_JOBS -e JOB_ID -e GITHUB_ACTION -e GITHUB_REF -e GITHUB_HEAD_REF -e GITHUB_SHA -e GITHUB_REPOSITORY -e GITHUB_RUN_ID -v $GITHUB_WORKSPACE/.ci_cache/scons_cache:/tmp/scons_cache -v $GITHUB_WORKSPACE/.ci_cache/comma_download_cache:/tmp/comma_download_cache -v $GITHUB_WORKSPACE/.ci_cache/openpilot_cache:/tmp/openpilot_cache $BASE_IMAGE /bin/sh -c

  BUILD_CL: selfdrive/test/docker_build.sh cl

  RUN_CL: docker run --shm-size 1G -v $GITHUB_WORKSPACE:/tmp/openpilot -w /tmp/openpilot -e PYTHONPATH=/tmp/openpilot -e NUM_JOBS -e JOB_ID -e GITHUB_ACTION -e GITHUB_REF -e GITHUB_HEAD_REF -e GITHUB_SHA -e GITHUB_REPOSITORY -e GITHUB_RUN_ID -v $GITHUB_WORKSPACE/.ci_cache/scons_cache:/tmp/scons_cache -v $GITHUB_WORKSPACE/.ci_cache/comma_download_cache:/tmp/comma_download_cache -v $GITHUB_WORKSPACE/.ci_cache/openpilot_cache:/tmp/openpilot_cache $CL_BASE_IMAGE /bin/sh -c


jobs:
  plotjuggler:
    name: plotjuggler
    runs-on: ubuntu-20.04
    timeout-minutes: 45
    steps:
    - uses: actions/checkout@v3
      with:
        submodules: true
    - name: Build Docker image
      run: eval "$BUILD"
    - name: Build openpilot
      timeout-minutes: 5
      run: ${{ env.RUN }} "scons -j$(nproc) --directory=/tmp/openpilot/cereal --minimal"
    - name: Test PlotJuggler
      timeout-minutes: 2
      run: |
        ${{ env.RUN }} "pytest tools/plotjuggler/"

  simulator:
    name: simulator
    runs-on: ubuntu-20.04
    if: github.repository == 'commaai/openpilot'
    timeout-minutes: 45
    steps:
    - uses: actions/checkout@v3
      with:
        submodules: true
    - uses: ./.github/workflows/setup
    - name: Build base cl image
      run: eval "$BUILD_CL"
    - name: Setup to push to repo
      if: github.ref == 'refs/heads/master' && github.repository == 'commaai/openpilot'
      run: |
        echo "PUSH_IMAGE=true" >> "$GITHUB_ENV"
        $DOCKER_LOGIN
    - name: Build and push sim image
      run: |
        selfdrive/test/docker_build.sh sim

  docs:
    name: build docs
    runs-on: ubuntu-20.04
    timeout-minutes: 45
    steps:
    - uses: actions/checkout@v3
      with:
        submodules: true
    - uses: ./.github/workflows/setup
      with:
        git_lfs: false
    - name: Setup to push to repo
      if: github.ref == 'refs/heads/master' && github.event_name != 'pull_request' && github.repository == 'commaai/openpilot'
      run: |
        echo "PUSH_IMAGE=true" >> "$GITHUB_ENV"
        $DOCKER_LOGIN
    - name: Build and push docs image
      run: |
        selfdrive/test/docker_build.sh docs

  devcontainer:
    name: devcontainer
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v3
      with:
        submodules: true
    - name: Setup Dev Container CLI
      run: npm install -g @devcontainers/cli
    - name: Build dev container image
      run: devcontainer build --workspace-folder .
    - name: Run dev container
      run: devcontainer up --workspace-folder .
    - name: Test environment
      run: devcontainer exec --workspace-folder . scons --dry-run
