---
name: tagged-release

on:
  push:
    tags:
      - 'v*.*.*'

jobs:
  dist:
    name: Checkout code and build warewulf.spec and dist
    runs-on: ubuntu-latest

    steps:
      - name: Set DIST
        run: |
          echo "DIST=warewulf-${GITHUB_REF#refs/tags/v}.tar.gz" >> $GITHUB_ENV

      - name: Checkout Code
        uses: actions/checkout@v4

      - name: Build spec and dist
        run: |
          make warewulf.spec dist

      - name: Upload spec and dist
        uses: actions/upload-artifact@v4
        with:
          name: spec-and-dist
          path: |
            warewulf.spec
            ${{ env.DIST }}

      - name: Attach dist to release
        uses: xresloader/upload-to-github-release@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          file: ${{ env.DIST }}
          tags: true
          draft: true

  rpm:
    name: Build RPMs
    needs: dist
    runs-on: ubuntu-latest
    container:
      image: rockylinux/rockylinux:9
      options: --privileged
    strategy:
      fail-fast: false
      matrix:
        include:
          - target: rocky+epel-8-x86_64
            arch: x86_64
            dist: el8
          - target: rocky+epel-9-x86_64
            arch: x86_64
            dist: el9
          - target: centos+epel-7-x86_64
            arch: x86_64
            dist: el7
          - target: opensuse-leap-15.5-x86_64
            arch: x86_64
            dist: suse.lp155

    steps:
      - name: Prepare mock and rpm-build
        run: |
          dnf -y install epel-release
          dnf -y install mock rpm-build
          echo "config_opts['print_main_output'] = True" >>/etc/mock/site-defaults.cfg

      - name: Download dist and spec
        uses: actions/download-artifact@v4
        with:
          name: spec-and-dist

      - name: Configure the environment
        run: |
          VERSION=$(rpm -q --qf "%{VERSION}\n" --specfile warewulf.spec)
          GENERIC_RELEASE=$(rpm -q --qf "%{RELEASE}\n" --specfile warewulf.spec | sed 's/\.[^.]*$//')
          RPM=warewulf-${VERSION}-${GENERIC_RELEASE}.${{ matrix.dist }}.${{ matrix.arch }}.rpm
          SRPM=warewulf-${VERSION}-${GENERIC_RELEASE}.${{ matrix.dist }}.src.rpm

          echo "EXPECTED_VERSION=${GITHUB_REF#refs/tags/v}" >> $GITHUB_ENV
          echo "RPM=${RPM}" >> $GITHUB_ENV
          echo "SRPM=${SRPM}" >> $GITHUB_ENV

      - name: Build RPMs and run tests
        run: |
          mock -r ${{ matrix.target }} --rebuild --spec=warewulf.spec --sources=.
          mock -r ${{ matrix.target }} --chroot -- make -C /builddir/build/BUILD/warewulf-${{ env.EXPECTED_VERSION }} test

      - name: Attach RPM and SRPM to release
        uses: xresloader/upload-to-github-release@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          file: "/var/lib/mock/${{ matrix.target }}/result/${{ env.RPM }};/var/lib/mock/${{ matrix.target }}/result/${{ env.SRPM }}"
          tags: true
          draft: true
