# Deploys PCP to Packagecloud
name: Release
on:
  push:
    tags:
      - '*.*.*'
      - '!*alpha'
      - '!*beta'
  workflow_dispatch:

permissions:
  contents: write

jobs:
  qa:
    name: ${{ matrix.platform }}
    if: github.repository == 'performancecopilot/pcp' || github.event_name == 'workflow_dispatch'
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        include:
          # run all PCP builds in containers, because the GitHub Actions images contain
          # different packages than the stock Ubuntu releases
          - {platform: debian11-container,        os: ubuntu-latest}
          - {platform: debian12-container,        os: ubuntu-latest}
          - {platform: debian13-container,        os: ubuntu-latest}
          - {platform: ubuntu1804-container,      os: ubuntu-latest}
          - {platform: ubuntu2004-container,      os: ubuntu-latest}
          - {platform: ubuntu2204-container,      os: ubuntu-latest}
          - {platform: ubuntu2404-container,      os: ubuntu-latest}
          - {platform: ubuntu2604-container,      os: ubuntu-latest}
          - {platform: fedora42-container,        os: ubuntu-22.04}
          - {platform: fedora43-container,        os: ubuntu-22.04}
          - {platform: fedora44-container,        os: ubuntu-22.04}
          - {platform: centos-stream8-container,  os: ubuntu-latest}
          - {platform: centos-stream9-container,  os: ubuntu-22.04}
          - {platform: centos-stream10-container, os: ubuntu-22.04}
          - {platform: amazonlinux2023-container, os: ubuntu-22.04}
          - {platform: amazonlinux2023-aarch64-container, os: ubuntu-22.04-arm}
    steps:
      - name: Checkout sources
        uses: actions/checkout@v7

      - 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
          touch artifacts/build/.keep
          # crun >= 1.9.1 is required on Ubuntu 20.04.6
          # this workaround came from ...
          # https://github.com/kubernetes-sigs/kind/pull/3527
          ARCH=$(arch | sed -e 's/x86_64/amd64/g' -e 's/aarch64/arm64/g')
          curl -sLo /tmp/crun https://github.com/containers/crun/releases/download/1.14.3/crun-1.14.3-linux-$ARCH
          chmod +x /tmp/crun
          sudo cp /tmp/crun /usr/bin/crun

          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@v7
        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@v7
        with:
          name: test-${{ matrix.platform }}
          path: artifacts/test

  release:
    name: Release packages
    if: github.repository == 'performancecopilot/pcp' || github.event_name == 'workflow_dispatch'
    needs: qa
    runs-on: ubuntu-latest
    steps:
      - name: Checkout sources
        uses: actions/checkout@v7

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

      - name: Discover artifacts
        run: find artifacts | xargs ls -l

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

      - name: Release on Packagecloud
        run: build/ci/packagecloud.sh
        env:
          PACKAGECLOUD_TOKEN: ${{ secrets.PACKAGECLOUD_TOKEN }}
