name: Test on Windows, Docker/Linux and deploy package to PyPI

on:
  push:

permissions:
  checks: write
  contents: read

jobs:
  test-windows:
    runs-on: self-hosted
    if: ${{ contains(github.event.head_commit.message, '#test-windows') || contains(github.event.head_commit.message, '#release') }}

    steps:
    - uses: actions/checkout@v3

    - name: Install rc
      run: |
        pip install virtualenv
        python -m venv venv
        .\venv\Scripts\activate
        pip install .

    - name: Run tests (windows)
      shell: powershell
      run: |
        .\venv\Scripts\activate
        python test/test.py test/test.epp

  test-docker:
    needs: [ test-windows ]
    runs-on: self-hosted
    if: ${{ contains(github.event.head_commit.message, '#test-docker') || contains(github.event.head_commit.message, '#release') }}

    steps:
    - uses: actions/checkout@v3

    - name: Build docker image
      shell: powershell
      run: |
        New-Item -ItemType Directory -Path "${{github.workspace}}/test/licenses" -Force
        $env:ML_LIC_CONTENT | Out-File -FilePath "${{github.workspace}}/test/licenses/matlab.lic" -Encoding UTF8
        $env:BTC_LIC_CONTENT | Out-File -FilePath "${{github.workspace}}/test/licenses/btc.lic" -Encoding UTF8
        if (!(docker images -q ep-ml:24.2_23b)) {
          docker build -t ep-ml:24.2_23b test
        }
      env:
        ML_LIC_CONTENT: ${{ secrets.ML_LICENSE }}
        BTC_LIC_CONTENT: ${{ secrets.BTC_LICENSE }}

    - name: Run tests (docker/linux)
      run: |
        docker run --rm --mac-address 02:42:ac:11:13:37 `
            --volume "${{github.workspace}}:/workdir" `
            --workdir "/workdir" `
            ep-ml:24.2_23b `
            sh -c "pip3 install . && python3 test/test.py test/test.epp"

    - name: Publish results
      uses: dorny/test-reporter@v1
      if: success() || failure()
      with:
        name: Test results
        path: test/test_results.xml
        reporter: jest-junit
        only-summary: 'false'

  deploy:
    needs: [ test-windows, test-docker ]
    runs-on: ubuntu-latest
    if: ${{ contains(github.event.head_commit.message, '#deploy') || contains(github.event.head_commit.message, '#release') }}

    steps:
    - uses: actions/checkout@v3
        
    - name: Set up Python
      uses: actions/setup-python@v5
      with:
        python-version: '3.12'

    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        pip install setuptools wheel twine

    - name: Build package
      run: python setup.py sdist
    
    - name: Deploy package
      run: twine upload dist/* -u __token__
      env:
        TWINE_PASSWORD: ${{ secrets.PYPI_API_TOKEN }}
