name: publish

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

  workflow_dispatch:
    inputs:
      publish:
        description: 'Manually publish'
        required: false
        type: boolean
        default: false

jobs:
  publish:
    if: inputs.publish == true || ${{ github.event_name == 'push' }}
    runs-on: [self-hosted, linux-3090]
    environment: 'prod'
    env:
      PREBUILD_DIR: /data2/actions-runner/prebuild
    steps:
      - name: Checkout repository
        uses: actions/checkout@v3
      - name: Get mmdeploy version
        run: |
          export MMDEPLOY_VERSION=$(python3 -c "import sys; sys.path.append('mmdeploy');from version import __version__;print(__version__)")
          echo $MMDEPLOY_VERSION
          echo "MMDEPLOY_VERSION=$MMDEPLOY_VERSION"  >> $GITHUB_ENV
          echo "OUTPUT_DIR=$PREBUILD_DIR/$MMDEPLOY_VERSION" >> $GITHUB_ENV
          pip install twine
      - name: Upload mmdeploy
        continue-on-error: true
        run: |
          cd $OUTPUT_DIR/mmdeploy
          ls -sha *.whl
          # twine upload mmdeploy-${MMDEPLOY_VERSION}-py3-none-manylinux2014_x86_64.whl --repository testpypi -u testmmdeploy -p ${{ secrets.test_pypi_password }}
          twine upload *.whl -u __token__ -p ${{ secrets.pypi_password }}
      - name: Upload mmdeploy_runtime
        continue-on-error: true
        run: |
          cd $OUTPUT_DIR/mmdeploy_runtime
          ls -sha *.whl
          # twine upload mmdeploy_runtime-${MMDEPLOY_VERSION}-cp38-none-manylinux2014_x86_64.whl --repository testpypi -u testmmdeploy -p ${{ secrets.test_pypi_password }}
          twine upload *.whl -u __token__ -p ${{ secrets.pypi_password }}
      - name: Create dummy softlinks to assets
        run: |
          ln -sf $OUTPUT_DIR/sdk ./prebuild
          ls -sha ./prebuild
      - name: Upload mmdeploy sdk
        uses: softprops/action-gh-release@v1
        with:
          tag_name: v${MMDEPLOY_VERSION}
          files: |
              ./prebuild/*.zip
              ./prebuild/*.tar.gz
