name: Release
on:
  push:
    tags:
      - '*.*.*'
  workflow_dispatch:
    inputs:
      bintray_subject:
        description: 'Bintray Subject (leave empty to not publish)'

jobs:
  qa:
    name: ${{ matrix.platform }}-${{ matrix.runner }}
    if: github.repository == 'performancecopilot/pcp' || github.event_name == 'workflow_dispatch'
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: true
      matrix:
        include:
          - {platform: ubuntu1604,     runner: container, os: ubuntu-latest}
          - {platform: ubuntu1804,     runner: direct,    os: ubuntu-18.04}
          - {platform: ubuntu1804i386, runner: container, os: ubuntu-latest}
          - {platform: ubuntu2004,     runner: direct,    os: ubuntu-20.04}
          - {platform: debian10,       runner: container, os: ubuntu-latest}
          - {platform: debian11,       runner: container, os: ubuntu-latest}
          - {platform: fedora31,       runner: container, os: ubuntu-latest}
          - {platform: fedora32,       runner: container, os: ubuntu-latest}
          - {platform: fedora33,       runner: container, os: ubuntu-latest}
          - {platform: centos6,        runner: container, os: ubuntu-latest}
          - {platform: centos7,        runner: container, os: ubuntu-latest}
          - {platform: centos8,        runner: container, os: ubuntu-latest}
    steps:
      - name: Checkout sources
        uses: actions/checkout@v2

      - name: Set up Python 3.6+
        if: matrix.platform == 'ubuntu1604'
        uses: actions/setup-python@v2
        with:
          python-version: '>=3.6'

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

          for i in $(seq 1 10)
          do
            if [ $i -gt 1 ]; then
              echo && echo "Probably network error, wait 5 min and try again ($i/10)..."
              sleep 300
              sudo podman system prune -a -f
            fi

            ./build/ci/run.py --runner ${{ matrix.runner }} --platform ${{ matrix.platform }} setup && exit 0 || true
          done
          exit 1

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

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

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

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

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

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

      - name: QA
        run: ./build/ci/run.py --runner ${{ matrix.runner }} --platform ${{ matrix.platform }} task qa_sanity

      - name: Copy test results
        if: always()
        run: ./build/ci/run.py --runner ${{ matrix.runner }} --platform ${{ matrix.platform }} artifacts test --path ./artifacts/test

      - name: Publish test results
        if: always()
        uses: actions/upload-artifact@v2
        with:
          name: test-${{ matrix.platform }}-${{ matrix.runner }}
          path: ./artifacts/test

  release:
    name: Release on Bintray
    if: (github.event_name == 'push' && github.repository == 'performancecopilot/pcp') || (github.event_name == 'workflow_dispatch' && github.event.inputs.bintray_subject != '')
    needs: qa
    runs-on: ubuntu-latest
    steps:
      - name: Checkout sources
        uses: actions/checkout@v2

      - name: Download all artifacts
        uses: actions/download-artifact@v2
        with:
          path: artifacts

      - name: Create source tarball
        run: ./Makepkgs --source --nonrpm

      - name: Release on Bintray
        run: |
          export PYTHONUNBUFFERED=1
          . ./VERSION.pcp
          ./build/ci/release-bintray.py --version "$PACKAGE_MAJOR.$PACKAGE_MINOR.$PACKAGE_REVISION" --source ./build/tar/*.src.tar.gz ./artifacts/build-*
        env:
          BINTRAY_SUBJECT: ${{ github.event.inputs.bintray_subject || 'pcp' }}
          BINTRAY_USER: ${{ secrets.BINTRAY_USER }}
          BINTRAY_APIKEY: ${{ secrets.BINTRAY_APIKEY }}
          BINTRAY_GPG_PASSPHRASE: ${{ secrets.BINTRAY_GPG_PASSPHRASE }}
