name: Build and release
on: workflow_dispatch
permissions:
  contents: write

jobs:
  build-and-release:
    name: Build and release
    runs-on: ubuntu-20.04
    steps:
      - name: Checkout repository
        uses: actions/checkout@v3.5.2

      - name: Setup Docker
        uses: docker/setup-buildx-action@v2.5.0

      - name: Build toolchain
        uses: docker/build-push-action@v4.0.0
        with:
          cache-from: type=gha
          cache-to: type=gha,mode=max
          context: ./toolchain/
          load: true
          tags: rg35xx-toolchain:latest

      - name: Build project
        uses: addnab/docker-run-action@v3
        with:
          image: rg35xx-toolchain:latest
          options: -v ${{github.workspace}}:/root/workspace
          run: |
            export CROSS_COMPILE=/opt/rg35xx-toolchain/usr/bin/arm-buildroot-linux-gnueabihf-
            export PATH="/opt/rg35xx-toolchain/usr/bin:${PATH}:/opt/rg35xx-toolchain/usr/arm-buildroot-linux-gnueabihf/sysroot/bin"
            export PREFIX=/opt/rg35xx-toolchain/usr/arm-buildroot-linux-gnueabihf/sysroot/usr
            export UNION_PLATFORM=rg35xx
            make all

      - name: Name Git tag
        run: |
          export GIT_TAG=$(cat ./build/latest.txt)
          echo "GIT_TAG=$GIT_TAG" >> $GITHUB_ENV

      - name: Create Git tag
        uses: mathieudutour/github-tag-action@v6.1
        id: tag_version
        with:
          custom_tag: ${{env.GIT_TAG}}
          github_token: ${{secrets.GITHUB_TOKEN}}

      - name: Create GitHub release
        uses: ncipollo/release-action@v1.12.0
        with:
          artifacts: "releases/*b-0-full.zip"
          makeLatest: true
          prerelease: true
          tag: ${{steps.tag_version.outputs.new_tag}}
