name: 'Frappe Assets'

on:
  release:
    types: [ created ]

env:
  GITHUB_TOKEN: ${{ github.token }}

jobs:
  build-release-and-publish:
    name: 'Build and Publish Assets built for Releases'
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2
        with:
          path: 'frappe'
      - uses: actions/setup-node@v1
        with:
          python-version: '12.x'
      - uses: actions/setup-python@v2
        with:
          python-version: '3.6'
      - name: Set up bench for current push
        run: |
          npm install -g yarn
          pip3 install -U frappe-bench
          bench init frappe-bench --no-procfile --no-backups --skip-assets --skip-redis-config-generation --python $(which python) --frappe-path $GITHUB_WORKSPACE/frappe
          cd frappe-bench && bench build

      - name: Package assets
        run: |
          mkdir -p $GITHUB_WORKSPACE/build
          tar -cvpzf $GITHUB_WORKSPACE/build/assets.tar.gz ./frappe-bench/sites/assets/js ./frappe-bench/sites/assets/css

      - name: Get release
        id: get_release
        uses: bruceadams/get-release@v1.2.0

      - name: Upload built Assets to Release
        uses: actions/upload-release-asset@v1.0.2
        with:
          upload_url: ${{ steps.get_release.outputs.upload_url }}
          asset_path: build/assets.tar.gz
          asset_name: assets.tar.gz
          asset_content_type: application/octet-stream
