# https://docs.gitea.com/usage/actions/overview
# https://github.com/marketplace?type=actions
# https://gitea.com/actions
# https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions
name: Push Release Tag
on:
  push:
    tags:
      - 'v*'
      - '*_v*'

jobs:
  Build_and_draft_Release_tag:
    name: Build Release and Draft
    runs-on: ubuntu-latest
    steps:

      # https://github.com/actions/checkout
      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          fetch-depth: 0
          fetch-tags: true

      - name: Dev Tools Cache
        uses: actions/cache@v3
        with:
          path: |
            ~/.xpack-dev-tools
            /usr/bin/xxd
            /usr/bin/srec_cat
            /lib/x86_64-linux-gnu/libsrecord.so.0.0.0
            /lib/x86_64-linux-gnu/libsrecord.so.0
          key: ${{ runner.os }}-xpack-dev-tools-xxd-srecord

      - name: Dev Tools install
        run: sys/install.sh

      - name: Build M2 Release
        id: M2
        if: success() || failure()
        run: . sys/export.sh; make -e MODEL=M2 release

      - name: Build M3 Release
        id: M3
        if: success() || failure()
        run: . sys/export.sh; make -e MODEL=M3 release

      - name: Changelog
        if: success() || failure()
        run: sys/changelog.sh; cat CHANGELOG.md

        # https://github.com/actions/setup-node
      - name: Node setup
        # https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/accessing-contextual-information-about-workflow-runs#steps-context
        if: ( ${{ success() }} || ${{ failure() }} ) && ( ${{ steps.M2.outcome }} == 'success' || ${{ steps.M3.outcome }} == 'success' )
        uses: actions/setup-node@v4
        with:
          node-version: 18

      # https://github.com/akkuman/gitea-release-action
      - name: Upload Draft Release
        if: ( ${{ success() }} || ${{ failure() }} ) && ( ${{ steps.M2.outcome }} == 'success' || ${{ steps.M3.outcome }} == 'success' )
        uses: akkuman/gitea-release-action@v1
        with:
          token: '${{secrets.ACTION_TOKEN}}'
          body: 'Описание релиза'
          body_path: 'CHANGELOG.md'
          draft: true
          prerelease: true
          files: |-
            build/M2_Release/*.fw
            build/M3_Release/*.fw
