torrust-actix 4.2.3

A rich, fast and efficient Bittorrent Tracker.
name: All Build

on:
  push:
    branches:
      - master

permissions:
  contents: write

env:
  CARGO_TERM_COLOR: always

jobs:
  get_version:
    name: Get Version
    runs-on: ubuntu-latest
    outputs:
      version: ${{ steps.get_version.outputs.version }}
      upx_version: ${{ steps.get_upx_version.outputs.upx_version }}

    steps:
      - name: Checkout code
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Get version from Cargo.toml
        id: get_version
        shell: bash
        run: |

          VERSION="v$(grep "^version = " Cargo.toml | head -1 | awk '{print $3}' | tr -d '"')"
          echo "version=$VERSION" >> $GITHUB_OUTPUT
          echo "Version: $VERSION"

      - name: Get latest UPX version
        id: get_upx_version
        run: |

          UPX_VERSION=$(curl -s https://api.github.com/repos/upx/upx/releases/latest | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/')
          echo "upx_version=$UPX_VERSION" >> $GITHUB_OUTPUT
          echo "Latest UPX version: $UPX_VERSION"

  build:
    name: Build on ${{ matrix.os }}
    needs: get_version
    runs-on: ${{ matrix.os }}

    strategy:
      matrix:
        os: [ubuntu-latest, macos-latest, windows-latest]
        include:
          - os: ubuntu-latest
            binary_ext: .bin
            binary_name: linux
            strip_command: strip
            upx_base_url: https://github.com/upx/upx/releases/download
            upx_file: amd64_linux.tar.xz
          - os: macos-latest
            binary_ext: .bin
            binary_name: macos
            strip_command: strip
          - os: windows-latest
            binary_ext: .exe
            binary_name: windows
            strip_command: strip
            upx_base_url: https://github.com/upx/upx/releases/download
            upx_file: win64.zip

    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Setup Toolchain
        uses: dtolnay/rust-toolchain@stable

      - name: Test project
        run: cargo test --all-features --verbose

      - name: Build project
        run: cargo build --release --verbose

      - name: Strip binary
        shell: bash
        run: |

          if [ "${{ runner.os }}" = "Windows" ]; then
            "${{ matrix.strip_command }}" target/release/torrust-actix.exe
          else
            "${{ matrix.strip_command }}" target/release/torrust-actix
          fi

      - name: Download and install UPX (Linux/Windows)
        if: matrix.upx_base_url != ''
        shell: bash
        run: |

          UPX_VERSION="${{ needs.get_version.outputs.upx_version }}"
          UPX_VERSION_NOV="${UPX_VERSION#v}"
          UPX_URL="${{ matrix.upx_base_url }}/$UPX_VERSION/upx-$UPX_VERSION_NOV-${{ matrix.upx_file }}"
          echo "Downloading UPX from: $UPX_URL"
          if [ "${{ runner.os }}" = "Windows" ]; then
            mkdir -p upx-temp
            cd upx-temp
            curl -L "$UPX_URL" -o upx.zip
            unzip upx.zip
            cp upx-*/upx.exe ../upx.exe
            cd ..
            rm -rf upx-temp
          else
            cd /tmp
            curl -L "$UPX_URL" -o upx.tar.xz
            tar xf upx.tar.xz
            UPX_DIR=$(ls -d upx* | head -1)
            cd "$UPX_DIR"
            chmod +x upx
            pwd >> $GITHUB_PATH
          fi

      - name: Build UPX from source (macOS)
        if: runner.os == 'macOS'
        run: |

          cd /tmp
          git clone --recursive https://github.com/upx/upx.git
          cd upx
          git checkout $(git describe --tags --abbrev=0)
          git submodule update --init --recursive
          make all
          chmod +x build/release/upx
          echo "/tmp/upx/build/release" >> $GITHUB_PATH

      - name: Compress binary with UPX
        shell: bash
        run: |

          if [ "${{ runner.os }}" = "Windows" ]; then
            ./upx.exe --best --lzma target/release/torrust-actix.exe
          elif [ "${{ runner.os }}" = "macOS" ]; then
            upx --best --lzma --force-macos target/release/torrust-actix
          else
            upx --best --lzma target/release/torrust-actix
          fi

      - name: Rename binary with version
        shell: bash
        run: |

          VERSION="${{ needs.get_version.outputs.version }}"
          if [ "${{ runner.os }}" = "Windows" ]; then
            cp target/release/torrust-actix.exe "torrust-actix-${VERSION}-windows.exe"
          else
            cp target/release/torrust-actix "torrust-actix-${VERSION}-${{ matrix.binary_name }}${{ matrix.binary_ext }}"
          fi

      - name: Upload artifacts
        uses: actions/upload-artifact@v4
        with:
          name: torrust-actix-${{ matrix.binary_name }}${{ matrix.binary_ext }}
          path: torrust-actix-*${{ matrix.binary_ext }}

  create_release:
    name: Create Draft Release
    needs: [get_version, build]
    runs-on: ubuntu-latest

    steps:
      - name: Download all artifacts
        uses: actions/download-artifact@v4
        with:
          path: ./artifacts

      - name: Create Draft Release
        uses: softprops/action-gh-release@v1
        with:
          tag_name: ${{ needs.get_version.outputs.version }}
          name: Release ${{ needs.get_version.outputs.version }}
          draft: true
          prerelease: false
          generate_release_notes: true
          files: |

            ./artifacts/*/torrust-actix-*
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}