name: "python packages -> PyPI.org"

#
# build a Bareos Python packages
# and publish it to https://test.pypi.org
# and publish releases also to https://pypi.org
#

on:
  push:
    branches:
      - master
      - dev/*/master/publish-pypi-*
    tags:
      - Release/*
  workflow_dispatch:
    inputs:
      ref:
        description: "The ref in the repository to checkout (use 'refs/tags/...' for tags)"
        type: string
        required: false
      publish_testpypi:
        # Using strings instead of boolean
        # makes handling as variables easier.
        description: 'Publish packages on test.pypi.org'
        type: choice
        options:
        - true
        - false
        default: false
      publish_pypi:
        # Using strings instead of boolean
        # makes handling as variables easier.
        description: 'Publish packages on pypi.org'
        type: choice
        options:
        - true
        - false
        default: false

env:
    # Note: env context is not available everywhere, see
    # https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/accessing-contextual-information-about-workflow-runs#context-availability
    publish_pypi: ${{ inputs.publish_pypi || 'true' }}
    publish_testpypi: ${{ inputs.publish_testpypi || 'true' }}
    ref: ${{ inputs.ref || github.ref }}
    is_release: ${{ startsWith( inputs.ref || github.ref, 'refs/tags/Release/') }}

jobs:
  build:
    name: "Build Python packages"
    runs-on: ubuntu-22.04

    steps:
    - name: "Show env"
      run: |
        echo "publish_pypi=${{ env.publish_pypi }}"
        echo "publish_testpypi=${{ env.publish_testpypi }}"
        echo "env.ref: ${{ env.ref }}"
        echo "is_release: ${{ env.is_release }}"
    - name: "Checkout source"
      uses: actions/checkout@v4
      with:
        fetch-depth: 0
        fetch-tags: true
        ref: ${{ inputs.ref || github.ref }}

    - name: "Python: set up"
      uses: actions/setup-python@v5
      with:
        python-version: '3.x'

    - name: "Python: install dependencies"
      run: |
        pip install --user wheel setuptools

    - name: "python-bareos: build package"
      run: |
        cd python-bareos
        # sdist mangles around with version information.
        # We replace ~pre with dev, as this will not be modified.
        # (pre will be replaced with rc).
        ../docs/manuals/source/get-version.sh > bareos/VERSION.txt
        printf "Version: %s\n" $(cat bareos/VERSION.txt)
        python setup.py sdist bdist_wheel

    - name: "python-bareos: create artifact"
      uses: actions/upload-artifact@v4
      with:
        name: python-bareos-dist
        path: python-bareos/dist/

    - name: "restapi: build package"
      run: |
        cd restapi
        # sdist mangles around with version information.
        # We replace ~pre with dev, as this will not be modified.
        # (pre will be replaced with rc).
        ../docs/manuals/source/get-version.sh > bareos_restapi/VERSION.txt
        printf "Version: %s\n" $(cat bareos_restapi/VERSION.txt)
        python setup.py sdist bdist_wheel

    - name: "restapi: create artifact"
      uses: actions/upload-artifact@v4
      with:
        name: restapi-dist
        path: restapi/dist/


  publish-python-bareos-to-testpypi:
    name: "python-bareos: publish to https://test.pypi.org/"
    needs:
    - build
    runs-on: ubuntu-22.04

    environment:
      name: testpypi
      url: https://test.pypi.org/p/python-bareos

    permissions:
      id-token: write  # IMPORTANT: mandatory for trusted publishing

    steps:
    - name: "Download artifact"
      uses: actions/download-artifact@v4
      with:
        name: python-bareos-dist
        path: dist/
    - name: "python-bareos -> https://test.pypi.org/"
      if: ${{ env.publish_testpypi == 'true' }}
      uses: pypa/gh-action-pypi-publish@release/v1
      with:
        repository-url: https://test.pypi.org/legacy/


  publish-python-bareos-to-pypi:
    name: "[Release] python-bareos: publish to https://pypi.org"
    if: ${{ startsWith( inputs.ref || github.ref, 'refs/tags/Release/') }} # only publish to PyPI on Release tag pushes
    needs:
    - build
    runs-on: ubuntu-22.04
    environment:
      name: pypi
      url: https://pypi.org/p/python-bareos
    permissions:
      id-token: write  # IMPORTANT: mandatory for trusted publishing

    steps:
    - name: "Download artifact"
      uses: actions/download-artifact@v4
      with:
        name: python-bareos-dist
        path: dist/
    - name: "python-bareos -> https://pypi.org/"
      if: ${{ env.publish_pypi == 'true' }}
      uses: pypa/gh-action-pypi-publish@release/v1


  publish-restapi-to-testpypi:
    name: "bareos-restapi: publish to https://test.pypi.org/"
    needs:
    - build
    runs-on: ubuntu-22.04

    environment:
      name: testpypi
      url: https://test.pypi.org/p/bareos-restapi

    permissions:
      id-token: write  # IMPORTANT: mandatory for trusted publishing

    steps:
    - name: "Download artifact"
      uses: actions/download-artifact@v4
      with:
        name: restapi-dist
        path: dist/
    - name: "bareos-restapi -> https://test.pypi.org/"
      if: ${{ env.publish_testpypi == 'true' }}
      uses: pypa/gh-action-pypi-publish@release/v1
      with:
        repository-url: https://test.pypi.org/legacy/


  publish-restapi-to-pypi:
    name: "[Release] bareos-restapi: publish to https://pypi.org"
    if: ${{ startsWith( inputs.ref || github.ref, 'refs/tags/Release/') }} # only publish to PyPI on Release tag pushes
    needs:
    - build
    runs-on: ubuntu-22.04
    environment:
      name: pypi
      url: https://pypi.org/p/python-bareos
    permissions:
      id-token: write  # IMPORTANT: mandatory for trusted publishing

    steps:
    - name: "Download artifact"
      uses: actions/download-artifact@v4
      with:
        name: restapi-dist
        path: dist/
    - name: "bareos-restapi -> https://pypi.org/"
      if: ${{ env.publish_pypi == 'true' }}
      uses: pypa/gh-action-pypi-publish@release/v1
