name: Publish

# If a pull-request is pushed then cancel all previously running jobs related
# to that pull-request
concurrency:
  group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.sha }}
  cancel-in-progress: true

on:
  push:
    tags:
      - 'beaker-*'

jobs:
  build-python-package:
    strategy:
      matrix:
        component:
          - "Common"
          - "Client"
    name: Build python package
    runs-on: ubuntu-latest
    steps:
    - name: Checkout source code
      uses: actions/checkout@v3
    - name: Set up Python
      uses: actions/setup-python@v4
      with:
        python-version: "3.x"
    - name: Install python dependencies
      run: pip install --upgrade pip && pip install build
    - name: Build python package
      run: cd ${{ matrix.component }} && python -m build
    - name: Upload python artifacts
      uses: actions/upload-artifact@v4
      with:
        path: ${{ matrix.component }}/dist
        name: artifact-${{ matrix.component }}

  publish-python-package:
    needs:
      - "build-python-package"
    environment: production
    permissions:
      id-token: write
    strategy:
      matrix:
        component:
          - "Common"
          - "Client"
    name: Publish package to PyPI
    runs-on: ubuntu-latest
    steps:
    - name: Download artifacts
      uses: actions/download-artifact@v4
      with:
        name: artifact-${{ matrix.component }}
    - name: Display structure of downloaded files
      run: ls -R
    - name: Publish package distributions to PyPI
      uses: pypa/gh-action-pypi-publish@release/v1
      with:
        print-hash: true
        packages-dir: "./"

  release-rpm:
    runs-on: ubuntu-latest
    environment: production
    name: Build RPM packages in COPR
    container:
      image: fedora:39
    steps:
      - uses: actions/checkout@v1
      - name: Install release toolbox
        run: |
          dnf install git rpkg copr-cli /usr/bin/spectool -y
      - name: Setup Copr config file
        env:
          # https://copr.fedorainfracloud.org/api/.
          COPR_CONFIG_FILE: ${{ secrets.COPR_CONFIG }}
        run: |
          mkdir -p ~/.config
          printf "$COPR_CONFIG_FILE" > ~/.config/copr
      - name: Build new packages
        run: |
          spectool -g beaker.spec
          rpkg copr-build @beaker-project/beaker
