name: Release

on:
  push:
    tags:
      - '*'

jobs:
  release:
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        include:
          - os: ubuntu-18.04
            locale: C.UTF-8
          - os: macos-10.15
            locale: en_US.UTF-8
    env:
      LC_ALL: ${{ matrix.locale }}
      LANG: ${{ matrix.locale }}
    steps:
      - uses: actions/checkout@v2
      - name: Set up Python
        uses: actions/setup-python@v2
        with:
          python-version: 3.6
      - name: Upgrade pip
        run: python -m pip install --upgrade pip setuptools==44.0.0
      - name: Print info about the current python installation
        run: make ci-info
      - name: Install requirements
        run: make bootstrap-dev-plugins
      - name: Run tests
        run: make tests
      - name: Create bundle
        run: make bundle
      - name: Test bundle
        run: make ci-test-bundle
      - name: Get release description
        id: release-description
        # We must escape multi-line string, as per:
        # https://medium.com/agorapulse-stories/how-to-work-with-multiline-string-variables-in-github-actions-23f56447d209
        run: |
          make release-description
          cat release_description.md
          description="$(cat release_description.md)"
          description="${description//'%'/'%25'}"
          description="${description//$'\n'/'%0A'}"
          description="${description//$'\r'/'%0D'}"
          echo "::set-output name=text::$description"
        shell: bash
      - name: Get release file name
        id: release-file
        run: echo "::set-output name=filename::tutor-$(uname -s)_$(uname -m)"
        shell: bash
      - name: Debug release variables
        run: |
          echo "Publish file '${{ steps.release-file.outputs.filename }}' to release ${{ github.ref }}"
          echo "================"
          echo "${{ steps.release-description.outputs.text }}"
      - name: Upload bundle
        # https://github.com/marketplace/actions/upload-files-to-a-github-release
        uses: svenstaro/upload-release-action@v2
        with:
          repo_token: ${{ secrets.GITHUB_TOKEN }}
          file: ./dist/tutor
          asset_name: "${{ steps.release-file.outputs.filename }}"
          tag: ${{ github.ref }}
          overwrite: true
          body: "${{ steps.release-description.outputs.text }}"
