name: Release

on:
  create:
    tags:
      - v*

jobs:
  release:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@master
      - name: Set up Python 3
        uses: actions/setup-python@v1
        with:
          python-version: 3.7
      - name: Set up Node.js
        uses: actions/setup-node@v2
        with:
          node-version: '14'
      - name: Build frontend
        working-directory: ./webiojs
        run: |
          npm install
          gulp
          cp dist/pywebio.min.js ../pywebio/html/js
      - name: PyPi Upload
        run: |
          pip3 install twine
          python3 setup.py sdist
          twine upload --username "__token__" --disable-progress-bar --verbose dist/*
        env:
          TWINE_PASSWORD: ${{ secrets.PYPI_TOKEN }}
      - name: Set tag
        run: echo "RELEASE_VERSION=${GITHUB_REF#refs/*/}" >> $GITHUB_ENV
      - name: Push asset
        run: |
          git config --global user.email "wang0.618@qq.com"
          git config --global user.name "${{ github.actor }}"
          git config --global credential.helper '!f() { sleep 1; echo "username=${{ github.actor }}"; echo "password=${GH_TOKEN}"; }; f'

          git clone https://github.com/wang0618/pywebio-assets.git
          rm -rf pywebio-assets/*
          cp -r pywebio/html/* pywebio-assets

          cd pywebio-assets
          git add .
          git commit -m "Build from https://github.com/wang0618/PyWebIO/commit/$GITHUB_SHA" || true
          git tag $RELEASE_VERSION

          git push -u origin --tags
          git push -u origin || true
        env:
          GH_TOKEN: ${{ secrets.ASSET_REPO_TOKEN }}
