# Deploys PCP to JFrog Artifactory
name: Release
on:
  push:
    tags:
      - '*.*.*'
  workflow_dispatch:

jobs:
  qa:
    name: ${{ matrix.platform }}
    if: github.repository == 'performancecopilot/pcp' || github.event_name == 'workflow_dispatch'
    runs-on: ubuntu-20.04
    strategy:
      fail-fast: false
      matrix:
        platform:
          # run all PCP builds in containers, because the GitHub Actions images contain
          # different packages than the stock Ubuntu releases
          - debian10-container
          - debian11-container
          - ubuntu1604-container
          - ubuntu1804-container
          - ubuntu2004-container
          - fedora33-container
          - fedora34-container
          - fedora35-container
          - centos6-container
          - centos7-container
          - centos8-container
    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: 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

  release:
    name: Release on Artifactory
    if: github.repository == 'performancecopilot/pcp' || github.event_name == 'workflow_dispatch'
    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 Artifactory
        run: |
          export PYTHONUNBUFFERED=1
          . VERSION.pcp
          build/ci/deploy-artifactory.py \
            --maturity release \
            --version "$PACKAGE_MAJOR.$PACKAGE_MINOR.$PACKAGE_REVISION" \
            --build_name "${GITHUB_WORKFLOW}" \
            --build_number "${GITHUB_RUN_ID}" \
            --source build/tar/*.src.tar.gz \
            --exclude 'pcp-testsuite.*' \
            --exclude 'pcp-.+\.src\.rpm' \
            artifacts/build-*
        env:
          ARTIFACTORY_USER: github-actions
          ARTIFACTORY_TOKEN: ${{ secrets.ARTIFACTORY_TOKEN }}
          ARTIFACTORY_GPG_PASSPHRASE: ${{ secrets.ARTIFACTORY_GPG_PASSPHRASE }}
