# Runs the sanity testsuite
name: CI
on:
  push:
    # run on all branches, no tags
    branches:
      - '**'
    tags-ignore:
      - '**'
  pull_request:

jobs:
  qa:
    name: ${{ matrix.platform }}
    runs-on: ${{ matrix.os }}
    continue-on-error: ${{ matrix.experimental }}
    strategy:
      fail-fast: false
      matrix:
        include:
# default platforms for CI in the performancecopilot/pcp repo
          - {platform: ubuntu1604-container,      os: ubuntu-20.04, experimental: false}
          - {platform: ubuntu1804-i386-container, os: ubuntu-20.04, experimental: false}
          - {platform: ubuntu2004-container,      os: ubuntu-20.04, experimental: false}
          - {platform: fedora34-container,        os: ubuntu-20.04, experimental: false}
          - {platform: fedora35-container,        os: ubuntu-20.04, experimental: false}
          - {platform: centos8-container,         os: ubuntu-20.04, experimental: false}
# other platforms that may be used for testing in developer repos
#          - {platform: debian10-container,        os: ubuntu-20.04, experimental: false}
#          - {platform: debian11-container,        os: ubuntu-20.04, experimental: false}
#          - {platform: ubuntu1804-container,      os: ubuntu-20.04, experimental: false}
#          - {platform: ubuntu2004-btf,            os: ubuntu-20.04, experimental: false}
#          - {platform: fedora35-container,        os: ubuntu-20.04, experimental: true }
#          - {platform: fedora-rawhide-container,  os: ubuntu-20.04, experimental: true }
#          - {platform: centos6-container,         os: ubuntu-20.04, experimental: false}
#          - {platform: centos7-container,         os: ubuntu-20.04, experimental: false}
    steps:
      - name: Checkout sources
        uses: actions/checkout@v2

      - name: Fix local hostname DNS lookup
        run: echo 127.0.1.1 $(hostname --fqdn) $(hostname) | sudo tee -a /etc/hosts

      - name: Update version information
        run: |
          . VERSION.pcp
          PACKAGE_BUILD="0.$(date +'%Y%m%d').$(git rev-parse --short HEAD)"
          PCP_VERSION=${PACKAGE_MAJOR}.${PACKAGE_MINOR}.${PACKAGE_REVISION}
          PCP_BUILD_VERSION=${PCP_VERSION}-${PACKAGE_BUILD}
          sed -i "s/PACKAGE_BUILD=.*/PACKAGE_BUILD=${PACKAGE_BUILD}/" VERSION.pcp
          sed -i "1 s/(.*)/(${PCP_BUILD_VERSION})/" debian/changelog

      - name: Setup
        run: |
          python3 -c 'import yaml' || pip3 install pyyaml
          mkdir -p artifacts/build artifacts/test

          build/ci/ci-run.py ${{ matrix.platform }} setup

      - name: Build
        run: build/ci/ci-run.py ${{ matrix.platform }} task build

      - name: Copy build artifacts
        run: build/ci/ci-run.py ${{ matrix.platform }} artifacts build --path artifacts/build

      - name: Publish build artifacts
        uses: actions/upload-artifact@v2
        with:
          name: build-${{ matrix.platform }}
          path: artifacts/build

      - name: Install
        run: build/ci/ci-run.py ${{ matrix.platform }} task install

      - name: Initialize QA
        id: init_qa
        run: build/ci/ci-run.py ${{ matrix.platform }} task init_qa

      - name: QA (sanity group)
        run: build/ci/ci-run.py ${{ matrix.platform }} task qa_sanity

      - name: Copy test results
        # always() is required here to run this step even if the QA step fails
        if: always() && steps.init_qa.outcome == 'success'
        run: build/ci/ci-run.py ${{ matrix.platform }} artifacts test --path artifacts/test

      - name: Publish test results
        if: always() && steps.init_qa.outcome == 'success'
        uses: actions/upload-artifact@v2
        with:
          name: test-${{ matrix.platform }}
          path: artifacts/test
