name: "Docker tests"

on:
  push:
    branches:
      - main
      - release-*
  pull_request:
    paths:
      - '.github/workflows/tests_docker.yml'
      - '**/Dockerfile*'
      - 'browsers.json'
      - 'package.json'
    branches:
      - main
      - release-*

env:
  # Force terminal colors. @see https://www.npmjs.com/package/colors
  FORCE_COLOR: 1
  FLAKINESS_CONNECTION_STRING: ${{ secrets.FLAKINESS_CONNECTION_STRING }}

jobs:
  test_linux_docker:
    name: "Docker Ubuntu-${{ matrix.tag }}-${{ matrix.user }} Tests"
    runs-on: ubuntu-20.04
    strategy:
      fail-fast: false
      matrix:
        tag: [bionic, focal]
        user: [pwuser, root]
    steps:
    - uses: actions/checkout@v2
    - uses: actions/setup-node@v2
      with:
        node-version: 14
    - run: npm i -g npm@8
    - run: npm ci
    - run: npm run build
    - name: Build
      run: bash utils/docker/build.sh --amd64 ${{ matrix.tag }} playwright:localbuild-${{ matrix.tag }}
    - name: Clean & Prepare for Docker
      run: |
        npm run clean
        rm -rf node_modules/
        if [[ ${{ matrix.user }} == 'root' ]]; then
          sudo chown -R 0 $(pwd)
        else
          sudo chown -R 1000 $(pwd)
        fi
    - name: Launch container
      run: docker run --rm --user=${{ matrix.user }} -v $(pwd):/tmp/playwright --workdir /tmp/playwright/ --name playwright-docker-${{ matrix.tag }}-test -d -t playwright:localbuild-${{ matrix.tag }} /bin/bash
    - name: Run "npm ci" inside docker
      run: docker exec playwright-docker-${{ matrix.tag }}-test npm ci
    - name: Run "npm run build" inside docker
      run: docker exec playwright-docker-${{ matrix.tag }}-test npm run build
    # Test the channel installation on root user
    - name: Test installing Chrome&MSEdge
      if: matrix.user == 'root'
      run: docker exec playwright-docker-${{ matrix.tag }}-test npx playwright install chrome msedge
    - name: Run "npm run test" inside docker
      run: docker exec -e INSIDE_DOCKER=1 -e CI=1 playwright-docker-${{ matrix.tag }}-test xvfb-run --auto-servernum --server-args="-screen 0 1280x960x24" npm run test
    - run: sudo chmod -R 777 . && ./utils/upload_flakiness_dashboard.sh ./test-results/report.json
      if: always()
    - uses: actions/upload-artifact@v1
      if: ${{ always() }}
      with:
        name: docker-ubuntu-${{ matrix.tag }}-${{ matrix.user }}-test-results
        path: test-results

