name: Distribution

on: push

jobs:
  pypi:
    if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/')
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
    - run: python setup.py sdist
    - name: Publish distribution 📦 to PyPI
      uses: pypa/gh-action-pypi-publish@master
      with:
        user: __token__
        password: ${{ secrets.pypi_token }}

  copr-build:
    name: Build rpm in Fedora Copr
    runs-on: ubuntu-latest
    needs: pypi
    steps:
    - name: Setup python
      uses: actions/setup-python@v2
      with:
        python-version: 3.8
    - name: Install copr-cli
      run: |
        python -m pip install copr-cli
    - name: Write Copr configuration
      run: echo "${{ secrets.copr_conf }}" > copr_conf
    - name: Start to build the package
      run: |
        copr-cli --conf ./copr_conf buildscm \
          --method make_srpm \
          --type git \
          --clone-url https://github.com/Nitrate/python-nitrate-tcms.git \
          -r fedora-32-x86_64 \
          -r fedora-33-x86_64 \
          cqi/python-nitrate-tcms

  image-build:
    runs-on: ubuntu-latest
    needs: copr-build
    steps:
    - name: Install make
      run: sudo apt-get install make
    - uses: actions/checkout@v2
    - run: echo build image
    - run: |
        echo "RELEASE_VERSION=$(echo "${{ github.ref }}" | sed 's#refs/tags/v##')" >> $GITHUB_ENV
    - name: Build image
      run: |
        CONTAINER=docker RELEASE_VERSION=${RELEASE_VERSION} make release-image
    - name: Publish image
      run: |
        CONTAINER=docker RELEASE_VERSION=${RELEASE_VERSION} \
        QUAY_USER=${{ secrets.quay_username }} QUAY_PASSWORD=${{ secrets.quay_token }} \
        make publish-release-image
