name: Build on manylinux

on:
  workflow_call:
    inputs:
      version:
        type: string
        required: true
      matrix: # [ { name, runner, docker_tag }, ... ]
        type: string
        required: true
      release:
        type: boolean

jobs:
  build_on_manylinux:
    strategy:
      fail-fast: false
      matrix:
        include: ${{ fromJSON(inputs.matrix) }}
    name: ${{ matrix.name }}
    runs-on: ${{ matrix.runner }}
    container: wasmedge/wasmedge:${{ matrix.docker_tag }}
    steps:
      - uses: actions/checkout@v3
        with:
          fetch-depth: 0
      - name: Ensure git safe directory
        run: |
          git config --global --add safe.directory $(pwd)
      - name: Build ${{ matrix.docker_tag }} package
        run: |
          bash utils/docker/build-manylinux.sh
          ./build/tools/wasmedge/wasmedge -v
      - name: Upload artifact
        uses: actions/upload-artifact@v3
        with:
          path: build/WasmEdge-${{ inputs.version }}-Linux.tar.gz
          name: WasmEdge-${{ inputs.version }}-${{ matrix.docker_tag }}.tar.gz
      - name: Install gh on manylinux
        if: ${{ inputs.release }}
        run: |
          type -p yum-config-manager >/dev/null || sudo yum install yum-utils
          yum-config-manager --add-repo https://cli.github.com/packages/rpm/gh-cli.repo
          yum install -y gh
      - name: Upload rpm package
        if: ${{ inputs.release }}
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          mv build/WasmEdge-${{ inputs.version }}-Linux.rpm WasmEdge-${{ inputs.version }}-${{ matrix.docker_tag }}.rpm
          gh release upload ${{ inputs.version }} WasmEdge-${{ inputs.version }}-${{ matrix.docker_tag }}.rpm --clobber
      - name: Upload tar.gz package
        if: ${{ inputs.release }}
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          mv build/WasmEdge-${{ inputs.version }}-Linux.tar.gz WasmEdge-${{ inputs.version }}-${{ matrix.docker_tag }}.tar.gz
          gh release upload ${{ inputs.version }} WasmEdge-${{ inputs.version }}-${{ matrix.docker_tag }}.tar.gz --clobber
      - name: Upload tar.xz package
        if: ${{ inputs.release }}
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          mv build/WasmEdge-${{ inputs.version }}-Linux.tar.xz WasmEdge-${{ inputs.version }}-${{ matrix.docker_tag }}.tar.xz
          gh release upload ${{ inputs.version }} WasmEdge-${{ inputs.version }}-${{ matrix.docker_tag }}.tar.xz --clobber
