RuStream 0.0.0-alpha

Rustic API to stream videos from localhost
name: Build, test, upload artifact and release crate

on:
  push:

env:
  CARGO_TERM_COLOR: always

jobs:
  release:
    runs-on: ubuntu-latest
    permissions:
      contents: write
    outputs:
      release-flag: ${{ steps.set-release-flag.outputs.release_flag }}
    steps:
      - uses: actions/checkout@v3
      - name: Get Package Name
        run: |
          name=$(cargo metadata --no-deps --format-version 1 | jq -r '.packages[0].name')
          echo "Package Name: $name"
          echo "pkg_name=$name" >> $GITHUB_ENV
        shell: bash
      - name: Set Release Flag  # Release flag is set only for a push on main branch
        id: set-release-flag
        if: github.event_name == 'push'
        run: |
          current_version=$(cargo metadata --no-deps --format-version 1 | jq -r '.packages[0].version')
          latest_version=$(curl -s https://crates.io/api/v1/crates/${{ env.pkg_name }} | jq -r '.versions[0].num')
          echo "Current Package Version: ${current_version}"
          echo "Latest Package Version: ${latest_version}"
          if [ "$latest_version" != "$current_version" ]; then
            echo "Version has changed. Setting release flag to true."
            echo "release=true" >> $GITHUB_ENV
            echo "release_flag=true" >> "$GITHUB_OUTPUT"
          else
            echo "Version has not changed. Setting release flag to false."
            echo "release=false" >> $GITHUB_ENV
            echo "release_flag=false" >> "$GITHUB_OUTPUT"
          fi
          echo "pkg_version=$current_version" >> $GITHUB_ENV
        shell: bash
      - name: Release Crate
        if: env.release == 'true'
        run: |
          cargo login ${{ secrets.CRATES_TOKEN }}
          cargo publish --allow-dirty  # Set allow-dirty since building will create a /target folder that will be uncommitted in git
        shell: bash
      - name: Build
        if: env.release == 'false'
        run: cargo build --verbose
      - name: Run tests
        if: env.release == 'false'
        run: cargo test --verbose

  upload_assets:
    needs: release
    if: needs.release.outputs.release-flag == 'true'
    strategy:
      matrix:
        platform:
          - release_for: Linux-x86_64
            os: ubuntu-20.04
            target: x86_64-unknown-linux-gnu
            bin: stream
            name: RuStream-Linux-x86_64.tar.gz
            command: build

          - release_for: Windows-x86_64
            os: windows-latest
            target: x86_64-pc-windows-msvc
            bin: stream.exe
            name: RuStream-Windows-x86_64.zip
            command: build

          - release_for: macOS-x86_64
            os: macOS-latest
            target: x86_64-apple-darwin
            bin: stream
            name: RuStream-Darwin-x86_64.tar.gz
            command: build

          - release_for: RaspberryPi
            os: ubuntu-20.04
            target: arm-unknown-linux-gnueabihf
            bin: stream
            name: RuStream-RaspberryPi.tar.gz
            command: build

    name: Upload asset for ${{ matrix.platform.release_for }}
    runs-on: ${{ matrix.platform.os }}
    permissions:
      contents: write

    steps:
      - name: Checkout Repo
        uses: actions/checkout@v3
      - name: Setup Python
        uses: actions/setup-python@v5
        with:
          python-version: 3.9

      - name: Install OpenSSL static for Windows
        # https://github.com/sfackler/rust-openssl/issues/1086
        if: ${{ matrix.platform.os == 'windows-latest' }}
        run: |
          mkdir \Tools
          cd \Tools
          git clone https://github.com/Microsoft/vcpkg.git
          cd vcpkg
          .\bootstrap-vcpkg.bat
          .\vcpkg.exe install openssl:x64-windows-static

          $env:OPENSSL_DIR = 'C:\Tools\vcpkg\installed\x64-windows-static'
          $env:OPENSSL_STATIC = 'Yes'
          [System.Environment]::SetEnvironmentVariable('OPENSSL_DIR', $env:OPENSSL_DIR, [System.EnvironmentVariableTarget]::User)
          [System.Environment]::SetEnvironmentVariable('OPENSSL_STATIC', $env:OPENSSL_STATIC, [System.EnvironmentVariableTarget]::User)

      - name: Build
        run: |
          cargo build --release
        shell: bash

      - name: Run tests
        run: |
          cargo test
        shell: bash

      - name: Copy artifacts to folder and compress non windows
        if: ${{ matrix.platform.os != 'windows-latest' }}
        run: |
          mkdir -p rustream
          cp target/release/${{ matrix.platform.bin }} rustream/${{ matrix.platform.bin }}
          tar -zcvf ${{ matrix.platform.name }} rustream/

      - name: Copy artifacts to folder and compress Windows
        if: ${{ matrix.platform.os == 'windows-latest' }}
        run: |
          mkdir -p rustream
          cp target/release/${{ matrix.platform.bin }} rustream/${{ matrix.platform.bin }}
          Compress-Archive -DestinationPath ${{ matrix.platform.name }} -Path rustream/

      - name: Upload Asset to Release
        run: |
          curl -X POST -H "Authorization: token ${{ secrets.GIT_TOKEN }}" \
          -H "Content-Type: application/octet-stream" \
          --data-binary @"${{ env.asset_name }}" \
          "https://uploads.github.com/repos/${{ github.repository }}/releases/${{ env.release_id }}/assets?name=${{ matrix.platform.name }}"
        shell: bash