name: publish distributions
on:
  push:
    branches:
    - master
    tags:
    - v*
  pull_request:
    branches:
    - master
  workflow_dispatch:
    inputs:
      tag:
        description: 'Tag to publish'
        required: true
        default: ''

jobs:
  build-and-publish:
    name: Build and publish Python distro to PyPI
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v1
      if: github.event_name == 'workflow_dispatch'
      with:
        ref: ${{ github.event.inputs.tag }}
    - uses: actions/checkout@v2
      if: github.event_name != 'workflow_dispatch'

    - name: Set up Python 3.7
      uses: actions/setup-python@v2
      with:
        python-version: 3.7

    - name: Install build
      run: |
        python -m pip install build

    - name: Build a binary wheel and a source tarball
      run: |
        python -m build

    - name: Make files available as an artifact
      uses: actions/upload-artifact@v2
      with:
        path: dist/*

    - name: Publish distribution 📦 to PyPI
      if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags') && github.repository == 'scikit-hep/mplhep'
      uses: pypa/gh-action-pypi-publish@v1.3.1
      with:
        password: ${{ secrets.pypi_password }}

    - name: Publish distribution 📦 to PyPI when manually run
      if: github.event_name == 'workflow_dispatch'
      uses: pypa/gh-action-pypi-publish@v1.3.1
      with:
        password: ${{ secrets.pypi_password }}
